...

View Full Version : I want to edit two variables with preg_replace(), but how?



Jero
08-17-2004, 01:20 PM
This is the code:


if($use_bb == 1) {
$message = preg_replace('/\[b\](.*?)\[\/b\]/is', '<b>\\1</b>', $message);
$message = preg_replace('/\[i\](.*?)\[\/i\]/is', '<i>\\1</i>', $message);
$message = preg_replace('/\[u\](.*?)\[\/u\]/is', '<u>\\1</u>', $message);
$message = preg_replace("#\[s\](.*?)\[/s\]#si", "<s>\\1</s>", $message);
if($use_bblinks == 1) {
$message = preg_replace("#\[link=([a-z]+?://){1}([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\](.*?)\[/link\]#", "<a href=\"\\1\\2\" target=\"_blank\">\\3</a>", $message);
$message = preg_replace("#\[email={1}([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\](.*?)\[/email\]#", "<a href=\"mailto:\\1\">\\2</a>", $message);
}
$message = preg_replace('/\[code\](.*?)\[\/code\]/is', '<b style=\"padding-left:5px\">Code: </b> <div class="code_message">\\1</div>', $message);
if($use_bbimg == 1)
// $message = eregi_replace("\\[img\\]([^\\[]*?)\\[/img\\]", "<img src=\"\\1\" border=\"0\" alt=\"\" />", $message);
$message = preg_replace ("#\[quote=(.*?)\](.*?)\[/quote\]#si", "<br /><b style=\"padding-left:5px\">Quote \\1:</b><div class=\"quote_message\">\\2</div>", $message);
$message = preg_replace ("#\[color=(\#[0-9A-F]{6}|[a-z\-]+)\](.*?)\[/color\]#si", "<font color=\"\\1\">\\2</font>", $message);
$message = preg_replace ("#\[font=(.*?)\](.*?)\[/font\]#si", "<font face=\"\\1\">\\2</font>", $message);
$message = preg_replace("#\[list\](.*?)\[/list\]#si", "<ul>\\1</ul>", $message);
$message = preg_replace("#\[li\](.*?)\[/li\]#si", "<li>\\1</li>", $message);
$message = preg_replace("#\[left\](.*?)\[/left\]#si", "<div style=\"text-align:left\">\\1</div>", $message);
$message = preg_replace("#\[center\](.*?)\[/center\]#si", "<div style=\"text-align:center\">\\1</div>", $message);
$message = preg_replace("#\[right\](.*?)\[/right\]#si", "<div style=\"text-align:right\">\\1</div>", $message);
}

