The number of samples per line
I am trying to get the length of images in a string.
My line
$page="<img><img><img><img>"; From the line above, I want to get the "4 images" output.
I tried the preg_match_all() and count() function, but it always returns "1 image".
$page="<img><img><img><img>"; preg_match_all("/<img>/",$page,$m); echo count($m); Is there any other way to find out how many images are in a row?
+6
2 answers
Preg_match_all returns a multidimensional array. Arrays are capture groups. Since you are not doing what you want to consider as index 0 $m (these are all the values โโfound). Therefore use:
echo count($m[0]); For demonstration, this is your $m .
Array ( [0] => Array ( [0] => <img> [1] => <img> [2] => <img> [3] => <img> ) ) count only takes into account index 0 , so you get 1 .
+11