0

I want to remove this code

[gallery columns="2" link="none" ids="69,50" orderby="rand"]

from a string. The [gallery .....] is always the same and the ... changes. How can I do this? Then I want to save the ids value in another string. What is the most elegant way? Thank you.

1 Answer 1

1

The following Regex will match the whole string to remove it, and will contain the ids in an extracted group:

/\[gallery .*?ids="(.*?)".*?\]/

This code should work:

$string ='test i am test lol [gallery columns="2" link="none" ids="69,50" orderby="rand"] me also!';
preg_match('/\[gallery .*?ids="(.*?)".*?\]/', $string, $matches);

echo $matches[1]; // prints the ids

$string = preg_replace('/\[gallery .*?ids="(.*?)".*?\]/', '', $string);

echo $string; // prints the cleaned string
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect! Thank you very much! I did not find a good manual to understand this stuff: /[gallery .*?ids="(.*?)".*?]/. Can you tell me how it is called so I can search for it?

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.