Domanda

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.

È stato utile?

Soluzione

Here is a way to do:

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

I suggest you to have a look at the doc.

Altri suggerimenti

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

/\w+/

That will match hello and world

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top