1

Very simply, i want to make a variable reads the html code as string ,, i mean dont execute it (run it) .

the problem with the code is : i have a html file , and i want to get the content of it , and make some preg_replace for it (run a function on the html code), the problem is i cant use preg_replace, or any another function because the html code is executed by php (php reads the html code)..

i wish you understand me, i want something like highlight_string, but it save the html code in the variable.

Thank you.

2 Answers 2

5

you're probably trying to include or require the HTML code. which is incorrect since it is evaluated as part of the source.

instead, use a function such as file_get_contents() to read the file into a string.

3
  • PHP is a templating engine :)
    – Jim
    Jun 3, 2011 at 19:33
  • yes i did,, im already using file_get_contents, but the problem is when i print the var, it give me the executed code .. an example ... the file of html contant '<p>test test</p>' ... when i get the content and 'echo file_get_contents($filepath)' the php return test test .. Jun 3, 2011 at 19:52
  • I dont think you have a PHP issue. Do you need to view source on your browser?
    – David Chan
    Jan 8, 2012 at 5:03
0

Use file_get_contents() as @David Chan suggested and then pass the result through htmlentities()... it converts the characters to HTML entities (i.e., < to &lt;).

$getTheContent = file_get_contents($filepath);
echo htmlentities($getTheContent);

It should return the code, not executed.

1
  • Thank you, its good idea .. but the problem is i have a regex patterns , and the regex patterns should find the <> so here is the problem ,,, i want the way which using in <form> ,, when it send the inputs it never executed. Thank you. Jun 4, 2011 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.