Jump to content
Sign in to follow this  
Ustag

htmldom + substr()

Recommended Posts

Hi, I am scraping a game page in order to grab certain info about a player. What I scrape is the info under "Character Deaths" for example on this page: http://www.tibia.com...s&name=Brunkarn This code will print all killed players in April:

if((substr($td2->nodeValue, 0, 3) == "Apr") && (substr($td2->nextSibling->nodeValue, 0, 6) == Killed)){echo $player.': '.substr($td2->nodeValue, 0, 7).'<br>';}

It prints "Brunkarn: Apr 08". now if I replace this:

substr($td2->nodeValue, 0, 3) == "Apr")

with this:

substr($td2->nodeValue, 0, 7) == "Apr 08")

I get no result anymore, why? When I echo substr($td2->nodeValue, 0, 7) I get "Apr 08". Sorry if I post in wrong section, please move this.

Edited by Ustag

Share this post


Link to post
Share on other sites

Instead of 0, 7 it should be 0, 6 becausse there are only 6 characters there. You forgot to put quotation marks around the word "Killed"If you have error messages activated it would showsomething like this: Use of undefined constant Killed, assuming "Killed"

Share this post


Link to post
Share on other sites

Adding "Killed" and using substr($td2->nodeValue, 0, 6) == "Apr 08" did not work eather. And how come echo substr($td2->nodeValue, 0, 7) = "Apr 08" and echo substr($td2->nodeValue, 0, 6) = "Apr 0"? I just noticed in their source code it says "Apr& #160;08", what is "& #160;" (without the whitespace)?

Edited by Ustag

Share this post


Link to post
Share on other sites

There may be something like a space before the value. If you save that value to a variable you can use var_dump and it will show you the type and length of the value.

Share this post


Link to post
Share on other sites

var_dump(substr($td2->nodeValue, 0, 3)); = string(3) "Apr"var_dump(substr($td2->nodeValue, 0, 4)); = string(4) "Apr�"var_dump(substr($td2->nodeValue, 0, 5)); = string(5) "Apr "var_dump(substr($td2->nodeValue, 0, 6)); = string(6) "Apr 0"var_dump(substr($td2->nodeValue, 0, 7)); = string(7) "Apr 08" Does this tell me anything? ^^

Share this post


Link to post
Share on other sites

The character   is a non-breaking space. It's not the same as an ordinary space.

Share this post


Link to post
Share on other sites

Thank you all but my solution will be to break this down and add substr($td2->nodeValue, 0, 3) and substr($td2->nodeValue, 5, 2) in to variables that will give me "Apr" and "08". Thanks !

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...