Title |
Test
Find
(X)HTML & char. replace
|
Expression |
&(?![a-z]+;|#\d+;) |
Description |
Single & character is not valid in HTML or XHTML code. In this case you can replace this character with its entity (if & character is not member of other entity [š]). example: preg_replace('/&(?!amp;)/', '&', $str).
(thanks to mr. Mrva) |
Matches |
& |
Non-Matches |
&|"|š|&blah;|© |
Author |
Rating:
Not yet rated.
Martin Švec
|
Source |
|
Your Rating |
|
Title: Close but not perfect
Name: Michael Ash
Date: 5/10/2007 11:19:07 AM
Comment:
Entities can also be written using hexadecimal notation which your regex doesn't account for. Also you should be careful when using the pound character in a regex as turning on the ignorewhitespace option can break your regex. Here's a regex I wrote a few years back to do the same thing. It put a limit of the range of values of the entities.
&(?!(?:\#((x([\dA-F]){1,5})|(104857[0-5]|10485[0-6]\d|1048[0-4]\d\d|104[0-7]\d{3}|10[0-3]\d{4}|0?\d{1,6}))|([A-Za-z\d.]{2,31}));)