Jump to content

[SOLVED] Finding a certain string in a bunch of text


Recommended Posts

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?

$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...

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);

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.