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
  1. #1
    New Coder
    Join Date
    Jul 2003
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post

    Searching a string, then running a function once a result is found?

    Hi

    I am building a small attachments system and am wanting to search a string for all instances of [attach] and then run a function once this is found. I know I can do something like this:

    Code:
    $str = preg_replace("'\[b\](.*?)\[/b\]'", '<b>\\1</b>', $str);
    which will replace text with <b>text</b>, but what I want to happen is when [b] for example is found, i can run a function instead of just replacing text. Can anyone give me any suggestions on how to do this.

    Thanks

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    D0u$h!t3 k4?
    Posts
    512
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Use preg_match instead? It takes the same parameters, except the replacement parameter:
    PHP Code:
    if (preg_match("/\[b\](.*?)\[\/b\]/i"$str))
        
    makeBold($str); 
    Something along those lines?

    Edit: I made it a case insensitive match since it could be [ B] or [ b].
    Last edited by rpgfan3233; 11-16-2007 at 07:58 PM. Reason: Missing a \ for my /, I think
    PHP Code:
    $hello file_get_contents('hello.txt'); echo $hello
    hello

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    I'm always amused when I see code that reads right, but does absolutely nothing useful.

    What you need is preg_replace_callback.

  • #4
    New Coder
    Join Date
    Jul 2003
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by aedrin View Post
    I'm always amused when I see code that reads right, but does absolutely nothing useful.

    What you need is preg_replace_callback.
    Excellent, this is just what I was looking for

  • #5
    New Coder
    Join Date
    Jul 2003
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Just another quick question if you dont mind Say I had this:

    Code:
    $str = "This is the text";
    $str = preg_replace_callback("'\[color=\"?(.*?)\"?](.*?)\[/color\]'", "doParse", $str);
    
    function doParse($matches) {
    	echo $matches[1];
    }
    which works fine and outputs 990000. What I want to do is when [attach=2]somefile.jpg[/attach] is found, use code similar to the above to run a function and then replace [attach=2]somefile.jpg[/attach] with the results from the function. Can this be done also?

    Many thanks so far.

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    It's a replace and a callback in one.

    What you return as the result of the function will be put into the string where the match was found.

    So yes, that is possible using that setup.

    You might want to make your setup more dynamic though.

    PHP Code:
    $pattern '/\[(.*?)\](.*?)\[/(.*?)\]/'
    So your doParse function just looks at the first match, then use a switch statement to determine what to do.

  • Users who have thanked aedrin for this post:

    Immulsifier (11-17-2007)

  • #7
    New Coder
    Join Date
    Jul 2003
    Posts
    21
    Thanks
    4
    Thanked 1 Time in 1 Post
    Thanks very much, I understand how it works now and have got it working just how I need it


  •  

    Posting Permissions

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