"; From the line ab...">

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
source share
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
source

You can use code

 <?php $page="<img><img><img><img>"; preg_match_all("/<img>/",$page,$m); echo count($m[0]); ?> 

Or is it an alternative

 <?php $page="<img><img><img><img>"; $words = substr_count($page, '<img>'); print_r($words); ?> 
+3
source

All Articles