2

I am trying to parse out images from a wordpress post with get_the_content().

It returns a bunch of html and multiple lightbox shortcodes:

<ul>
    <li>testing1</li>
    <li>egfgf</li>
</li>
<p>Here!</p>
[lightbox link="http://www.test.com/photo1.jpg" width="150" align="none" title="photo 1" frame="true" icon="image"]
[lightbox link="http://www.test.com/photo2.jpg" width="150" align="none" title="photo 2" frame="true" icon="image"]
[lightbox link="http://www.test.com/photo5.jpg" width="150" align="none" title="photo 5" frame="true" icon="image"]

The html is not always like above and can be any html variant. My question is how can I use a regex pattern to get the link value from all the lightbox shortcodes?

Desired output:

Array
(
[0] => Array
    (
        [0] => http://www.test.com/photo1.jpg
        [1] => http://www.test.com/photo2.jpg
        [2] => http://www.test.com/photo5.jpg
    )
)

Patterns I've tried using:

preg_match_all('/(?<![^"])\S+\.[^"]+/', $text, $matches);
print_r($matches);

This works on just lightbox text but when I add the html it doesnt work.

Why does my regex work on this site http://regex101.com/r/eE6fU9 but not in php?

1 Answer 1

1

You can use the following

preg_match_all('/\[lightbox[^\]]*link="([^"]*)"[^\]]*\]/i', $text, $matches);
print_r($matches[1]);

See demo

Sign up to request clarification or add additional context in comments.

1 Comment

I was having a hard time getting the value from the array. Your regex works 100%. Thanks!

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.