Page 1 of 1

[SOLVED] preg_replace

Posted: Fri Nov 26, 2004 1:04 pm
by Calimero
Ok, I'm in a hurry - to write this, so try to do my best in a few words (without sample code) :D

I take a string from a textfield (POST method) and use this function to replace all 2 letter and shorter words with "" - nothing, just need to kill them.

It works fine, but only every second value is replaced,

change 2 letter word into ---

Output:

--- 12 --- 12 --- 12 --- 12 --- 12 --- etc...


I added \s vefore and after the $pattern, but is\t still doesn't work.


There was only one solution I made working, but it takes CPU resources,
that is to preg_replace every \s with " " - two blanks.


I tried for $i = 0;, $i < 1000000; $i++)
{
do replace
}

but on the 1000001 word it fail's :twisted:


Anyone knows of better ways ?


Thanks Ahead !!!!

Posted: Fri Nov 26, 2004 4:56 pm
by Steveo31
preg_replace("/[\w]{,2}/ise", "", $body);

...

Posted: Fri Nov 26, 2004 5:31 pm
by Calimero
Got the little basta...,

Code: Select all

<?php

echo preg_replace("/\b[\w]{1,2}\b/i", " ", $string_to_check);

?>

Been using \s instead of \b.


I hope this will do the job without any flaws or security errors.


If anyone has a comment, do reply.


See ya !