Count words between two tags as foreach

2019-09-10 02:24发布

问题:

A post have some quotes. When a quote have more then X words, I have to display a error message. To count a string between two words I use this function / code:

function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}

$fullstring = '<div class="quotecontent">This is the message with a lot of text</div>';

$parsed = get_string_between($fullstring, 'content">', '</div>');

$parsed_clean = strip_tags($parsed);

echo str_word_count($parsed_clean);

Now I have the amount of words inside of the quote.

Problem:

When the message have more then 1 quote, the function count only the words from the first quote and not from the others. How is it possible to count the text from every quote and display a error message with the text:

"Please check your quotes. Minimum 1 Quote is longer then X Words".

This shows the code in action.

http://ideone.com/PQAYUm

This shows the problem.

http://ideone.com/8bpnsq

Thank you.