BOB KUSPE
asked on
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
I want limit the output to substring with less than 15 words and 150 characters.
Thanks
Bob
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;
}
?>
I want limit the output to substring with less than 15 words and 150 characters.
Thanks
Bob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
ASKER
Many Thanks
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.