【问题标题】:Lowercase Text Except URL's小写文本(URL 除外)
【发布时间】:2011-07-06 11:36:56
【问题描述】:

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

【问题讨论】:

  • 你能提供代码摘录吗?目前尚不清楚您要做什么。
  • 是的,我们希望查看来源(仅几个重要的行)以了解问题。我认为您可以使用正则表达式检查您的文本是否为 URL,然后使用您的 strtolower 函数。

标签: php hyperlink lowercase


【解决方案1】:

让我试试:

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

祝你好运:)

【讨论】:

    【解决方案2】:

    这里有一个较短的版本:

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

    【讨论】:

      猜你喜欢
      • 2015-02-04
      • 2014-12-07
      • 2017-03-05
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2014-07-23
      • 2013-01-01
      • 2018-07-15
      相关资源
      最近更新 更多