Page 1 of 2
using substr and strpos
Posted: Thu Nov 12, 2009 1:16 pm
by thadson
I'm trying to pick up a number from my website using curl
The text I'm searching in has 2 possible looks (warlord or warlady), like these:
<h3>John - level 22 Warlord</h3>
<h3>Kitty - level 5 Warlady</h3>
<h3>Maryanne - level 16 Warlady</h3>
<h3>Mr. Drinkalot - level 9 Warlord</h3>
I need to pick up the level like this:
22
5
16
9
I tried this code but it only picks up one - either the Warlords or the Warladies (when I tweak it) but never both...
What am I doing wrong?
Code: Select all
$temp0=substr($connect,strpos($connect,'</h3>')-9,7);
if ($temp0 == 'Warlord') {
$temp=substr($connect,strpos($connect,'Warlord</h3>')-3,-2);
} else {
$temp=substr($connect,strpos($connect,'Warlady</h3>')-3,-2);
}
Also, is this enough of the code to help me, or do you need more of the surrounding code?
Re: using substr and strpos
Posted: Thu Nov 12, 2009 5:19 pm
by cpetercarter
Have a look at
this as an alternative approach.
Re: using substr and strpos
Posted: Thu Nov 12, 2009 8:43 pm
by thadson
Thanks for the help, but this approach does not seem to work for me.
I do not have a static text like "you have 29 messages" where the " you have ... messages" part is always the same.
The only static thing I have is that the text I need to search for is between the <h3> </h3> tags on that particular page but the name and title (ie: marianne warlady or john warlord) and the number in the level is always different.
Re: using substr and strpos
Posted: Thu Nov 12, 2009 10:41 pm
by superdezign
Use regex.
Choose either of these (untested):
Code: Select all
~<h3>.+level\s(\d+)\sWar(lord|lady).+</h3>~
Use
preg_match_all() to find all occurrences. The levels will exist in "\\1" (otherwise known in PHP as "$1").
Re: using substr and strpos
Posted: Fri Nov 13, 2009 12:59 am
by cpetercarter
I don't think that you looked very hard at the solution I pointed you to. It simply takes a string and removes everything which isn't a number. So it should work with any of the examples you give.
Re: using substr and strpos
Posted: Fri Nov 13, 2009 2:34 am
by thadson
this is the string I try to look at:
<h3>MyName - level 22 Warlord</h3>
This is how I used it the suggested code:
$ch=curl_init();
//I set some curl options here that are not relevant and already working
$connect=curl_exec($ch);
$response1=curl_getinfo( $ch );
$temp1 = substr($connect,strbetween($string, '<h3>', '</h3>'));
$temp2 = preg_replace("/[^0-9]/", '', $temp1); // ditch anything not a number
I should get 22
instead I get
31031131999884400102020123123595910000000002121000000121001001228308640787811112
10010012283086407878111101112300147751479961426909451001012311110123114223223141
50015001150015000406150017332173317330459150019253192519250452615007414741741029
11150015005150015000404150086168618610312150015007150015000404241341279962866721
44417022773584905661421060170161793103448286907251705037629350893709945318900023
01101023114411808311364573311117759920079200311200148200102001092402006310200316
00200399200464426024292124444552401514124764404455240131317525044552401717310304
45524021216168224444402215154401101101201204402112112242240440309309211229151411
51247644110727261201721731032118381411224213216168224309134131752501616720080919
13333323832516
Re: using substr and strpos
Posted: Fri Nov 13, 2009 3:33 am
by angelicodin
I'm vary new to the whole php thing but if the format is always the same could you use explode() and just reference the array like.....
Code: Select all
<?
$title = "John - level 22 Warlord";
$title_array = explode(" ", $title);
echo $title_array[3]; //output 22
?>
As I said I'm new so I don't know if this would work for you.
Re: using substr and strpos
Posted: Fri Nov 13, 2009 5:16 pm
by cpetercarter
I should get 22
instead I get
31031131999884400102020123123595910000000002121000000121001001228308640787811112
10010012283086407878111101112300147751479961426909451001012311110123114223223141
50015001150015000406150017332173317330459150019253192519250452615007414741741029
11150015005150015000404150086168618610312150015007150015000404241341279962866721
44417022773584905661421060170161793103448286907251705037629350893709945318900023
01101023114411808311364573311117759920079200311200148200102001092402006310200316
00200399200464426024292124444552401514124764404455240131317525044552401717310304
45524021216168224444402215154401101101201204402112112242240440309309211229151411
51247644110727261201721731032118381411224213216168224309134131752501616720080919
13333323832516
Check $temp1 - it isn't "MyName - level 22 Warlord".
Re: using substr and strpos
Posted: Fri Nov 13, 2009 8:56 pm
by superdezign
thadson wrote:This is how I used it the suggested code:
$ch=curl_init();
//I set some curl options here that are not relevant and already working
$connect=curl_exec($ch);
$response1=curl_getinfo( $ch );
$temp1 = substr($connect,strbetween($string, '<h3>', '</h3>'));
$temp2 = preg_replace("/[^0-9]/", '', $temp1); // ditch anything not a number
Don't use cURL unless you have to.
file_get_contents() should suffice.
Also, the reason that you are getting so many numbers is because the regex you are using is taking every non-number and removing it, leaving you with only numbers. Apparently, this "strbetween" function is greedy and selects the first '<h3>' and the very last '</h3>'. Speaking of, I just noticed that my regex was greedy. Here's the fixed versions:
Code: Select all
~<h3>.+?level\s(\d+)\sWar(lord|lady).+?</h3>~
or
Again, I suggest you use my regex solution. Use file_get_contents() to get the data, and use preg_match_all() with the provided regex in order to get an array of all the levels. You may have to make use of the flags to get the array you are after.
Re: using substr and strpos
Posted: Fri Nov 13, 2009 9:39 pm
by Mirge
thadson wrote:this is the string I try to look at:
<h3>MyName - level 22 Warlord</h3>
This is how I used it the suggested code:
$ch=curl_init();
//I set some curl options here that are not relevant and already working
$connect=curl_exec($ch);
$response1=curl_getinfo( $ch );
$temp1 = substr($connect,strbetween($string, '<h3>', '</h3>'));
$temp2 = preg_replace("/[^0-9]/", '', $temp1); // ditch anything not a number
I should get 22
instead I get
31031131999884400102020123123595910000000002121000000121001001228308640787811112
10010012283086407878111101112300147751479961426909451001012311110123114223223141
50015001150015000406150017332173317330459150019253192519250452615007414741741029
11150015005150015000404150086168618610312150015007150015000404241341279962866721
44417022773584905661421060170161793103448286907251705037629350893709945318900023
01101023114411808311364573311117759920079200311200148200102001092402006310200316
00200399200464426024292124444552401514124764404455240131317525044552401717310304
45524021216168224444402215154401101101201204402112112242240440309309211229151411
51247644110727261201721731032118381411224213216168224309134131752501616720080919
13333323832516
L-M-A-O... I know this isn't constructive or helpful.. but damn, thank you for the chuckle!

