Results 1 to 7 of 7

Thread: PHP Function in Regular expression

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default PHP Function in Regular expression

    Is there a way to call a php function within a regular expression, for example preg_replace?

    This is what I currently have but doesn't work...

    PHP Code:
    $case_changed preg_replace('/<em>(.*) (.*)</em>/'ucwords($1) . $2$case_changed); 
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Since $1 is a special variable, I'm not surprised it doesn't work. I can't think of an easy way to do this (assuming what you have won't work).

    You could do it in several steps: use regex to mark and/or split at the places then replace as needed.
    I'd probably do this using arrays, similar to explode(), but using regex. I believe there's a "split by regex" function, then you can loop through the array, do the function, and implode it back together again...
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    PHP Code:
    function change_case($arr)
    {
        return 
    ucwords($arr[1]) . $arr[2];
    }

    $case_changed '<em>Hello World</em>';
    $case_changed preg_replace_callback('/<em>(.*) (.*)<\/em>/''change_case'$case_changed); 

  4. #4
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    That's giving me an error

    Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'O-', to be a valid callback
    I altered it somewhat

    PHP Code:
    function italic_word($arr) {
        return 
    ucwords($arr[1]) . $arr[2];
    }
    $case_changed preg_replace_callback('/<em>(.*) (.*)<\/em>/'italic_word($case_changed), $case_changed); 
    Corrections to my coding/thoughts welcome.

  5. #5
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Quote Originally Posted by bluewalrus View Post
    That's giving me an error



    I altered it somewhat

    PHP Code:
    function italic_word($arr) {
        return 
    ucwords($arr[1]) . $arr[2];
    }
    $case_changed preg_replace_callback('/<em>(.*) (.*)<\/em>/'italic_word($case_changed), $case_changed); 
    It should be:
    PHP Code:
    $case_changed preg_replace_callback('/<em>(.*) (.*)<\/em>/''italic_word'$case_changed

  6. The Following User Says Thank You to techietim For This Useful Post:

    bluewalrus (06-09-2010)

  7. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Oh, okay thanks.

    So the second parameter there is the function name and the third is the variable being passed in?
    Corrections to my coding/thoughts welcome.

  8. #7
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    Quote Originally Posted by bluewalrus View Post
    So the second parameter there is the function name and the third is the variable being passed in?
    php.net/preg_replace_callback

Bookmarks

Posting Permissions

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