Page 1 of 1

preg_replace_callback

Posted: Fri Apr 13, 2007 5:25 am
by sajith
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi ,

We are facing some problem with preg_replace_callback() . This below code is running perfectly in the php 5.2.1 on redhat linx but it is not running in the feeBSD server with php5.2.1. Please help me out  in this cae

Code: Select all

<?php

$exp = "size {width},{height}; autoresize width; type {type},{palette},{color:{bgcolor}}; padding {padding}; color {color:{bgcolor}}; fill; color {color:{color},{alpha}}; antialias {antialias}; font {font:{font},{size},{leading}}; string {text},0,0,{width},{height},{align},{valign};" ;
function dummy($mathes)
{
	return preg_replace('/([;])/', "\\\\\\\$1", $matches[0]);
}

$lines = preg_replace_callback(
			'/(?<!\\\)\\\'(?:\\\\\'|[^\'])*(?:(?<!\\\)\\\')|'.
			'(?<!\\\)\"(?:\\\\"|[^"])*(?:(?<!\\\)\")|'.
			'(?<=\{)(?:[^{}]*(?:\{[^{}]*\})*[^{}]*)*(?=\})/',
			"dummy",
			$exp
		);

var_dump($lines);
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Apr 13, 2007 7:59 am
by feyd
I'll have to assume it's your escaping as it appears non-standard.

Posted: Fri Apr 13, 2007 9:30 am
by sajith
I jus stuck up with the functon problem I found out that preg_replace_callbak() function has some problem with php5.2.1 version on the FreeBSD server. So I looking for a solution for this

Posted: Fri Apr 13, 2007 9:35 am
by feyd
What's the functionality supposed to be doing? There may be a simpler way of accomplishing it.

Posted: Mon Apr 16, 2007 1:59 am
by sajith
In this code the preg_replace_callback() function will call the dummy function replace the $exp string accroding to the matches . so the callback fucntion will call the dummy function 14 times. my intension is I have to replace some string we cannot replace the string at a time

Do you have any other idea to acomplish the task in another way.
please let me know

Posted: Mon Apr 16, 2007 6:16 am
by stereofrog
Please post an example of source string and expected output.

Posted: Tue Apr 17, 2007 1:58 am
by sajith
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

$matches[2][0] = preg_replace_callback(
            '/(?<!\\\)\\\'(?:\\\\\'|[^\'])*(?:'.'(?<!\\\)\\\')|'.
            '(?<!\\\)\"(?:\\\\"|[^"])*(?:'.'(?<!\\\)\")|'.
            '(?<=\{)(?:[^{}]*(?:\{[^{}]*\})*[^{}]*)*(?=\})/',
            create_function('$matches', 'return preg_replace(\'/([,])/\', "\\\\\\\$1", $matches[0]);'),
            $matches[2][0]
        );
This is the input and expeced out put . we have to call the fucntion iterativly and please see below the possible input and out puts

input: {width},{height}
output: {width},{height}

input: width
output: width

input: {type},{palette},{color:{bgcolor}}
output: {type},{palette},{color:{bgcolor}}

input: {padding}
output: {padding}

input: {color:{bgcolor}}
output: {color:{bgcolor}}

input: {color:{color},{alpha}}
output: {color:{color}\,{alpha}}

input: {antialias}
output: {antialias}

input: {font:{font},{size},{leading}}
output: {font:{font}\,{size}\,{leading}}

input: {text},0,0,{width},{height},{align},{valign}
output: {text},0,0,{width},{height},{align},{valign}

If you can give a alternative solution that could be a greate help for me


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Apr 17, 2007 7:22 am
by feyd
So at maximum you're dealing with one level of nesting or could there potentially be more?

Posted: Wed Apr 18, 2007 11:26 pm
by sajith
it will come more than one level