使用特殊字符表示意外结果

I'm trying to find the position of the HTML element in a HTML document. So i do this:

    $filestring = file_get_contents($filename); //get the raw file
    $filestring = htmlspecialchars($filestring);

   $pos = strpos($filestring, "<head>"); //find the position of <head>
   print_r($pos); //print the position

End print_r don't show nothing. I think it is due to the special characters, but do not understand how to do.

There is no such things as <head> in $filestring. When you use htmlspecialchars, the < and > get replaced:

http://php.net/manual/en/function.htmlspecialchars.php

$pos = strpos($filestring, "&lt;head&gt;");

Or don't use htmlspecialchars when searching for the string

Why do you use htmlspecialchars? Do you understand that using this function causes all entities like > or < to be replaces by their representations like &gt; or &lt;?

So, the solutions are

  • either not use htmlspecialchars
  • or search not for <head> but for &lt;head&gt;

i think you should remove this line from your code.

$filestring = htmlspecialchars($filestring);