user2639545
user2639545

Reputation: 55

PHP regex to parse wordpress shortcode attribute

I am trying to regex all lightbox wordpress shortcodes and receive their 'link' attribute.

Example:

[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"]

There can be any number of these shortcodes but I need to get all their link attributes:

http://www.test.com/photo1.jpg

My pattern I am working with:

$pattern = '/\[(\[?)(lightbox)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)/';

Upvotes: 0

Views: 1158

Answers (1)

Machavity
Machavity

Reputation: 31624

I think you're overthinking it a bit

preg_match_all('/\[lightbox link="(.*?)".*\]/i', $str, $matches);

Upvotes: 1

Related Questions