Blue
Blue

Reputation: 259

Counting matches from preg_match_all

I've been searching for similar question but haven't found no one with similar problem or possible solution. I have preg_match_all function which finds me 5 matches and returns them in array. How can I count strings that are in array ?

preg_match_all("/\)\">(.*?)<\/a>/s",$value,$counter);
here is var_dump(counter[1]):
array (size=1)


0 => string 'word1' (length=..)

array (size=1)
  0 => string 'word2' (length=..)

array (size=1)
  0 => string 'word3' (length=..)

array (size=1)
  0 => string 'word4' (length=..)

array (size=1)
  0 => string 'word5' (length=..)

As you seen it returns me 5 matches, but i cant print that number neither with array_count_values, count()... I've tried imploding then to get strings but count() wont work either.

    count($counter[1]) returns me ->
int 1

int 1

int 1

int 1

int 1

I need to get number of matches and I dont have any more ideas, so anything is very welcome.

Update: Here is var_dump($value);

string '<h2><a href="javascript:Organigram.toggle('organigram_section_29')">word1</a></h2><div class="show"><p class="nums"><span class="phone">03 839 11 00</span></p>' (length=170)

string '<h2><a href="javascript:Organigram.toggle('organigram_section_35')">word2</a></h2><div class="show"><p class="nums"><span class="phone">03 839 12 00</span><span class="fax">03 839 12 18</span><span class="email email-mod"><a title="[email protected]" href="mailto:[email protected]"><img alt='[email protected]' src='Txt2Image.ashx?AAARAAcASQAAAB0AEgAGADMAEwAGAB8AAAAMAF0ABwAAAFUAGQAGABcAEQBUAAAAAAAQAB8AEQANAFUAEgAGAB0AAABUADIABgAAABIAGABSAEIAQABSAAMADABSAFAARQAtAEcARQBeADYATwALABwAGAANAEMARABZAEMARQBdADIARQA=' /></a></s'... (length=938)`

string '<h2><a href="javascript:Organigram.toggle('organigram_section_51')">word3</a></h2><div class="show"><p class="nums"><span class="phone">03 839 12 12</span></p>' (length=162)

string '<h2><a href="javascript:Organigram.toggle('organigram_section_56')">word4</a></h2><div class="show"><p class="nums"><span class="phone">03 839 12 14</span></p>' (length=168)

string '<h2><a href="javascript:Organigram.toggle('organigram_section_61')">word5</a></h2><div class="show"><p class="nums"><span class="phone">03 839 61 28</span></p>' (length=162)

Upvotes: 1

Views: 6092

Answers (2)

Sundar
Sundar

Reputation: 4650

<?php 

/* function strlen1($val)
{
    return strlen($val);
} */

//get the each value count
$b = array_map('strlen', $counter);

print_r($b);

Upvotes: 0

Amir Bilal
Amir Bilal

Reputation: 457

preg_match_all itself returns the number of full pattern matches (which might be zero), or FALSE if an error occurred.

$number = preg_match_all("/\)\">(.*?)<\/a>/s",$value,$counter);

Upvotes: 7

Related Questions