php - 为什么这个 PHP 会导致执行超时?

标签 php wordpress

我刚刚发布了我编写的 WordPress 插件的第一个版本,并且我收到了一份报告,其中至少有一个使用我的插件的人正在收到引用此代码块的执行超时错误:

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);
}

页面上的每个图像都会调用此函数两次。我的效率低得离谱吗,还是他们的主人很糟糕?

在此先感谢您提供的任何帮助。

最佳答案

while(!(is_numeric($searchWithin[$strOffset]))) 如果 $searchWithin 没有数字字符,将运行无限次 !

请注意,问题可能出在其他地方。

为了查明实际问题,我建议您从错误中获取可重现的步骤并使用 profiler分析代码。您一定会找到问题所在。

关于php - 为什么这个 PHP 会导致执行超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12961546/

相关文章:

php - 从网站代码中删除未使用的 CSS

php - 如何获取登录用户的用户名

javascript - 使用 jquery 更改后复选框未提交

php - 使用 MySQL 中的临时表

php - 检查是否从 iOS 设备访问 PHP 页面

html - 如何禁用值

php - 如何按分类术语从自定义 WordPress MySQL 查询中排除结果

php - 样式化 WordPress 自定义导航菜单

wordpress - Woocommerce 自定义产品文本

php - post_excerpt 而不是 post_content,在循环之外