1

I am using preg_match_all to build the array for shortcodes and it works fine but it also returns arrays with empty values see here please

https://eval.in/141437

Using this match witch I am sure is casuing the extra empty arrays

#\[link(.*?)link\=\"(.*?)\"(.*?)text\=\"(.*?)\"\]#e

How can I clear those. I tried array_filter but it did not work.

Thank you!

2 Answers 2

2

() represent a capture group and will be represented in the $matches array even if it is empty.

Either get rid of the () around the groups that are returning empty like (.*?) to make it just .*? (because presumably you don't want those returned) or tell the engine not to capture that with (?: like (?:.*?).

#\[link.*?link\=\"(.*?)\".*?text\=\"(.*?)\"\]#e

Or if you do want those returned when they are not empty, then use + instead of *:

#\[link(.+?)link\=\"(.*?)\"(.+?)text\=\"(.*?)\"\]#e
Sign up to request clarification or add additional context in comments.

3 Comments

Nailed it! Both options work. Thank you. Must wait few min to accept.
OK, edited right as you accepted to include if you wanted those groups when not empty.
Thank you again. What I am trying to say with that expression is , capture the string inside "" and dont pay attention what is in between the tags like link---ANYTHING--text. Your explanation now makes sense since I was using () and captured everything
0

The array_filter() function should work if you use it like this:

$matches = array_filter($matches, function($item) { return trim($item[0]) && true; });

AbraCadaver's answer is the best way to go.

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.