1

I am putting "merge" fields in my text, ie:

::sub_menu::33::

I want to be able to find all occurrences of this pattern:

::sub_menu::(any number)::

Then I'll go on to extract the number and replace it (I don't need help with that bit thanks).

I've used preg_match_all before, but am not sure how to use it when I want to use a specific string as part of the match? Any help much apprciated.

0

3 Answers 3

1

I want to be able to find all occurrences of this pattern: ::sub_menu::(any number)::

You can use this regex:

/::sub_menu::\(\d+\)::/

To use it in preg_match_all use:

if ( preg_match_all('/::sub_menu::\((\d+)\)::/', $input, $match) )
    print_r($match[0]);
Sign up to request clarification or add additional context in comments.

3 Comments

I think he is asking how to use preg_match_all not how to construct regex.
Thanks! This worked: preg_match_all('/::sub_menu::(\d+)::/', $str_content, $matches);
Short and sweet, +1 :)
0

You can use:

preg_match("/::sub_menu::\((\d+)\)::/", $input_line, $output_array);

The $output_array for an input of ::sub_menu::(33):: will contain:

Array
(
    [0] => ::sub_menu::(33)::
    [1] => 33
)

1 Comment

Thanks for your comment, I didn't actually mean ( ) to be part of the pattern, so the first answer did work.
0

You can try this one:

$input = '::sub_menu::33::';

/* if you have one or more numbers,  use (+) quantifiers*/ 
if ( preg_match('/::sub_menu::[0-9]+::/', $input, $match) )


/* if you want to put min/max quantifiers min by 2 and max by 10*/
if ( preg_match('/::sub_menu::[0-9]{2,10}::/', $input, $match) ){

    print_r($match);

}

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.