TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > PHP

Notices

Execute function in preg_replace_callback with matches and an extra param?

PHP


Reply
 
Thread Tools Display Modes
Old 12-10-2007, 04:35 AM   #1
Marc Bauer
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


  Reply With Quote
Old 20-11-2007, 10:42 AM   #2
Csaba Gabor
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

  Reply With Quote
Old 20-11-2007, 10:42 AM   #3
Csaba Gabor
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

  Reply With Quote
Reply

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?

All times are GMT +5.5. The time now is 02:48 PM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional