1

I need to search in wordpress posts for posts that have the gallery included in the content with the photos inserted from the media library and not attached to the posts (this is the wordpress part, if you don't get it, that's fine, this is just to explain what I'm doing.

Anyway, the $subject looks like this:

"some text or no text at all [gallery order="DESC" orderby="title" link="file" include="1089,1090,1091,1099,1105,1109,1113"]some text or no text at all"

What I need is the list of numbers from the include. Note that the other arguments (like order, link, etc) are not mandatory.

I tried myself but I just can't make it work, this is what I've done:

$pattern = '/^.\[gallery.include="(?)".\]$/';
$subject = '[gallery order="DESC" orderby="title" link="file" include="1089,1090,1091,1099,1105,1109,1113"]';
var_dump(preg_match($pattern, $subject, $matches));
print_r($matches);

Prints:

int(0) Array ( )
1

3 Answers 3

3

Try with:

$pattern = '/^\[gallery.*?\sinclude="([^"]*)".*\]$/';

A single . in a regex only matches one character. .* matches any number of characters, greedily (i.e. it goes as far as it can). .*? does the same thing, but non-greedily (i.e. it stops as soon as the next part gets a match more or less).

The capture part is [^"]* which is an inverted character class which means "anything but a " repeated zero or more times".

And you need to escape [ and ] because they are the start/end markers for character classes.

If you want to be able to have text before or after the [thing], remove the anchors (first ^, and last $).

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

2 Comments

@GabrielCol: I've changed it a bit, it would have matched galleryinclude=.... Added a mandatory space before include.
I saw, Thanks a lot for your help, is just what I was looking for
1

OK, some assumptions, because this really needs a proper parser, rather than regex:

  • there are no ] characters in your gallery tag.
  • there's no occurrence of the include= in your gallery tag

Both of these are true inside quoted sections as much as outside them.

This regex should work:

$pattern = '/\[gallery[^\]]* include="([\d,]*)"/';

$matches[1] will now contain a comma-separated string.

$values = explode(',', $matches[1]);

$values will now be an array of the numbers.

2 Comments

Thanks, works but i prefer Mat's code because i have the full sentence in the [0] position of the array and the numbers in the [1]. I need it like this since i will replace the IDs and update the post after.
@GabrielCol I'm glad to help, and I'm not the slightest bit offended that you've chosen Mat's answer, but I'm afraid I don't remotely understand that explanation!
0

Try with:

$pattern = '/^[gallery.?include="([^"])".*]$/';

That probably won't work because he said there might be text before and after the gallery tags.

The ^ and $ flags contained respectivelly at the start and end of your string means that this tag must be the only thing in a line.

Use:

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

and it should work.

Comments

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.