Skip to content
Advertisement

Acessing values from preg_match_all in PHP 7.4

I am trying to access the values from the results of preg_match_all. Below is my coding:

$strNbrOfPages = preg_match_all($strPattern,$strWorkDetails,$matches,PREG_OFFSET_CAPTURE);

Below is the results of ensuring there are values:

Array ( [0] => Array ( [0] => Array ( [0] => PAGE [1] => 465 ) [1] => Array ( [0] => PAGE [1] => 5400 ) [2] => Array ( [0] => PAGE [1] => 10434 ) [3] => Array ( [0] => PAGE [1] => 15684 ) [4] => Array ( [0] => PAGE [1] => 20775 ) ) )

This my attempt to access the page location, i.e. 465,5400 etc.. I have tried various ways and (3 indices and 4 indices).I always end up with an error message – Array to string conversion error. What a I doing wrong??

echo '<BR>FROM $MATCHES: ' . $matches[0],[0],[1];
echo '<BR>FROM $MATCHES: ' . $matches[0],[1],[1];

Advertisement

Answer

You need to remove the commas from your notation:

echo '<BR>FROM $MATCHES: ' . $matches[0][0][1];
echo '<BR>FROM $MATCHES: ' . $matches[0][1][1];
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement