我的一对功能存在问题,这些功能允许用户使用 [link] 和 [/link] 集在没有事先 html 知识的情况下创建链接。一切都很好,直到我开始测试链接前后的空格等意外情况。现在它在我的服务器上超时,设置为 30 秒的最大处理时间。
用户总共可以输入 5000 个字符,这是这些功能必须浏览的健康页面。但是,当用户正确键入代码(不带空格)时,它不会对服务器征税,并且我的页面会在几分之一秒内弹出,就像它应该的那样。如果缺少 [link] [/link] 之一,则 while 循环退出 - 没有任何反应。只有在链接中键入这样的空格时:
[链接] www.google.com[/link] --或-- [链接]www.google.com [/link]
两者都使服务器超时并出现此错误(路径稍作修改):
致命错误:/home/example/thiscode.php 第 18 行的最大执行时间超过 30 秒
[link]www.google.com[/link] 不会超时,也不会:[link]www. google.com[/link] 根本不会被识别为有效的 html 链接,但会正确放置锚标记。
有什么想法吗?代码哈!
function my_link($snip)
{
// Locate '[link]'
$pos = stripos($snip, '[link]');
// Strips off everything before [link]
$str = substr($snip, $pos);
// Removes [link] from $str
$str = substr($str, 6);
// Trim off any accidental whitespace BEFORE the link
$str = ltrim($str);
// Locate the end delimiter '[/link]'
$pos_2 = stripos($str, '[/link]');
// Cut off everything after and including '[/link]'
$str = substr($str, 0, $pos_2);
// Trim any accidental whitespace AFTER the link
$str = rtrim($str);
// Construct valid HTML link using content given
if(strpos($str, 'http://') === FALSE) {
$link = '<a href="http://'.$str.'">'.$str.'</a>';
}else{
$link = '<a href="'.$str.'">'.$str.'</a>';
}
// Replace the old [link]content[/link] with our newly constructed link
$new_input = str_replace('[link]'.$str.'[/link]', $link, $snip);
return $new_input;
}
function making_links($input)
{
// Loop through $input as many times as [link] & [/link] pairs occur
while(strpos($input, '[link]') && strpos($input, '[/link]')) {
$input = my_link($input);
}
return $input;
}
提前感谢任何可以帮助我的人!