amedhussaini Posted April 14, 2010 Share Posted April 14, 2010 Trying to search a $string (just an HTML page grabbed with curl after stripping the tags) for questions. I thought to look to return content between "What" and "?" inclusive, etc... but no luck so far. Valid questions I'm looking for: "What is the point?" "what is the point?" I'd like the function to return all results into an array if possible Cheers, Amed Quote Link to comment Share on other sites More sharing options...
newbtophp Posted April 14, 2010 Share Posted April 14, 2010 You can try: <?php $data= <<<eof What is the point? eof; //the i modifier is for case insensitive (so can match in between What and what) preg_match("~What(.+)\?~Ui", $data, $a); //the array of matched results... print_r($a); ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 newbtophp, I think that will match more than what he wants. Also, maybe preg_match_all would be more useful. What if you have this - "I wonder what he would do after this fails. Do you think think he'll jump off a building?" Try your regexp against that string. Or this one - "Geez, whatever man. Just stop yelling at me. Did I do something that offended you?" I would try: preg_match_all("#\W\s(?<questions>What [^!?\.]+\?)#i", $str, $results); Quote Link to comment Share on other sites More sharing options...
amedhussaini Posted April 14, 2010 Author Share Posted April 14, 2010 Thanks! This works like a charm. Ran it on a few test cases... 99% were accurate, came up with a few of these: : What is RSS? ) What is the WTO? ) Any ideas on further refining it? For now, I will study your regex expression and decipher how you came up with it... this place really is a wealth of knowledge newbtophp, I think that will match more than what he wants. Also, maybe preg_match_all would be more useful. What if you have this - "I wonder what he would do after this fails. Do you think think he'll jump off a building?" Try your regexp against that string. Or this one - "Geez, whatever man. Just stop yelling at me. Did I do something that offended you?" I would try: preg_match_all("#\W\s(?<questions>What [^!?\.]+\?)#i", $str, $results); Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 14, 2010 Share Posted April 14, 2010 Try just looping through $results['questions']. foreach ($results['questions'] as $key => $value) { echo $value; } Quote Link to comment Share on other sites More sharing options...
amedhussaini Posted April 15, 2010 Author Share Posted April 15, 2010 err, that was silly.. that was part of my print_r output that i mistook for something else. thx again fellas. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.