Asheeown Posted March 23, 2009 Share Posted March 23, 2009 I'm using a site to find a value I need. When I pull the HTML for the site I put it into the variable $Info. I need to find "Requires level xx" in all that text. There are no unique HTML tags around it, I need the number after it, 'xx'. Almost always it's a two digit number, could possibly be a one. Anyone know how I could do this? Quote Link to comment https://forums.phpfreaks.com/topic/150651-solved-finding-a-certain-string-in-a-bunch-of-text/ Share on other sites More sharing options...
Floydian Posted March 23, 2009 Share Posted March 23, 2009 $strpos = strpos('Requires level', $info); // I might have the args in the wrong order, but that's the idea $strpos = $strpos + strlen('Requires level') + 1; // +1 for the space after it $level = substr($info, $strpos, 2); Hope that helps... Quote Link to comment https://forums.phpfreaks.com/topic/150651-solved-finding-a-certain-string-in-a-bunch-of-text/#findComment-791440 Share on other sites More sharing options...
.josh Posted March 23, 2009 Share Posted March 23, 2009 preg_match('~Requires level \d+~i',$Info,$match); print_r($match); Quote Link to comment https://forums.phpfreaks.com/topic/150651-solved-finding-a-certain-string-in-a-bunch-of-text/#findComment-791441 Share on other sites More sharing options...
redarrow Posted March 23, 2009 Share Posted March 23, 2009 <?php $word="hi the i am red arrow Requires level 29 love php"; if(preg_match_all("/(Requires level)(.?)[0-9]{0,2}/i",$word,$matched)){ echo $matched[0][0]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150651-solved-finding-a-certain-string-in-a-bunch-of-text/#findComment-791443 Share on other sites More sharing options...
Asheeown Posted March 23, 2009 Author Share Posted March 23, 2009 Floy, yours worked perfectly, sorry guys, didn't get to try yours out before I did Floydian's. You did have the args mixed on the first function, here is my working script: $strpos = strpos($Info, 'Requires level'); $strpos = $strpos + strlen('Requires level') + 1; $level = substr($Info, $strpos, 2); Quote Link to comment https://forums.phpfreaks.com/topic/150651-solved-finding-a-certain-string-in-a-bunch-of-text/#findComment-791446 Share on other sites More sharing options...
redarrow Posted March 23, 2009 Share Posted March 23, 2009 Fort this was a regex issue finding words from a web page. i differ to agree example. ((am i missing some think here? <?php $Info="hi the i am red arrow Requires level 29 love php"; $strpos = strpos($Info, 'Requires level'); $strpos = $strpos + strlen('Requires level') + 1; $level = substr($Info, $strpos, 2); echo $level; ?> result 29 <?php $word="hi the i am red arrow Requires level 29 love php"; if(preg_match_all("/(Requires level)(.?)[0-9]{0,2}/i",$word,$matched)){ echo $matched[0][0]; } ?> result. Requires level 29 Quote Link to comment https://forums.phpfreaks.com/topic/150651-solved-finding-a-certain-string-in-a-bunch-of-text/#findComment-791453 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.