Reputation: 48
I've just released the first version of a WordPress plugin I wrote, and I've received a report that at least one of the people using my plugin is receiving an execution timeout error citing this block of code:
function getNumericAttributeFromHTML($htmlElement, $attribute){
$attrStartPos = stripos($htmlElement, $attribute) + strlen($attribute);
$strOffset = 0;
$searchWithin = substr($htmlElement, $attrStartPos);
while(!(is_numeric($searchWithin[$strOffset]))){
$strOffset++;
}
$attrStartPos += $strOffset;
$strOffset = 0;
$searchWithin = substr($htmlElement, $attrStartPos);
while((is_numeric($searchWithin[$strOffset]))){
$strOffset++;
}
return substr($htmlElement, $attrStartPos, $strOffset);
}
This function is called twice per image on the page. Am I being crazy inefficient, or is it possible their host is just terrible?
Thanks in advance for any help you can provide.
Upvotes: 3
Views: 302
Reputation: 57670
while(!(is_numeric($searchWithin[$strOffset])))
will run infinte times if $searchWithin
has no numeric character!
Note the problem might be somewhere else.
To pinpoint the actual problem I suggest you get the reproducible steps from the bug and use a profiler to profile the code. You'll surely find where the problem is.
Upvotes: 2
Reputation: 2244
If its causing timeout, well you have a extra large or infinite loop. You have 2 loops here, check if they are infinite.
If this is not the problem, increase the maximum execution time by adding this at start of your script:
ini_set('max_execution_time', 36000); //3600 seconds = 1 hour
Upvotes: 0
Reputation: 78006
You have an infinite while
loop. You're just incrementing the value of $strOffset
without changing the value of $searchWithin[$strOffset]
so if it's not numeric, it never will be and will get stuck looping forever.
Upvotes: 1