Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You can use the following

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

See demo

share|improve this answer
 
I was having a hard time getting the value from the array. Your regex works 100%. Thanks! –  user2997491 Nov 15 '13 at 19:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.