regex - PHP Match String Pattern and Get Variable -


i looked reference this question , this question not figure out need do.

what trying is:

say, have 2 strings:

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

now want check if pattern matches. if match, want value of country set usa.

$country = "usa"; 

i want work in cases like:

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

maybe integers well. match every braces , provide variable value. (and, yes better performance if possible)

i cannot work around since new regular expresssions. in advance.

it give results 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 parse alphabets, can extend characters in range per need.

update:

you can 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] $key => $matches){         $$matches = $matches1[1][$key];     }     echo $country;      echo '<br>';     echo $city;     echo '<br>';     echo $page; } 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -