CastenettoA
CastenettoA

Reputation: 693

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.

Upvotes: 1

Views: 453

Answers (3)

Divyesh Jesadiya
Divyesh Jesadiya

Reputation: 957

i think you should remove this line from your code.

$filestring = htmlspecialchars($filestring);

Upvotes: 0

PSZ_Code
PSZ_Code

Reputation: 1045

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

Upvotes: 3

u_mulder
u_mulder

Reputation: 54841

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;

Upvotes: 3

Related Questions