Search code examples
phphtmlstrpos

Strpos unexpected result with special characters


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.


Solution

  • 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