Page 1 of 1

Problem with integers and and strings.

Posted: Mon Jan 09, 2006 10:34 pm
by ron_j_m
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

Posted: Mon Jan 09, 2006 10:46 pm
by feyd
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));

Posted: Mon Jan 09, 2006 11:21 pm
by ron_j_m
feyd wrote:To find out, use the following instead of var_dump(): htmlentities(var_export($pages,true));
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

Posted: Mon Jan 09, 2006 11:32 pm
by feyd
I forgot to add a call to echo() :oops:

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>";}

Posted: Mon Jan 09, 2006 11:41 pm
by ron_j_m
Thanks feyd. I was looking at the regex too. Still trying to get the hang of them.

Thanks
Ron