![]() |
![]() |
|
|||||||
| Register | Forum Rules | Getting Started! - Guide | Blog | Videos | Gallery | Members List | Social Groups | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Guest
Posts: n/a
|
Execute function in preg_replace_callback with matches and an extra param?
Hi
i have an preg_replace_callback that matches some strings and i'd like to execute a sub function with two params. Only thing i need is to call the subfunction 'my_function' with the $matches and an addition $param. How can i add the second param to the preg_replace_callback line? function test_function($demo) { $example = "Some data"; $test = preg_replace_callback('my example regexp', 'my_function', $example); } function my_function($matches, $param = 0) { //do some stuff here if ($param) { // do something special } } Regards Marc |
|
|
|
#2 |
|
Guest
Posts: n/a
|
Re: Execute function in preg_replace_callback with matches and anextra param?
Poster asks how to pass an additional parameter to callback function in preg_replace_callback.
Instead of preg_replace_callback it may be feasible to use preg_replace. Here's an example showing parameter passing (watch the wrapping - 5 lines total): function xslashify($string, $safeChars = "0-9A-Za-z'\\\\0\"") { // escape all but the safe characters $string = str_replace(array("\\", "\x0", '"', "'"), array('\x5c', '\x00', '\x22', '\x27'), $string); return preg_replace ("/[^$safeChars]/e", "'\\x' . substr('00' . dechex(ord('$0')),-2)", $string); } To use poster's own setup as a model, the following will put an apostrophe after each vowel: $param = "\\'"; $test = preg_replace ('/[aeiou]/ie', "my_function(array('$0'), '$param')", "Example string"); function my_function ($aMatches, $param = "^") { return "$aMatches[0]$param"; } print $test; Another option is to have a global variable, and the callback could make use of that. Csaba Gabor from Vienna Marc Bauer wrote: > Hi > > i have an preg_replace_callback that matches some strings and i'd like to > execute a sub function with two params. Only thing i need is to call the > subfunction 'my_function' with the $matches and an addition $param. How can > i add the second param to the preg_replace_callback line? > > > function test_function($demo) { > $example = "Some data"; > $test = preg_replace_callback('my example regexp', 'my_function', > $example); > } > > function my_function($matches, $param = 0) { > //do some stuff here > if ($param) { > // do something special > } > } > > > Regards > Marc |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Re: Execute function in preg_replace_callback with matches and anextra param?
Poster asks how to pass an additional parameter to callback function in preg_replace_callback.
Instead of preg_replace_callback it may be feasible to use preg_replace. Here's an example showing parameter passing (watch the wrapping - 5 lines total): function xslashify($string, $safeChars = "0-9A-Za-z'\\\\0\"") { // escape all but the safe characters $string = str_replace(array("\\", "\x0", '"', "'"), array('\x5c', '\x00', '\x22', '\x27'), $string); return preg_replace ("/[^$safeChars]/e", "'\\x' . substr('00' . dechex(ord('$0')),-2)", $string); } To use poster's own setup as a model, the following will put an apostrophe after each vowel: $param = "\\'"; $test = preg_replace ('/[aeiou]/ie', "my_function(array('$0'), '$param')", "Example string"); function my_function ($aMatches, $param = "^") { return "$aMatches[0]$param"; } print $test; Another option is to have a global variable, and the callback could make use of that. Csaba Gabor from Vienna Marc Bauer wrote: > Hi > > i have an preg_replace_callback that matches some strings and i'd like to > execute a sub function with two params. Only thing i need is to call the > subfunction 'my_function' with the $matches and an addition $param. How can > i add the second param to the preg_replace_callback line? > > > function test_function($demo) { > $example = "Some data"; > $test = preg_replace_callback('my example regexp', 'my_function', > $example); > } > > function my_function($matches, $param = 0) { > //do some stuff here > if ($param) { > // do something special > } > } > > > Regards > Marc |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Apache1.3 can't execute cgi | swun2010@gmail.com | FreeBSD | 1 | 28-09-2007 01:30 PM |
| Execute OS command | Earl Lewis | Java | 7 | 15-09-2007 03:34 AM |
| Execute OS command | Earl Lewis | Java | 2 | 15-09-2007 03:12 AM |
| Execute OS command | Earl Lewis | Java | 0 | 15-09-2007 03:07 AM |
| Extra Memory: Which slots should I put the faster extra in? | Nathan Sokalski | Windows XP | 4 | 16-08-2007 08:05 PM |
< Home - Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |