Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    211
    Thanks
    0
    Thanked 1 Time in 1 Post

    I want to edit two variables with preg_replace(), but how?

    This is the code:

    PHP 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);
        } 

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    De gustibus non est disputandum.

  • #3
    Regular Coder
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    211
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    De gustibus non est disputandum.

  • #5
    Regular Coder
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    211
    Thanks
    0
    Thanked 1 Time in 1 Post
    Uhm, could you please give me a little example of how it should look like? And I'm sorry for not being so clear.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure. It's not complete, but shows the general picture:

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

    $message replace($message);
    $sig replace($sig); 
    De gustibus non est disputandum.

  • #7
    Regular Coder
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    211
    Thanks
    0
    Thanked 1 Time in 1 Post
    Sweet! I'll try it.

  • #8
    Regular Coder
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    211
    Thanks
    0
    Thanked 1 Time in 1 Post
    I can get it to work. This is what I did:

    PHP Code:
        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])); 

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    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

    PHP Code:
    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.
    De gustibus non est disputandum.

  • #10
    Regular Coder
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    211
    Thanks
    0
    Thanked 1 Time in 1 Post
    Whoa, it works, thanks you so much!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •