0

I get a string in PHP and I need to look up the values of the IDs, can be 1, how can be 100 IDs, how to do? I tried something like this:

$text = 'Name1 <span id="indicado-b">John</span> Name2 <span id="indicado-c">Mike</span>';
preg_match_all('/<span id="indicado-(.*)"/si',$text, $match);
print_r($match);

The value that I need to receive is b and c, which are the ID values, but it is not returning this, I get:

Array ( [0] => Array ( [0] => John Name2 Array ( [0] => b">John Name2
caiocafardo
  • 196
  • 1
  • 13

1 Answers1

0

Try this code:

$text = 'Name1 <span id="indicado-b">John</span> Name2 <span id="indicado-c">Mike</span>';
$separate_array = explode('<span id="indicado-', $text);

$span_ids = array();
foreach ($separate_array as $row) {
    $test_span = explode('"', $row);
    if (count($test_span) > 1 ) {
        $span_ids[] = $test_span[0];
    }
}
print_r($span_ids);

It returns the result as you expected using explode in PHP.

Sami Ahmed Siddiqui
  • 2,328
  • 1
  • 16
  • 29