Hitesh Garg Hitesh Garg - 10 months ago 71
PHP Question

file_get_contents remove include header files from a C program

There is a C program file hosted on github and I am trying to get its content using php from its raw file url.
This is the code that I am using currently.

$x = file_get_contents("https://raw.githubusercontent.com/HiteshGarg/codingeek/master/Data-Structure/Queue/PriorityQueue.c");
echo $x;

This is able to fetch file but it removes
<stdio.h> and <stdlib.h>
and consider them as HTML tags when try to render themby appending their closing tag like -


//Other CODE in FILE


I have tried it using curl as well but no luck.

$curl = curl_init('https://raw.githubusercontent.com/HiteshGarg/codingeek/master/Data-Structure/Queue/PriorityQueue.c');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$x = curl_exec($curl);
echo $x;

Is there any way to show this as it is?

The real scenario is that I am using Wordpress and I have to embed this code inside a code highlighter. For that, I have added a shortcode but this is displaying additional HTML tags in the code displayed on the webpage. So I don't have to use echo or print in the real scenario rather I have to get a plain text and merge it in the existing post.

Answer Source

after you disclosed the scenario, the solution would be instead to escape HTML-sequences, so that your wordpress doesn't automatically close tags anymore.

$x = file_get_contents("https://raw.githubusercontent.com/HiteshGarg/codingeek/master/Data-Structure/Queue/PriorityQueue.c");
echo htmlentities($x);

(edit: removed old answer from before)