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 7 of 7

Thread: Regex Problem

  1. #1
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Regex Problem

    Hello,

    I'm having some trouble with preg_replace. Here is the line... (I have tried both of these)
    PHP Code:
    preg_replace"<\{(.+?)\}>"tab("$1"), $text );
    preg_replace"<\{(\w+)\}>"tab("$1"), $text ); 
    It is supposed to find {anythinginhere}, save the "anythinginhere" in $1 and then replace it with text returned from the tab() function.

    Unfortunately, the text is not replaced, and I cannot for the life of me figure out why.. I have used a regex editor and it works fine but when it is run in the script it does not replace anything.

    Thanks in advance!

    FuZion
    Last edited by FuZion; 07-09-2007 at 03:33 PM.

  • #2
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    PHP Code:
    function mTab($fNum)
    {
        
    $total $fNum 4;
        return 
    $total;
    }

    $text "{anythinginhere}";
    echo 
    preg_replace('/\{(.*?)\}/i'"{" mTab(5) . "}"$text); 
    Created my own function to test with as i don't have a copy of your tab(), seems too work for me.

  • #3
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Well, in your script the mTab function accepts any number.. however my script uses a backreference ( $1 ) for the function.

    The tab function has an ASSOC array in it and all it does is pull the array value from $1.

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    your regexp is missing delimiters (the /s in Serex's code), and you probably want to be using preg_replace_callback() - http://php.net/preg-replace-callback if you want to be using the backref
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #5
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    The <>s in my code also act as delimiters, I have tried it with the slashes also. The thing is, I have seen scripts before in a bb code parser that uses a backreference to $1, $2, ect. But for some reason this does not work.

  • #6
    Regular Coder
    Join Date
    Oct 2004
    Posts
    366
    Thanks
    0
    Thanked 18 Times in 18 Posts
    The "e" modifier.

    PHP Code:
    <?php

    $test_string 
    '{one} stuff {two}

    {three} stuff {four}'
    ;

    function 
    tab($key)
    {
      
    $tab = array('one' => 'abc''two' => 'def''three' => 'ghi');
      if(isset(
    $tab[$key])) return $tab[$key];
      else return 
    $key;
    }

    echo 
    preg_replace('#\{(.*?)\}#e''tab("\1")'$test_string);
    Last edited by schleppel; 07-10-2007 at 01:16 AM.

  • #7
    Regular Coder
    Join Date
    May 2006
    Posts
    152
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok that works, thanks a lot! But now there is another problem.. if I run this once with one query, and then run another, it does not replace it.. and I cannot figure out why, I know for sure that the array. Why would it not work the second time?


  •  

    Posting Permissions

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