1

Today, I have read a question on Stack Overflow, at here: https://stackoverflow.com/questions/32151810/how-to-parse-this-text-block-into-variables-by-php

I have tried to do it with this code:

preg_match("/([a-zA-Z_]+)/", "[first_text] = [second_text, third_text] : [forth_text, fifth_text]", $matches);

When I tested it, it didn't work correctly:

echo $matches[0];
echo $matches[1];
echo $matches[2];
echo $matches[3];
echo $matches[4];

Will print:

first_textfirst_text

What is the error in my own PHP regex code?

0

1 Answer 1

3

You need to use:

preg_match_all('/([a-zA-Z_]+)/', "[first_text] = [second_text, third_text] : [forth_text, fifth_text]", $matches);

preg_match_all will return all the matches using your regex whereas preg_match just gives you first match.

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

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.