1

I'm trying to grab the string between :" and " in an array in PHP using preg_match_all.

eg:

"I am a string"("string"):"Need this string", "I am a string"("string"):"Need this string", etc, etc

I had the following but it returns no results or errors and works in the regex builders I've tried.

/\"\:"(.*?)\"/ and #/\"\:"(.*?)\"/# 

2 Answers 2

1

The expression can be made much simpler like this:

if (preg_match_all('/:"(.*?)"/', $str, $matches)) {
    // $matches[1] will contain all the strings you want
}
Sign up to request clarification or add additional context in comments.

2 Comments

isn't correct.. expression matches strings like ':"string","adfadf"' instead of ':"string"'
@vlcekmi3 I'm using a non-greedy pattern by adding a question mark. Alternatively I could have used the U modifier.
0

try this pattern:

#:"([^"]*)"#

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.