Find all occurrences of a Substring in a String

Dear all,

Could someone help me fox fixing this function?

When I execute the script I receive an error message:

Warning:  count(): Parameter must be an array or an object that implements Countable in

<?php
function tag_contents($string, $start, $end){
   foreach (explode($start, $string) as $key => $value) {
       if(strpos($value, $end) !== TRUE){
            $result[] = substr($value, 0, strpos($value, $end));
//			 $result[] ="<strong>".$start."</strong>" . substr($value, 0, strpos($value, $end)) . "<strong>".$end."</strong>";
     	  	   
	   [b]if(count($result[$key]) > 150 || str_word_count($result[$key]) > 15)   [/b]
	   
	    {
	    
        unset($result[$key]);
					}
		   }
   
   }
   return $result;	 
}

?>

Open in new window



I want limit the output to substring with less than 15 words and 150 characters.

Thanks

Bob
BOB KUSPEAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
I think this:
if(count($result[$key])
should be
if(strlen($result[$key])
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
Also this:
if(strpos($value, $end) !== TRUE){

...probably does not work exactly like you want it to work. strpos will NEVER return a boolean True value. It either returns a number indicating the position of the substring, which can be 0 or greater, OR if the substring is not found, it returns a FALSE boolean. So that code will literally run every single time because it will either be false or a number, and neither of which will ever be a boolean of true.

So if you did this:
if(strpos($value, $end) !== FALSE){
...then that would mean "Run the following code if $end is found ANYWHERE inside $value"

So if $end was NOT found inside $value, then it would skip over that section of code.
0
gr8gonzoConsultantCommented:
In fact, since you're checking it twice, just assign it to a variable and test it.

$endPos = strpos($value, $end);
if(endPos !== FALSE){
            $result[] = substr($value, 0, $endPos);

Open in new window

0
BOB KUSPEAuthor Commented:
Many Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.