Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/135397-solved-preg_replace/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/135397-solved-preg_replace/#findComment-705482
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/135397-solved-preg_replace/#findComment-705506
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.