1

I have this string

$url = offer?offer_id={{offer_category_id}}&item{{offer_title}}

Is there a way how I can create a php array with the text inside the {{ }} thus resulting an array similar to

$array[0] = 'offer_category_id'
$array[1] = 'offer_title'

This is what I have but its not working as wanted

preg_match("/{{([^\"]*)\}}/", $url , $cols);

3 Answers 3

1

This code will give your values:

$str = 'offer?offer_id={{offer_category_id}}&item{{offer_title}}';
if ( preg_match_all('~{\s*{([^}]*)}~i', $str, $m) )
   print_r ( $m[1] );

OUTPUT:

Array
(
    [0] => offer_category_id
    [1] => offer_title
)
Sign up to request clarification or add additional context in comments.

Comments

1

Use preg_match_all:

preg_match_all("/{{([^\"}]+)\}}/", $url , $cols);

2 Comments

Great Man ! Any idea why it outputted: Array ( [0] => Array ( [0] => {{offer_category_id}} [1] => {{offer_title}} ) [1] => Array ( [0] => offer_category_id [1] => offer_title ) )
What do you mean? $cols[1] will contain an array you need
1

i think it should be (you might need to play with the flag to get the desired order):

preg_match_all("/{{([^}]+)\}}/", $url , $cols, PREG_SET_ORDER);

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.