2

I am trying to extract numbers from a mix string.

<?php
$string = "c <a data-player-id=\"5528\" href=\"/players/5528-ga-name--5406546\" target=\"_self\">GA Name</a> b <a data-player-id=\"8992842\" href=\"/players/8992842-chandran-win--123345\" target=\"_self\">C Win</a>";

//preg_match_all('!\d+!', $string, $matches);
//preg_match_all('/data-player-id=\"(\d+)/', $string, $matches);
preg_match_all('/\/players\/(\d+)/', $string, $matches);
print_r($matches);

?>

But it results into 2 arrays:

Array
(
[0] => Array
    (
        [0] => /players/5528
        [1] => /players/8992842
    )

[1] => Array
    (
        [0] => 5528
        [1] => 8992842
    )

)

I want to capture numbers like 5528 and 8992842. The code below did not work.

 /*
 $zero = $matches[0];
 $one = $matches[1];
 $two = $matches[2];

 echo $zero;
 echo $one;
 echo $two;
 */

Edit : Any idea why it returns into 2 arrays ? Is it possible to count the items in array[1] ?

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
Dil Dilshan
  • 361
  • 1
  • 4
  • 17
  • `echo $matches[1][0];` and `echo $matches[1][1];` – Hanky Panky Dec 22 '16 at 06:10
  • As already mentioned, the reason is that you use a [capturing group](http://www.regular-expressions.info/brackets.html) for extraction. `$matches[1]` always contains matches of the first parenthesized group (capturing group), `$matches[0]` contains the full pattern matches. It's possible to do it without capturing group by use of [`\K` which resets](http://www.rexegg.com/regex-php.html#K) beginning of the reported match, thus returns only the numbers in `$matches[0]` [see this demo at regex101](https://www.regex101.com/r/FV3kke/1). To count them, just use use `count($matches[0])`. – bobble bubble Dec 22 '16 at 11:47

2 Answers2

2

Try to something like this.

<?php
$string = "c <a data-player-id=\"5528\" href=\"/players/5528-ga-name--5406546\" target=\"_self\">GA Name</a> b <a data-player-id=\"8992842\" href=\"/players/8992842-chandran-win--123345\" target=\"_self\">C Win</a>";

preg_match_all('!\d+!', $string, $matches);

$arr = array_unique($matches[0]);

// For Count items...
$count = count($arr);
echo $count;

foreach($arr as $match)
{
    echo $match."<br />";
}
?>

Output

5528

5406546

8992842

123345

Pravin Vavadiya
  • 3,195
  • 1
  • 17
  • 34
  • any idea why it returns into 2 array and also is it possible to count the items in array[1] ? – Dil Dilshan Dec 22 '16 at 07:02
  • $match[0] contains array of strings that matched full pattern, and $match[1] contains array of strings enclosed by tag. Use count($arr) function for count the items – Pravin Vavadiya Dec 22 '16 at 07:19
1

You can use foreach loop print all found value in $matches[1]

Try

$string = "c <a data-player-id=\"5528\" href=\"/players/5528-ga-name--5406546\" target=\"_self\">GA Name</a> b <a data-player-id=\"8992842\" href=\"/players/8992842-chandran-win--123345\" target=\"_self\">C Win</a>";


preg_match_all('/\/players\/(\d+)/', $string, $matches);
//print_r($matches);


foreach($matches[1] as $match)
{
    echo $match."<br />";
}

output

UPDATE 1

Yes, you can count the elements found in $matches[1] by using count()

$total_matches = count($matches[1]);
echo $total_matches;
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50