SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2013
    Location
    Houston
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.



    Code:
    //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.

    Code:
    //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.

  2. #2
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,270
    Mentioned
    33 Post(s)
    Tagged
    5 Thread(s)
    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:
    PHP Code:
    $string '<span class="nbaGIScrTot">team rebs:</span> 12';
    $pattern "/(\d+)/";

    preg_match($pattern$string$matches);
    var_dump($matches); 
    The output:
    PHP Code:
    array
      
    => string '12' (length=2)
      
    => string '12' (length=2

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    PHP Code:
    $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.
    - Robert

  4. #4
    SitePoint Member
    Join Date
    Jul 2013
    Location
    Houston
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •