asmith Posted February 27, 2009 Share Posted February 27, 2009 Hi, I use this code to replace the words in the array, with links : <?php preg_replace('~('.implode('|', explode(',',$keywords)).')~','<a href="search.php?keyword=$0">$0</a>',$content)) ?> it works fine for english characters. But for other characters, I have to rawurlencode() them. I did the changes like this : (or some similar ways) : <?php preg_replace('~('.implode('|', explode(',',$keywords)).')~','<a href="search.php?keyword='.rawurlencode('$0').'">$0</a>',$content)) ?> But no success. How can I use rawurlencode inside that function? edit : its makes the link like keyword=$0 , and if O remove the single quotes around it, it gives me error. (That's obvious ^^) Quote Link to comment https://forums.phpfreaks.com/topic/147154-solved-need-to-use-rawurlencode-inside-the-preg_replace/ Share on other sites More sharing options...
rhodesa Posted February 27, 2009 Share Posted February 27, 2009 try this: <?php preg_replace('~('.implode('|', array_map('rawurlencode', explode(',',$keywords))).')~','<a href="search.php?keyword=$0">$0</a>',$content)) ?> Quote Link to comment https://forums.phpfreaks.com/topic/147154-solved-need-to-use-rawurlencode-inside-the-preg_replace/#findComment-772496 Share on other sites More sharing options...
asmith Posted February 27, 2009 Author Share Posted February 27, 2009 Thanks for the reply. I think with your solution, I will lose the $0 as of the link name too : <a href="search.php?keyword=$0">$0</a> I need the red one to be rawurlencode, but the blue one without it. I guess you code makes both rawurlencode(). Quote Link to comment https://forums.phpfreaks.com/topic/147154-solved-need-to-use-rawurlencode-inside-the-preg_replace/#findComment-772506 Share on other sites More sharing options...
rhodesa Posted February 27, 2009 Share Posted February 27, 2009 i like this way better: <?php foreach(explode(',',$keywords) as $keyword){ $content = str_ireplace($keyword,'<a href="search.php?keyword='.rawurlencode($keyword).'">'.$keyword.'</a>',$content); } ?> but if you want to stick with preg: <?php preg_replace('~('.implode('|', explode(',',$keywords)).')~e',"'<a href=\"search.php?keyword='.rawurlencode('\\0').'\">\\0</a>'",$content); ?> Quote Link to comment https://forums.phpfreaks.com/topic/147154-solved-need-to-use-rawurlencode-inside-the-preg_replace/#findComment-772514 Share on other sites More sharing options...
asmith Posted February 27, 2009 Author Share Posted February 27, 2009 It worked, Thanks a lot mate ^^ anyway, I came up with str_replace first, but isn't using one-line preg_replace faster than a str_replace loop ? Quote Link to comment https://forums.phpfreaks.com/topic/147154-solved-need-to-use-rawurlencode-inside-the-preg_replace/#findComment-772519 Share on other sites More sharing options...
rhodesa Posted February 27, 2009 Share Posted February 27, 2009 not sure, and the answer will probably be dependent on how big $keywords and $content is. but, I will say two things: -the foreach() code is WAY easier to read -the preg code will fail if a keyword has a ~ or a ) in it (and I image some other characters too) Quote Link to comment https://forums.phpfreaks.com/topic/147154-solved-need-to-use-rawurlencode-inside-the-preg_replace/#findComment-772527 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.