[PHP] Re: preg_replace_callback
tJey wrote: Hi. I have problem with preg_replace_callback. It seems that my pattern is bad, but I can't find any error. Pattern : \[\s*((\d|\w|_)+)\s*\] this pattern is intended to find strings like "[field]", "[ fi12_eld]"... but every time I get warning message "Warning: preg_replace_callback(): Delimiter must not be alphanumeric or backslash in test.php on line 100" $pattern= "%\[[\s\w]+\]%"; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace_callback
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] (Rafael Fernandes) wrote: > First, sorry for my elementary english... > I need a function made for PHP3 that works like preg_replace_callback... Before preg_replace_callback() was introduced, preg_replace() used to have another an "f" modifier which had similar functionality. I'm not sure whether that modifier was available back in PHP3, but here's the relevant quote about "f" from an old set of PCRE Syntax docs: > F > If this modifier is set, preg_replace() treats the replacement parameter as a > function name that should be called to provide the replacement string. The > function is passed an array of matched elements in the subject string. NOTE: > this modifier cannot be used along with /e modifier; and only preg_replace() > recognizes this modifier. If I recall correctly (which I may not), an example would look something like this: $output=preg_replace("/(\w+)/f","'***' . strtolower($1) . '***'",$input); -- CC -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: preg_replace_callback()
[EMAIL PROTECTED] (Richard Lynch) writes: > A "callback" is when you execute a function, and you provide to it a name of > *another* function, which it will call on some data in the middle of its > task. > > It's a very handy way to provide extreme flexibility in functional > languages. > > For example: > > function my_array_walk($array, $function){ > while (list($k, $v) = each($array)){ > # Here's the magic that implements a 'callback' > $function($k, $v); > } > } > > function your_echo($key, $value){ > echo "$key $value\n"; > } > > $foo = array('a'=>1, 'b'=>2, 'c'=>3); > > my_array_walk($foo, 'your_echo'); > > This rather silly example will "walk" the array and call 'your_echo' on each > key/value pair. > > Dunno exactly how preg_ uses it though... It calls the callback function for each match, passing it an array. The matching string is replaced by whatever the callback function returns. Here's a fairly pointless example of names and email addresses being replaced by mailto links with a bit of extra processing on their name... -robin mailto:{$match[2]}\";>{$match[1]}"; } $testString =<< "Tommy Atkins" <[EMAIL PROTECTED]> "Davey Jones" <[EMAIL PROTECTED]> EOS; $outputString = preg_replace_callback('/"(.+?)"\s*<(.+?)>/', 'mailto', $testString); print " $outputString "; ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Re: preg_replace_callback()
A "callback" is when you execute a function, and you provide to it a name of *another* function, which it will call on some data in the middle of its task. It's a very handy way to provide extreme flexibility in functional languages. For example: function my_array_walk($array, $function){ while (list($k, $v) = each($array)){ # Here's the magic that implements a 'callback' $function($k, $v); } } function your_echo($key, $value){ echo "$key $value\n"; } $foo = array('a'=>1, 'b'=>2, 'c'=>3); my_array_walk($foo, 'your_echo'); This rather silly example will "walk" the array and call 'your_echo' on each key/value pair. Dunno exactly how preg_ uses it though... -- WARNING [EMAIL PROTECTED] address is an endangered species -- Use [EMAIL PROTECTED] Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm Volunteer a little time: http://chatmusic.com/volunteer.htm - Original Message - From: Arash Dejkam <[EMAIL PROTECTED]> Newsgroups: php.general To: <[EMAIL PROTECTED]> Sent: Saturday, September 08, 2001 7:35 PM Subject: preg_replace_callback() > Hi, > > What is a callback in preg_replace_callback(...) ? > how can I use it ? can anybody give me an example ? > > Thanks, > Arash > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]