PDA

View Full Version : eregi_replace problem with regex


Alex Piotto
08-20-2002, 07:27 PM
Hi my friends!
It is possible with regex to remove/replace from a string all the words (but not the numbers) that are 3 or less characters long?
I am trying with this

$keywords = eregi_replace("[a-z]{3}", " ", $words);

but it cuts the first three character of any word... it is not right!

Suggestions?

Alex :confused:

beetle
08-22-2002, 03:15 PM
Do you want to replace associated punctuation and spacing too? I don't know how to do backreferences in PHP regex's, but I got this to work in javascript (mostly)

<script language="javascript" type="text/javascript">
var words = "These are some words. aha!! Some are big, it's one two three four! five six? seven ate nine.";
document.write(words);
var keywords = words.replace(/([ ]+)([a-z]{2}[']?[a-z]{1})([ \?!,\.]+)/gi, "$1 $3");
document.write("<br>"+keywords);
</script>Anyone know how backreferences work in PHP?

mordred
08-22-2002, 03:54 PM
If you just want to remove matches, you don't need backreferences.


$words = 'Here oh are x some words that can be longer than 3 characters';

$keywords = preg_replace("/(\\b[a-z]{1,3}\\b)/i", "", $words);
echo $keywords . "\n";
// ^ prints 'Here some words that longer than 3 characters'

$keywords = preg_replace("/(.*?\\s*)(\\b[a-z]{1,3}\\b)\\s+/i", "\\\\1", $words);
echo $keywords;
// ^ prints 'Here some words that longer than 3 characters'


The first one is quite easy; it just removes the matches. That will leave unneccesary whitespace in the original string though, to handle this circumstance I ended up with the second regexp. The second one works, but is significantly slower.

@beetle:
Have a good read at
http://se.php.net/manual/en/pcre.pattern.syntax.php (scroll down to "BACK REFERENCES").

EDIT: Arghl, this forum eats backslashes.