[RESOLVED] Replacing Repeated Words
I've been experimenting with preg_replace() today, but I can't figure out how to replace words that are repeated. I can the first word replaced, but the same word right after that word is still output to the browser.
What I'm trying to do is replace a word that a) has one or more characters in front of it (except letters) followed by b) the substring "the" followed by c) one or more characters that are not letters. What am I doing wrong?
$string1 = '/[^a-zA-Z]+(the)[^a-zA-Z]+/i';
$string2 = 'ha';
$string3 = 'A the the joke';
echo preg_replace($string1, $string2, $string3);
Well, I got the last problem fixed, but now there's something I don't understand. In the sentence 'Chuff chuffing chuffer!', it's replaced by '!@#$%!@#$%' instead of '!@#$%!@#$%!@#$%'. What gives?
// create an empty array for words
$swear = array();
// fill the array with word patterns
$swear = '/[^a-zA-Z]+?(chuffing)[^a-zA-Z]??/i';
$swear = '/(chuffing)[^a-zA-Z]??/i';
$swear = '/[^a-zA-Z]+?(chuffer)[^a-zA-Z]??/i';
$swear = '/^(chuffer)[^a-zA-Z]??/i';
$swear = '/[^a-zA-Z]+?(chuff)[^a-zA-Z]??/i';
$swear = '/^(chuff)[^a-zA-Z]??/i';
// replace the words
$string = preg_replace($swear, '!@#$%', $string);
// return it
Hey, nothing to do with this post but Joseph Witchard - I love your signiture, stamp out those idiots =)
Lack of knowledge != idiocy. For example, you've posted here asking for help, does that make you an idiot?
Well yeah i guess your right on that one. Not like i thought about that =p
Well, anyway, I've got the problem worked out. Resolved.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)