Roy5 Roy5 - 5 months ago 18
PHP Question

Regular expression clean results

I am trying to extract the words from this string with PHP :

$string= '\'banana\', "orange", "apple"';


Using this pattern :

/([\'"])(.*?)\1/


But it gives me this results :

array(3) {
[0] array(3) {
[0] "'banana'"
[1] ""orange""
[2] ""apple""
}
[1] array(3) {
[0] "'"
[1] ""
[2] ""
}
[2] array(3) {
[0] "banana"
[1] "orange"
[2] "apple"
}
}


Is these a way I can clean it up to just :

array(3) {
[0] "banana"
[1] "orange"
[2] "apple"
}


Thanks for your help.

Answer

With regex, you can just extract word characters using the \w matcher. Put it together with a Global flag, and I believe you'll get what you want :)

See this example