Hitesh Garg Hitesh Garg - 1 month ago 7
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.

<?php
$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 -

#include<stdio.h>
#include<stdlib.h>

//Other CODE in FILE

</stdlib.h></stdio.h>


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?

EDIT -
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

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

<?php
$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)

Comments