if($use_html == 1) {
$message = preg_replace('/&lt;b&gt;(.*?)&lt;\/b&gt;/is', '<b>\\1</b>', $message);
$message = preg_replace('/&lt;i&gt;(.*?)&lt;\/i&gt;/is', '<i>\\1</i>', $message);
$message = preg_replace('/&lt;u&gt;(.*?)&lt;\/u&gt;/is', '<u>\\1</u>', $message);
$message = preg_replace('/&lt;s&gt;(.*?)&lt;\/s&gt;/is', '<s>\\1</s>', $message);
if($use_htmllinks == 1) {
$message = preg_replace("#&lt;a href=\"([a-z]+?://){1}([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\"&gt;(.*?)&lt;/a&gt#", "<a href=\"\\1\\2\" target=\"_blank\">\\3</a>", $message);
$message = preg_replace("#&lt;a mailto:\"{1}([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\"&gt;(.*?)&lt;/a&gt#", "<a href=\"mailto:\\1\">\\2</a>", $message);
}

mordred
08-17-2004, 01:26 PM
Please elaborate. Where's the problem? What is not working? You wrote that you wanted to "edit" two variables, but to which lines in the code posted does that refer?

Jero
08-17-2004, 01:35 PM
I want to use the preg_replace fot $messages and another variable ($sig), but simply added $sig at the end of the preg_replace code won't work.

mordred
08-17-2004, 03:13 PM
Whatever you meant by "added $sig at the end of the preg_replace code", in your case I would put the regex code in a function, and pass the string I want the regexes to work on as a parameter to this function. Then you only have to make to function calls and store the returned value.

Jero
08-17-2004, 03:21 PM
Uhm, could you please give me a little example of how it should look like? And I'm sorry for not being so clear.

mordred
08-17-2004, 03:27 PM
Sure. It's not complete, but shows the general picture:



function replace($input) {
// here comes the regex code...
return $input;
}

$message = replace($message);
$sig = replace($sig);

Jero
08-17-2004, 03:37 PM
Sweet! I'll try it.

Jero
08-17-2004, 03:55 PM
:confused: I can get it to work. This is what I did:


function replace($input) {
if($use_bb == 1) {
$input = preg_replace('/\[b\](.*?)\[\/b\]/is', '<b>\\1</b>', $input);
$input = preg_replace('/\[i\](.*?)\[\/i\]/is', '<i>\\1</i>', $input);
$input = preg_replace('/\[u\](.*?)\[\/u\]/is', '<u>\\1</u>', $input);
$input = preg_replace("#\[s\](.*?)\[/s\]#si", "<s>\\1</s>", $input);
if($use_bblinks == 1) {
$input = preg_replace("#\[link=([a-z]+?://){1}([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\](.*?)\[/link\]#", "<a href=\"\\1\\2\" target=\"_blank\">\\3</a>", $input);
$input = preg_replace("#\[email={1}([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\](.*?)\[/email\]#", "<a href=\"mailto:\\1\">\\2</a>", $input);
}
$input = preg_replace('/\[code\](.*?)\[\/code\]/is', '<b style=\"padding-left:5px\">Code: </b> <div class="code_message">\\1</div>', $input);
if($use_bbimg == 1)
// $message = eregi_replace("\\[img\\]([^\\[]*?)\\[/img\\]", "<img src=\"\\1\" border=\"0\" alt=\"\" />", $input);
$input = preg_replace ("#\[quote=(.*?)\](.*?)\[/quote\]#si", "<br /><b style=\"padding-left:5px\">Quote \\1:</b><div class=\"quote_message\">\\2</div>", $input);
$input = preg_replace ("#\[color=(\#[0-9A-F]{6}|[a-z\-]+)\](.*?)\[/color\]#si", "<font color=\"\\1\">\\2</font>", $input);
$input = preg_replace ("#\[font=(.*?)\](.*?)\[/font\]#si", "<font face=\"\\1\">\\2</font>", $input);
$input = preg_replace("#\[list\](.*?)\[/list\]#si", "<ul>\\1</ul>", $input);
$input = preg_replace("#\[li\](.*?)\[/li\]#si", "<li>\\1</li>", $input);
$input = preg_replace("#\[left\](.*?)\[/left\]#si", "<div style=\"text-align:left\">\\1</div>", $input);
$input = preg_replace("#\[center\](.*?)\[/center\]#si", "<div style=\"text-align:center\">\\1</div>", $input);
$input = preg_replace("#\[right\](.*?)\[/right\]#si", "<div style=\"text-align:right\">\\1</div>", $input);
}

if($use_html == 1) {
$input = preg_replace('/&lt;b&gt;(.*?)&lt;\/b&gt;/is', '<b>\\1</b>', $input);
$input = preg_replace('/&lt;i&gt;(.*?)&lt;\/i&gt;/is', '<i>\\1</i>', $input);
$input = preg_replace('/&lt;u&gt;(.*?)&lt;\/u&gt;/is', '<u>\\1</u>', $input);
$input = preg_replace('/&lt;s&gt;(.*?)&lt;\/s&gt;/is', '<s>\\1</s>', $input);
if($use_htmllinks == 1) {
$input = preg_replace("#&lt;a href=\"([a-z]+?://){1}([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\"&gt;(.*?)&lt;/a&gt#", "<a href=\"\\1\\2\" target=\"_blank\">\\3</a>", $input);
$input = preg_replace("#&lt;a mailto:\"{1}([a-z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+\(\)]+)\"&gt;(.*?)&lt;/a&gt#", "<a href=\"mailto:\\1\">\\2</a>", $input);
}
// if($use_htmlimg == 1)
// $message = eregi_replace("#&lt;img src=\"([^\\[]*?)\" /&gt;", "<img src=\"\\1\" border=\"0\" />", $input);
}

if($use_smilies == 1) {
$smiliesDir = "images/smilies/";
$smilies = array(':-)' => "happy.gif",
':-D' => "bigsmile.gif",
':-(' => "sad.gif",
':\'(' => "crying.gif",
'(A)' => "angle.gif",
'-_-' => "doh.gif",
':-O' => "oh.gif",
';-)' => "wink.gif");

foreach ($smilies AS $find => $character)
$input = str_replace(htmlentities($find), "<img src=\"". $smiliesDir . $character ."\" alt=\"\" />", $input);
}
return $input;
}

$message = replace($message);
$sig = addslashes(replace($signature[0]));

mordred
08-17-2004, 04:14 PM
Well, you haven't defined any of the $use_bb* variables in your function. In other words, the function does not know the values of these variables because they are out of the function's scope. This is a very practical feature (because each function can have the same named variables and yet don't interfer with one another), but in your case you have to make these (presumably global) variables visible. That's what the global keyword is for:



function replace($input) {
global $use_bb, $use_bblinks; // etc.

if($use_bb == 1) {
$input = preg_replace // etc.


Also, this kind of error should have raised an error message. If there wasn't one, please activate errors by putting error_reporting(E_ALL) at the top of your script.

Further, I just noticed that



if($use_bbimg == 1)
// $message = eregi_replace("\\[img\\]([^\\[]*?)\\[/img\\]", "<img src=\"\\1\" border=\"0\" alt=\"\" />", $input);
$input = preg_replace ("#\[quote=(.*?)\](.*?)\[/quote\]#si", "<br /><b style=\"padding-left:5px\">Quote \\1:</b><div class=\"quote_message\">\\2</div>", $input);


might have some side-effects you haven't intended, because the line which is commented out was originally effected by the if-condition, but now the "if" controls execution of the following preg_replace(). Put comments in front of the "if" as well.

Jero
08-17-2004, 04:38 PM
Whoa, it works, thanks you so much!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum