Php 除URL以外的小写文本

Php 除URL以外的小写文本,php,hyperlink,lowercase,Php,Hyperlink,Lowercase,我有一个名为load.php的页面,它在每个页面的顶部被调用。它有一些不同的preg_replace函数,以及影响页面末尾$text1变量的strtolower函数。这些更改是在加载页面时完成的,而不是插入到数据库中 我想在strtolower之前或之后添加最后一个函数,以从strtolower中排除URL的href属性。我该怎么办?谢谢。让我试试: //search for links with href $links = preg_match_all('/href="(?P<link&

我有一个名为load.php的页面,它在每个页面的顶部被调用。它有一些不同的preg_replace函数,以及影响页面末尾$text1变量的strtolower函数。这些更改是在加载页面时完成的,而不是插入到数据库中 我想在strtolower之前或之后添加最后一个函数,以从strtolower中排除URL的href属性。我该怎么办?谢谢。

让我试试:

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

祝你好运:

这里有一个较短的版本:

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

你能提供代码摘录吗?不清楚你想做什么。是的,我们只想看到一些重要的信息来源来理解这个问题。我认为您可以使用regex检查文本是否为URL,然后使用strtolower函数。