我有一个名为“load.php”的页面,它在每个页面的顶部调用。它有一些不同的preg_replace()函数和strtolower()函数,它影响页面末尾的$ text1变量。 (此更改在加载页面时完成,而不是插入到数据库中) 我想在strtolower()之前或之后添加一个final函数,以从strtolower()中排除URL的href属性。我该怎么办呢?感谢。
答案 0 :(得分:0)
让我试试:
//search for links with href
$links = preg_match_all('/href="(?P<link>[^"]*?)"/i',$text1, $matches);
if(count($matches['link'])>0){
// explode non links pieces of code
$blocks = preg_split('/href="(?P<link>[^"]*?)"/i',$text1);
// for assurance
// non-links pieces should be equal a links plus one
if(count($matches['link']) == (count($blocks)-1))
{
// to lower non-link pieces
$blocks = array_map("strtolower", $blocks);
$size = count($matches['link']);
for($i=0;$i<$size;$i++){
//putting together the link again without change a case
$blocks[$i] .= 'href="'.$matches['link'][$i].'"';
}
$text1 = join("",$blocks);
}
} else {
$text1 = strtolower($text1);
}
祝你好运:)
答案 1 :(得分:0)
这里有一个较短的版本:
function strtolowerExceptLinks($text) {
$search = '(\b[a-zA-Z0-9]+://[^( |\>\n)]+\b)';
preg_match_all($search, $text, $matches);
$urls = array_unique($matches[0]);
$text = mb_strtolower($text);
if (is_array($urls)) {
foreach ($urls as $url) {
$text = str_replace(mb_strtolower($url), $url, $text);
}
}
return $text;
}