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);
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.
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
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);
That's giving me an error
I altered it somewhatWarning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'O-', to be a valid callback
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.
bluewalrus (06-09-2010)
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.
Bookmarks