PHP Extract Values From One String Based on a Pattern Defined in Another

Posted by ironkeith on Stack Overflow See other posts from Stack Overflow or by ironkeith
Published on 2010-04-26T04:13:46Z Indexed on 2010/04/26 4:23 UTC
Read the original article Hit count: 186

Filed under:
|
|
|

I have two strings:

$first = '/this/is/a/string';
$second = '/this/:param1/a/:param2';

And I'm trying to get this:

$params = array('param1' => 'is', 'param2' => 'string');

But getting from point a to b is proving more than my tired brain can handle at the moment.

Anything starting with a ':' in the second string defines a variable name/position. There can be any number of variables in $second which need to be extracted from $first. Segments are separated by a '/'.

Thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about php5