rarebit Posted December 3, 2008 Share Posted December 3, 2008 Hi, I've got basically the same problem in JS as well, but the php version works in tests but not in production! Heres the test: <?php $s = "Hello Mr. >!"; $pattern = array('/\076:O/is', '/\x3e:O/is', '/\>:O/is'); $replacement = array('XYZ','xyz', 'abc'); $s = preg_replace($pattern, $replacement, $s); print $s; ?> and this is the other: $pattern = array( '/\[b\](.+?)\[\/b\]/is', '/\[i\](.+?)\[\/i\]/is', '/\[u\](.+?)\[\/u\]/is', '/\[h1\](.+?)\[\/h1\]/is', '/\[h2\](.+?)\[\/h2\]/is', '/\[h3\](.+?)\[\/h3\]/is', '/\[h4\](.+?)\[\/h4\]/is', '/\[sub\](.+?)\[\/sub\]/is', '/\[sup\](.+?)\[\/sup\]/is', '/\[tt\](.+?)\[\/tt\]/is', '/\[s\](.+?)\[\/s\]/is', '/\[em\](.+?)\[\/em\]/is', '/\[strong\](.+?)\[\/strong\]/is', '/\[samp\](.+?)\[\/samp\]/is', '/\[kbd\](.+?)\[\/kbd\]/is', '/\[code\](.+?)\[\/code\]/is', '/\[code=(.+?)\] Can you see why it's malfunctioning, or is it just the curse of the angry emoticon! Quote Link to comment https://forums.phpfreaks.com/topic/135397-solved-preg_replace/ Share on other sites More sharing options...
trq Posted December 3, 2008 Share Posted December 3, 2008 What do you meen by malfunctioning? Quote Link to comment https://forums.phpfreaks.com/topic/135397-solved-preg_replace/#findComment-705334 Share on other sites More sharing options...
rarebit Posted December 3, 2008 Author Share Posted December 3, 2008 The production variation just misses it out and leave it as is, but the demo recognises it and changes it. I can't see what the difference is! Quote Link to comment https://forums.phpfreaks.com/topic/135397-solved-preg_replace/#findComment-705455 Share on other sites More sharing options...
DarkWater Posted December 3, 2008 Share Posted December 3, 2008 You shouldn't need to escape >. <?php $str = "Something with some SMILEYS! >"; echo preg_replace('/>:O/i', '***', $str); ?> Output: Something with some SMILEYS! *** Here's a little tip though. Use preg_quote() and stuff to make safe patterns: $s = "Hello Mr. >! I'm so to see you."; $pattern = array(">", ""); function fix_preg_array($val) { return '/' . preg_quote($val, '/') . '/i'; } $pattern = array_map('fix_preg_array', $pattern); print_r($pattern); preg_quote() escapes all potentially bad characters. Quote Link to comment https://forums.phpfreaks.com/topic/135397-solved-preg_replace/#findComment-705482 Share on other sites More sharing options...
rarebit Posted December 3, 2008 Author Share Posted December 3, 2008 The thing is, it works ok in the tests, but this doesn't do it either? '/'.preg_quote('>', '/').'/is', and i've just noticed that some of the original code was truncated... so to show I am smiling on the inside... $pattern = array( '/\[b\](.+?)\[\/b\]/is', '/\[i\](.+?)\[\/i\]/is', '/\[u\](.+?)\[\/u\]/is', '/\[h1\](.+?)\[\/h1\]/is', '/\[h2\](.+?)\[\/h2\]/is', '/\[h3\](.+?)\[\/h3\]/is', '/\[h4\](.+?)\[\/h4\]/is', '/\[sub\](.+?)\[\/sub\]/is', '/\[sup\](.+?)\[\/sup\]/is', '/\[tt\](.+?)\[\/tt\]/is', '/\[s\](.+?)\[\/s\]/is', '/\[em\](.+?)\[\/em\]/is', '/\[strong\](.+?)\[\/strong\]/is', '/\[samp\](.+?)\[\/samp\]/is', '/\[kbd\](.+?)\[\/kbd\]/is', '/\[code\](.+?)\[\/code\]/is', ****************************************** '/\[quote=(.+?)\](.+?)\[\/quote\]/is', '/\[quote\](.+?)\[\/quote\]/is', '/\[size=(.+?)\](.+?)\[\/size\]/is', '/\[color=(.+?)\](.+?)\[\/color\]/is', '/\[url\]((www|ftp|)\.[^ \\\"\\n\\r\\t<]*?)\[\/url\]/is', '/\[url=([\w]+?:\/\/[^ \\\"\\n\\r\\t<]*?)\](.*?)\[\/url\]/is', '/\[url=((www|ftp)+?\.[^ \\\"\\n\\r\\t<]*?)\](.*?)\[\/url\]/is', '/\[url\]([\w]+?:\/\/[^ \\\"\\n\\r\\t<].*?)\[\/url\]/is', '/\[img\]((www)\.[^ \\\"\\n\\r\\t<]*?)\[\/img\]/is', '/\[img\]([\w]+?:\/\/[^ \\\"\\n\\r\\t<].*?)\[\/img\]/is', '/\[img\]((\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\/[^ \\\"\\n\\r\\t<]*?)\[\/img\]/is', '/\[email\]([\w]+?@(([\w]+)([\w]+))\.(([\w]+)([\w]+)).*?)\[\/email\]/is', '/\[email=((([\w]+)@(([\w]+)([\w]+))\.(([\w]+)([\w]+))).*?)\](.+?)\[\/email\]/is', '/:-\)/is', '/:-D/is', '/;-D/is', '/;-\)/is', '/:-\*/is', '/S-\)/is', '/8-D/is', '/\?\?\?/is', '/:-P/is', '/:-O/is', "/>:O/is", '/:-\[/is', '/:-\(/is', '/:\*\(/is', '/:-X/is', '/:-\//is' ); //'/'.preg_quote('>', '/').'/is', $replacement = array( '<b>$1</b>', '<i>$1</i>', '<u>$1</u>', '<h1>$1</h1>', '<h2>$1</h2>', '<h3>$1</h3>', '<h4>$1</h4>', '<sub>$1</sub>', '<sup>$1</sup>', '<tt>$1</tt>', '<s>$1</s>', '<em>$1</em>', '<strong>$1</strong>', '<samp>$1</samp>', '<kbd>$1</kbd>', "<table width='100%' cellpadding='5' bgcolor='".$theme_wysiwyg['bgcode']."'><tr><td><pre class='codex'><code class='codex'>$1</code></pre></td></tr></table>", "<table width='100%' cellpadding='5' bgcolor='".$theme_wysiwyg['bgcode']."'><tr><td><b>$1</b><hr noshade></td></tr><tr><td><pre class='codex'><code class='codex'>$2</code></pre></td></tr></table>", "<table width='100%' cellpadding='5' class='quotex'><tr><td><b>$1</b><hr noshade></td></tr><tr><td><q class='quotex'>$2</q></td></tr></table>", "<table width='100%' cellpadding='5' class='quotex'><tr><td><q>$1</q class='quotex'></td></tr></table>", "<span style='font-size:$1'>$2</span>", "<span style='color:$1'>$2</span>", "<a href='http://$1' target='blank'>$1</a>", "<a href='$1' target='blank'>$2</a>", "<a href='http://$1' target='blank'>$3</a>", "<a href='$1' target='blank'>$1</a>", "<img src='$1' alt='$1'>", "<img src='$1' alt='$1'>", "<img src='http://$1' alt='http://$1'>", "<a href='mailto:$1'>$1</a>", "<a href='mailto:$1'>$10</a>", "<img src='".$base."smiley.gif'>", "<img src='".$base."grin.gif'>", "<img src='".$base."cheesy.gif'>", "<img src='".$base."wink.gif'>", "<img src='".$base."kiss.gif'>", "<img src='".$base."rolleyes.gif'>", "<img src='".$base."cool.gif'>", "<img src='".$base."huh.gif'>", "<img src='".$base."tongue.gif'>", "<img src='".$base."shocked.gif'>", "<img src='".$base."angry.gif'>", "<img src='".$base."embarrassed.gif'>", "<img src='".$base."sad.gif'>", "<img src='".$base."cry.gif'>", "<img src='".$base."lipsrsealed.gif'>", "<img src='".$base."undecided.gif'>" ); $s = preg_replace($pattern, $replacement, $s); The starry line is just another variation on code blocks, but it rally did get in the way here so it ended up right out of the way... I've also got this issue on the exact same string when using JS... me eh poco loco eh a losta! Quote Link to comment https://forums.phpfreaks.com/topic/135397-solved-preg_replace/#findComment-705506 Share on other sites More sharing options...
rarebit Posted December 6, 2008 Author Share Posted December 6, 2008 OK, OK, I was being silly! It turns out that I was doing some pre-parsing of < > elements to disable script inclusions, so i've added another parsed variation to the list: "/>:O/is", Solves for both JS and PHP! Cheers... Quote Link to comment https://forums.phpfreaks.com/topic/135397-solved-preg_replace/#findComment-707605 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.