[SOLVED] preg_replace

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
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

[SOLVED] preg_replace

Post 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 !!!!
Last edited by Calimero on Fri Nov 26, 2004 5:32 pm, edited 1 time in total.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

preg_replace("/[\w]{,2}/ise", "", $body);
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

...

Post 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 !
Post Reply