preg_replace pattern to html entities ampersands

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Walid
Forum Commoner
Posts: 33
Joined: Mon Mar 17, 2008 8:43 am

preg_replace pattern to html entities ampersands

Post by Walid »

Hi all,

Can anyone help me out in putting together an expression which will run through the document and replace all cases of the ampersand sign with the ampersand html entity leaving out those ampersands which already belong to an html entity

Thanks
Walid
Forum Commoner
Posts: 33
Joined: Mon Mar 17, 2008 8:43 am

Re: preg_replace pattern to html entities ampersands

Post by Walid »

Over at http://www.addedbytes.com/html/on-the-fly-validation/, I found this one which seems to do the trick:

$output = preg_replace("/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w{1,8});)/i", "&", $output);

Any comments?
Post Reply