質問

I want to encode entities to prevent XHTML from breaking though without encoding XHTML elements themselves. So leaving things like carets alone but encoding ampersands for example. The following is a minimal example of what I've tried messing with (yes I know it's not valid XHTML, just for testing purposes).

<?php
$b = '&<br />&';
//echo htmlentities($b,ENT_XHTML);
echo htmlspecialchars($b);
?>

The desired output...

&

&

役に立ちましたか?

解決

If I understand you correctly, you want to only encode ampersands, try this:

echo str_replace('&', '&amp;', $b);

[Edit]

If you're concerned about encoding instances of &amp; into &amp;amp;, you could use:

echo preg_replace('/&[^(amp;)]/', '&amp;', $b);

In this case Test & &amp; would be encoded as Test &amp; $amp;

他のヒント

My catch all...

<?php
$s = 'example & and &#38;.';
$f = array('&','&#38;#38;','&amp;','&#38;amp;');
$r = array('&#38;','&#38;','&#38;','&#38;');
echo str_replace($f,$r,$b);
?>

After creating a thousand entity test case, copying all the characters to an XHTML file all that truly needed to be encoded were the ampersands.

For those testing and encounter weird characters such as à make sure your page is encoded as UTF-8 and not windows-1252 for example.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top