1

I am a newbie in preg_match patterns, so I would be glad if someone could help me for next situation:

I need to replace those string:

[popup="about"]about me[/popup]

to

<a href="#PopupAbout"  data-plugin-options='{"type":"inline", preloader: false}'>about me</a>

I have tried with $pattern = '/\[popup="(.*?)"\](.*?)\[\/popup\]/'; but it does not give me expected result, it give duplicated results. And how can I replace it all in a simple way!

Regards!

2 Answers 2

1

How about:

$str = preg_replace('~\[popup="about"\](.+?)\[/popup\]'~, 
   "<a href=\"#PopupAbout\" data-plugin-options='{\"type\":\"inline\", preloader: false}'>$1</a>",
   $str);
Sign up to request clarification or add additional context in comments.

1 Comment

That looks like interesting, I will try this later at home
0

Try this:

preg_match("/\[popup="(.*)"\](.*?)\[\/popup\]/", $input_line, $output_array);

I get this result:

Array
(
    [0] => [popup="about"]about me[/popup]
    [1] => about
    [2] => about me
)

You can test it online here: http://www.phpliveregex.com/

1 Comment

You should use a reluctant quantifier .*? instead of .*. See when it fails

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.