PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
ron_j_m
Forum Commoner
Posts: 35 Joined: Wed Feb 02, 2005 8:56 pm
Post
by ron_j_m » Mon Jan 09, 2006 10:34 pm
I have a string that I need to be an integer so I can do some math with it.
Heres to code:
Code: Select all
if (preg_match("/<td width=\"63%\">[\d]+<\/td>/", $string, $pages)) {
echo "<br>page was found<br>";
print_r($pages); //Returns: Array ( [0] => 143 )
$pages = $pages[0];
echo $pages; //Returns: 143
var_dump($pages); //Returns: string(24) "143"
$div = 40;
var_dump($div); //Returns: int(40)
$numpages = $pages / $div;
var_dump($numpages); //Returns: int(0)
}
else{ echo no page was found<br>";}
I need $pages to be an integer so I can divide it by $div. Ive tried alot of things and cant get it to work.
settype($pages, "integer") only sets $pages to 0
Its got to be something simple I'm missing here.
Any Ideas?
Thanks
Ron
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Jan 09, 2006 10:46 pm
notice how the
var_dump() says string(24). That means there are 24 characters in the string, which it then prints out in quotes. These characters may or may not be visible, or if you are displaying this information on a web page, may contain HTML. I'm going to say it's definitely the latter culprit.
To find out, use the following instead of
var_dump() :
htmlentities(var_export($pages,true)) ;
ron_j_m
Forum Commoner
Posts: 35 Joined: Wed Feb 02, 2005 8:56 pm
Post
by ron_j_m » Mon Jan 09, 2006 11:21 pm
Thanks feyd but unfortunatly htmlentities(var_export($pages,true)) doesn't spit out a thing.
I stuck in this for loop to see whats in there:
Code: Select all
for ($i = 0; $i <= 24; $i++){
echo "<br>";
var_dump($pages[$i]);}
And the results are:
Code: Select all
string(1) "<"
string(1) "t"
string(1) "d"
string(1) " "
string(1) "w"
string(1) "i"
string(1) "d"
string(1) "t"
string(1) "h"
string(1) "="
string(1) """
string(1) "6"
string(1) "3"
string(1) "%"
string(1) """
string(1) ">"
string(1) "1"
string(1) "4"
string(1) "3"
string(1) "<"
string(1) "/"
string(1) "t"
string(1) "d"
string(1) ">"
string(0) ""
Witch is basicaly the regex im using.
So I guess im stuck here. Not quite sure where to go next?
Thanks
Ron
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Jan 09, 2006 11:32 pm
I forgot to add a call to echo()
Anyways, this will fix your issue:
Code: Select all
if (preg_match("/<td width=\"63%\">([\d]+)<\/td>/", $string, $pages)) {
echo "<br>page was found<br>";
echo '<pre>'.htmlentities(var_export($pages,true)).'</pre>';
$pages = $pages[1];
echo $pages; //Returns: 143
var_dump($pages); //Returns: string(24) "143"
$div = 40;
var_dump($div); //Returns: int(40)
$numpages = $pages / $div;
var_dump($numpages); //Returns: int(0)
}
else{ echo no page was found<br>";}
ron_j_m
Forum Commoner
Posts: 35 Joined: Wed Feb 02, 2005 8:56 pm
Post
by ron_j_m » Mon Jan 09, 2006 11:41 pm
Thanks feyd. I was looking at the regex too. Still trying to get the hang of them.
Thanks
Ron