08-20-2003, 10:11 AM | #1 (permalink) |
Insane
|
PHP preg_replace question
ok...let's see if I can explain this properly...
I have a string. It needs to: 1) Be split into an array of single words (check) 2) Be stripped of "noisewords" (here's the problem) 3) Be inserted into a "keywords" table in a MySQL database. Number 2 works...just too well. I have the word "the" in my noisewords array...the problem is, it not only replaces the word the, but also any time that "the" is used in another word. So "another" would become "anor". Actually, since I also have "a" and "no" specified as noisewords, it becomes "r". Any thoughts? MPEDrummer
__________________
My sig can beat up your honor student. |
08-20-2003, 11:00 AM | #2 (permalink) |
Muffled
Location: Camazotz
|
I'm not familiar with how php handles strings, but does it accept the space character as a legitamite string character? You could make " the " what's replaced. Just make sure to throw spaces into the replacement word.
Actually, it seems like you're not replacing, but removing. So just wrap the words to be stripped in spaces and that might work.
__________________
it's quiet in here |
08-20-2003, 11:34 AM | #3 (permalink) |
Upright
|
Actually, the best way to do this probably to use the "word boundary" escape character.
Each word in your array that is noise, you should put a \b on each side of the word: the => \bthe\b Mind you, it's been a while since I've used PHP, but that should work. Read RegExp Pattern Syntax for more information. |
Tags |
php, pregreplace, question |
|
|