Welcome to WebmasterWorld Guest from 54.146.230.149

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Why won't preg replace replace the same character twice in a row?

     
5:07 pm on Nov 6, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:May 31, 2002
posts: 157
votes: 0


Hey all,

I'm trying to debug a third party regex script used to display smileys, and while it works, it won't display the same smiley more than twice in a row. Here's the code I'm looking at:

function lmbbox_smileys_comment_convert($text) {
global $lmbbox_smileys;
return preg_replace($lmbbox_smileys->comment_smileys['search'], $lmbbox_smileys->comment_smileys['replace'], $text);
}

Here's the array I think it's using:

foreach ($info['smileys'] as $smiley => $img) {
if (!in_array($img, $info['disabled']['comment'])) {
$this->comment_smileys['search'][] = '/(^¦\s)' . preg_quote($smiley) . '(\s¦\W¦$)/';
$this->comment_smileys['replace'][] = '\\1<img class="lmbbox_smileys_img" src="' . $this->manage['smileys_url_path'] . $smileys . '/' . $img . '" alt="' . htmlspecialchars(trim($smiley) , ENT_QUOTES) . '" />\\2';

For those familiar with preg_replace, any idea why it won't replace the same smiley more than twice in a row? If I use the same three smileys for example, the first and third smiley will be replaced, but the second remains unchanged. Drives me nuts. :-P

Any help or guidance appreciated!

7:31 am on Nov 7, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


My guess is that you have an error in your pattern. Try something like this to see if it helps:

$this->comment_smileys['search'][] = '/'.preg_quote($smiley).'/';
7:42 am on Nov 7, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:May 31, 2002
posts: 157
votes: 0


I removed the whitespace "/s" and it fixed the problem. Weird, but I'm happy. :-)
7:45 am on Nov 7, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 0


Glad you figured out the solution :)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members