Re: using substr and strpos
Posted: Sun Nov 15, 2009 12:20 am
by thadson
Thank you for everyone that tried to help...
I have tried all the solutions you have sent me and this is what actually have worked for me...
Now, please understand, that I have started learning this only a few days ago, so I'm in no way a professional, and I'm sure my code has a lot to improve but here it is anyway:
Code: Select all
$temp=substr($connect,strpos($connect,'Warlord</h3>')-3,-2);
$temp=substr($temp,0,strpos($temp,'<'));
$temp=(int)$temp;
if ($temp < 1){
$temp=substr($connect,strpos($connect,'Warlady</h3>')-3,-2);
$temp=substr($temp,0,strpos($temp,'<'));
$temp=(int)$temp;
}
It works like a charm...
Thank you all again for helping me...
Re: using substr and strpos
Posted: Sun Nov 15, 2009 12:48 am
by thadson
Oh, by the way,
there is only one <h3> </h3> statement in every web page on the whole page. Only one!
It should go like this in my limited imagination:
1) look in mydatabase for id & charactername
2) open connection to webpage
3) Go to first character's page ( like http:\\mypage.com\index.php?m=characterpage&user=1)
4) search for <h3>[charactername] - level 22 Warlord</h3>
5) grab 22
6) go to 2nd character's page ( like http:\\mypage.com\index.php?m=characterpage&user=2)
7) search for <h3>[charactername] - level 5 Warlady</h3>

grab 5
.
.
.
and so on until all specified character's level is grabbed.
100) close connection
101) Then write all into temp file,
102) Then update all character levels in database...
and then the next problem starts, but that is a whole new story

Re: using substr and strpos
Posted: Sun Nov 15, 2009 12:52 am
by thadson
Mirge wrote:thadson wrote:this is the string I try to look at:
<h3>MyName - level 22 Warlord</h3>
This is how I used it the suggested code:
$ch=curl_init();
//I set some curl options here that are not relevant and already working
$connect=curl_exec($ch);
$response1=curl_getinfo( $ch );
$temp1 = substr($connect,strbetween($string, '<h3>', '</h3>'));
$temp2 = preg_replace("/[^0-9]/", '', $temp1); // ditch anything not a number
I should get 22
instead I get
31031131999884400102020123123595910000000002121000000121001001228308640787811112
10010012283086407878111101112300147751479961426909451001012311110123114223223141
50015001150015000406150017332173317330459150019253192519250452615007414741741029
11150015005150015000404150086168618610312150015007150015000404241341279962866721
44417022773584905661421060170161793103448286907251705037629350893709945318900023
01101023114411808311364573311117759920079200311200148200102001092402006310200316
00200399200464426024292124444552401514124764404455240131317525044552401717310304
45524021216168224444402215154401101101201204402112112242240440309309211229151411
51247644110727261201721731032118381411224213216168224309134131752501616720080919
13333323832516
L-M-A-O... I know this isn't constructive or helpful.. but damn, thank you for the chuckle!

I'm happy I could make you laugh...
But, Yep, it is not constructive at all...
at least tell me what are you laughing about, considering that on the page I'm scanning there is only one
<h3> </h3> statement and the only number that it contains is like 22.
So, where the other numbers are coming from, I have no idea...
Re: using substr and strpos
Posted: Sun Nov 15, 2009 12:48 pm
by Mirge
thadson wrote:I'm happy I could make you laugh...
But, Yep, it is not constructive at all...
at least tell me what are you laughing about, considering that on the page I'm scanning there is only one
<h3> </h3> statement and the only number that it contains is like 22.
So, where the other numbers are coming from, I have no idea...
Was just scanning through the PHP Code section, ran into this... saw "should get 22, got 9327493249832749832984732" whatever. Wasn't laughing at YOU or the trouble you were having... was laughing at your response. It was pretty funny.
Re: using substr and strpos
Posted: Sun Nov 15, 2009 4:51 pm
by thadson
As I said, "I'm happy I could make you laugh..."
No sarcasm intended
I was just hoping for some insight, but making people laugh is good too...