Can't get this number via regular expressions

I’m having this weird issue where after I located a number using regular expressions, I can’t convert it to an integer without it reading as 0.


//this code in php
//do next row
    $team_reb=$items2[5]->children($row+1)->children(5)->innertext;
//row is an integer, items2 is an array gotten from a find() call

//gives me this in html
<span class="nbaGIScrTot">team rebs:</span> 12

I give the html because I don’t know if the tags are messing things up.


//this in php
 preg_match('/team rebs:(.+)/',$team_reb,$matches,PREG_OFFSET_CAPTURE);
        print_r($matches);
    $team_reb=$matches[1][0];
echo gettype($team_reb).$team_reb."<br>";
//gives me this in html
Array
(
    [0] => Array
        (
            [0] => team rebs:</span> 12
            [1] => 26
        )

    [1] => Array
        (
            [0] => </span> 12
            [1] => 36
        )

)
string</span> 12

I’ve tried a bunch of different regular expressions, including stuff with (\d\d) inside to try and get the number, but for whatever reason it doesn’t find the number. I noticed the </span> stuff as well, and when I added that in before the (.+) part, it ended up giving me like 5-17 or something which is a completely different statistic on the table. If I try and convert the variable to an int before echoing it, it returns as 0 (and says the type is an integer with no warnings). This regular expression stuff can be confusing sometimes.

I’m open for ideas.

Provided that there will always only be one number within the string, you can use the pattern (\\d+) which will match any number 1 or more times.

Here’s the code I used to test it:


$string = '<span class="nbaGIScrTot">team rebs:</span> 12';
$pattern = "/(\\d+)/";

preg_match($pattern, $string, $matches);
var_dump($matches);

The output:


array
  0 => string '12' (length=2)
  1 => string '12' (length=2)

I would change the pattern to only look at the end of the string to cover any case which may have a digit in the class or who knows what.


$pattern = "/(\\d+)$/";

So you understand why you were getting zero, what you got as a match was “</span> 12” (as you saw) which in PHP is zero if converted to an integer because the first character is not a number. PHP does not give any error when converting like that. It’s how the language works.

Well…

It worked. I could’ve sworn I tried something like that. I know about \d. I probably did something slightly different and that’s why it didn’t work and I didn’t catch the mistake.

Thanks guys for seeing through what I thought was a complex situation and knowing the answer was really simple.