Question

I have this string:

Hello, world

My goal is to use the preg_match function from PHP to end up with:

array(
      [0] => Hello
      [1] => world
) 

This is just an example. I know that I can use explode, or any other method, but it is mandatory for me to use preg_match to match a sub-string followed by a comma, followed by another sub-string while ignoring the comma in the matched result.

Thank you.

Was it helpful?

Solution

Here is a way to do:

$str = 'Hello, world';
preg_match('/^([^,]+),([^,]+)$/', $str, $matches);

I suggest you to have a look at the doc.

OTHER TIPS

I don't know much about php, but you can do simply:

/\w+/

That will match hello and world

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top