I'm looking for an RegExp-pattern which converts all ampersands ( & ) to its system identifier " & ". The reason i want to use RegExp is becuase if using normal Str_Replace() to replace all & with & , other system-identifiers which starts with & gets invalid, for exmaple:
If i replace & with & in this string: " sometext someothertext ",
it turns into: " sometext someothertext "
So i'm looking for e regexp pattern which finds all & which is alone, and haven't got an ; after them. Are there any Regexp gurus out there who can solve this?
Auto-escape ampersands[solved]
Moderator: General Moderators
Auto-escape ampersands[solved]
Last edited by vigge89 on Thu May 20, 2004 5:44 am, edited 3 times in total.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
$string = "this is a test & show of regex";
preg_replace("/(\W)&(\W)/","\\1&\\2",$string)great, thanks! 
Edit: after trying this:
i get an blank output, any ideas?
Edit: after trying this:
Code: Select all
<?php
echo preg_replace ("/(\W)\&(\W)/","\\1&\\2", "<a href='http://domain.com/index.php?pagetype=2&article=30262§ionid=1585' target='_blank'>");
?>Try this:
It uses a negative look-ahead assertion (the (?!...) part), which says "replace any ampersands not followed by between 3 and 7 letters/numbers/#-signs and a semi-colon. That means it'll match & signs, but not & signs in HTML entities (you can fiddle with the 3,7 limits, but the largest entity I found had 6 chars, so with the hash, is 7, so it shoudl cover almost anything).
I tried it with a string full of entities, and even ampersands within words, and it worked fine. That's no guarantee of course, as I might have missed something blatantly obvious :)
Code: Select all
preg_replace("/&(?![A-Za-z0-9#]{3,7};)/", "&", $text);I tried it with a string full of entities, and even ampersands within words, and it worked fine. That's no guarantee of course, as I might have missed something blatantly obvious :)