Domanda

So first I noticed when an ampersand (&) is in the htmlentities function it will count the ampersand as 5 characters. So this code:

$a = htmlentities("&12345");
$b = substr($a,0,6);
  echo $b; 

would echo '&1' as, I believe it is counting & as 5 characters. more interestingly is with the GBP symbol (£) which is ignored all together so this:

$a = htmlentities("£");
  echo $a;   

echos nothing. I get the same results on Chrome and FF. I don't know if this is a bug or I should use a different syntax. Does anyone know why this might be happening? Thanks

UPDATE

I have solved the £ issue with this: $a = htmlentities("£", ENT_COMPAT, 'ISO-8859-15'); but the ampersand issue remains.

È stato utile?

Soluzione

The problem is that you are viewing the output in the browser window where output from htmlentities (and equivalent functions) is rendered for display to an end user.

For example:

echo htmlentities("&");

Will output & because it has converted the & character to it's html entity equivalent. See that it is five characters long. However, you don't see the full text & because you're viewing from your browser which has pre-rendered it as the & symbol. In firfox if you right click the viewport and click on "View Page Source" you will see the full text &...

Your code:

$a = htmlentities("&12345"); //Outputs: &12345
$b = substr($a, 0, 6); //Selects first six charachters: &, a, m, p, ; and 1
echo $b; // Echo's: &1 which is displayed by the browser as &1

To get around this you can change the order of your functions:

$a = substr("&12345", 0, 6);
echo htmlentities($a);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top