homepage Welcome to WebmasterWorld Guest from 54.196.18.51
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

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




msg:3497846
 5:07 pm on Nov 6, 2007 (gmt 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!

 

eelixduppy




msg:3498488
 7:31 am on Nov 7, 2007 (gmt 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).'/';

Bluestreak




msg:3498497
 7:42 am on Nov 7, 2007 (gmt 0)

I removed the whitespace "/s" and it fixed the problem. Weird, but I'm happy. :-)

eelixduppy




msg:3498501
 7:45 am on Nov 7, 2007 (gmt 0)

Glad you figured out the solution :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved