3

I looked up some reference like this question and this question but could not figure out what I need to do.

What I am trying to do is:

Say, I have two strings:

$str1 = "link/usa";
$str2 = "link/{country}";

Now I want to check if this the pattern matches. If they match, I want the value of country to be set usa.

$country = "usa";

I also want it to work in cases like:

$str1 = "link/usa/texas";
$str2 = "link/{country}/{place}";

Maybe integers as well. Like match every braces and provide the variable with value. (And, yes better performance if possible)

I cannot get a work around since I am very new to regular expresssions. Thanks in advance.

2 Answers 2

6

It will give you results as expected

$str1 = "link/usa";
$str2 = "link/{country}";

if(preg_match('~link/([a-z]+)~i', $str1, $matches1) && preg_match('~link/{([a-z]+)}~i', $str2, $matches2)){
    $$matches2[1] = $matches1[1];
    echo $country;
}

Note: Above code will just parse alphabets, you can extend characters in range as per need.

UPDATE:

You can also do it using explode, see example below:

$val1 = explode('/', $str1);
$val2 = explode('/', $str2);
${rtrim(ltrim($val2[1],'{'), '}')} = $val1[1];
echo $country;

UPDATE 2

$str1 = "link/usa/texas/2/";
$str2 = "/link/{country}/{city}/{page}";

if(preg_match_all('~/([a-z0-9]+)~i', $str1, $matches1) && preg_match_all('~{([a-z]+)}~i', $str2, $matches2)){

    foreach($matches2[1] as $key => $matches){
        $$matches = $matches1[1][$key];
    }
    echo $country; 
    echo '<br>';
    echo $city;
    echo '<br>';
    echo $page;
}
Sign up to request clarification or add additional context in comments.

7 Comments

How about the performance? Can I depend on this looping maybe a hundred times?
hundred times, no big deal, try it
@tika you can also do it using explode, if you would like to avoid regex, see my updated answer
That works for the set example, but for /link/usa/texas/2/ type of links? Maybe the pattern be: /link/{country}/{city}/{page}. Please see that I have updated the question. I do not want the preg_match(~link) to be hard-coded.
${rtrim(ltrim($val2[1],'{'), '}')} => ${trim($val2, '{}')} is more simple.
|
2

I don't see the point to use the key as variable name when you can built an associative array that will be probably more handy to use later and that avoids to write ugly dynamic variable names ${the_name_of_the_var_${of_my_var_${of_your_var}}}:

$str1 = "link/usa/texas";
$str2 = "link/{country}/{place}";

function combine($pattern, $values) {
    $keys = array_map(function ($i) { return trim($i, '{}'); },
                      explode('/', $pattern));
    $values = explode('/', $values);

    if (array_shift($keys) == array_shift($values) && count($keys) &&
        count($keys) == count($values))
        return array_combine($keys, $values);
    else throw new Exception ("invalid format");
}

print_r(combine($str2, $str1));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.