Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

[SOLVED] RegEx help - should be simple...

$
0
0
Hey, D.I.C.

I'm having a little more trouble with my RegEx, although I can't figure out what I'm doing wrong, regardless of much research.

Here's what's meant to happen:
My script is given a string (which starts with either (who|what|where|why|when) which would suggest that the string is a question. Then, I'm figuring out of it is past or present tense (is|was) then I want to get everything else as a whole string after than.

That proved difficult since I was using the ([\w]+) and that wouldn't do the trick. So I proceeded with this method, used array_slice to chop off the first two elements. Surely, this would be a success? No.

My RegEx is only matching the first three words, so when I use Who is the president of America? - my array of matches appears to be Array([0]=>"Who is the" [1]=>"who" [2]=>"is" [3]=>"the"). I've tried amending my code in many ways, shapes and forms to try and get some order by this script is running riot and not wanting to help me out at all.

Below is my function to break down the sentence/string - hopefully, someone will be able to give me some explanation.

function isQuestion($str) {
    $q = preg_match("/(who|what|where|why|when) (is|was) ([\w]+)/i", $str, $matches);
    return Array($q, $matches);
}



Many thanks in advanced, always appreciated.

~huzi

Solution (thanks to Dormilich)
function isQuestion($str) {
    $q = preg_match("/(who|what|where|why|when) (is|was) ([\w].+)/i", $str, $matches);
    return Array($q, $matches);
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>