Problem matching group inside another group

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
aye
Forum Commoner
Posts: 25
Joined: Wed Apr 11, 2007 9:02 am

Problem matching group inside another group

Post by aye »

hello,
i have an url which contains variable names and i want to parse it, so that these variable names are replaced with real --variables (hope you get what i mean). the problem comes when the variable inside the url is actually an array. It seems you can't match this array and then print out its value by a variable variable. Example:

Code: Select all

$url = "http://example.com/%test['field1']['feild2']['field3']%";
preg_match_all("/%(.+?)%/", $url, $matches, PREG_PATTERN_ORDER);
foreach($matches[1] as $match)
    $url = preg_replace("/%" . preg_quote($match) . "%/", $$match, $url);
 
Won't work ($$match doesn't seem to have any value).

So I thought, let's retreive the name of the array and the name of every field. Then maybe oyu could 're-create' the array somehow. But I can't get the regex to work. This is the current expression:

Code: Select all

/%([a-zA-Z|0-9]+?)(\['([a-zA-Z|0-9]+?)'\])*%/
Which gives the output:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => %test['field1']['feild2']['field3']%
        )
 
    [1] => Array
        (
            [0] => test
        )
 
    [2] => Array
        (
            [0] => ['field3']
        )
 
    [3] => Array
        (
            [0] => field3
        )
 
)
and to clarify: the output I would like it to be is rather something like the following:

Code: Select all

Array
(
    [0] => Array
        (
            [0] => %test['field1']['feild2']['field3']%
        )
 
    [1] => Array
        (
            [0] => test
        )
 
    [2] => Array
        (
            [0] => [b]field1[/b]
        )
 
    [3] => Array
        (
            [0] => [b]field2[/b]
        )
 
    [4] => Array
        (
            [0] => field3
        )
 
)
Any help with getting this to work would be great.
thanks!
dhiraj
Forum Newbie
Posts: 12
Joined: Wed Apr 23, 2008 12:23 am

Re: Problem matching group inside another group

Post by dhiraj »

i suggest u to use the while loop and print the values

:)
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Problem matching group inside another group

Post by prometheuzz »

How about this approach?

Code: Select all

<?php
$url = "http://example.com/%test['field1']['feild2']['field3']%";
if(preg_match('/^[^%]+%([^%]+).*$/', $url, $match)) {
    $array = preg_split('/[\'\[\]]+/', $match[1], -1, PREG_SPLIT_NO_EMPTY);
    echo print_r($array) . "\n";
}
/* output:
    Array
    (
        [0] => test
        [1] => field1
        [2] => feild2
        [3] => field3
    )
*/
?>
aye
Forum Commoner
Posts: 25
Joined: Wed Apr 11, 2007 9:02 am

Re: Problem matching group inside another group

Post by aye »

Thanks for your help guys,
ended up doing a variant of prometheuzz' suggestion. This is the end-results:

Code: Select all

preg_match_all('/^[^%]+%([^%]+).*$/', $url, $match);
foreach($match[1] as $val)
{
    $array = preg_split('/[\'\[\]]+/', $val, -1, PREG_SPLIT_NO_EMPTY);
    for($i = 0; $i < sizeof($array); $i++)
    {
        if($i == 0) $replace = $$array[$i];
        else $replace = $replace[$array[$i]];
    }
            
    $url = preg_replace('/%' . preg_quote($val) . '%/', $replace, $url);
}
        
exit($url);
Surprised it worked, but it did! :)
Post Reply