php
  • token
  • strtok
  • 2011-09-15 52 views 1 likes 
    1

    我想在文本中找到某些詞/字符串,如鏈接。我從php.bet那裏得到了一段代碼,但它也從<a href="http://www.domain.com/index.php" title="Home">go to homepage</a>中刪除了標籤的開始和結束。你能幫助解決這個問題嗎?PHP令牌替換html實體

    下面是一段代碼:

    <?php 
    
    $str_in = '<p>Hi there worm! You have a disease!</p><a href="http://www.domain.com/index.php" title="Home">go to homepage</a>'; 
    $replaces=  array(
           'worm' => 'http://www.domain.com/index.php/worm.html', 
           'disease' => 'http://www.domain.com/index.php/disease.html' 
           ); 
    
    function addLinks($str_in, $replaces) 
    { 
        $str_out = ''; 
        $tok = strtok($str_in, '<>'); 
        $must_replace = (substr($str_in, 0, 1) !== '<'); 
        while ($tok !== false) { 
        if ($must_replace) { 
         foreach ($replaces as $tag => $href) { 
         if (preg_match('/\b' . $tag . '\b/i', $tok)) { 
          $tok = preg_replace(
               '/\b(' . $tag . ')\b/i', 
               '<a title="' . $tag . '" href="' . $href . '">\1</a>', 
               $tok, 
               1); 
          unset($replaces[$tag]); 
         } 
         } 
        } else { 
         $tok = "<$tok>"; 
        } 
        $str_out .= $tok; 
        $tok = strtok('<>'); 
        $must_replace = !$must_replace; 
        } 
        return $str_out; 
    } 
    
    echo addLinks($str_in, $replaces); 
    

    結果是:

    您好蟲!你有一種疾病!

    A HREF = 「http://www.domain.com/index.php」 標題= 「主頁」/一個

    的 「蠕蟲」 和 「疾病」 字變換成鏈接等所需,但其餘...

    非常感謝!

    +1

    +1對於一個深思熟慮的問題,顯示研究工作量。 – Herbert

    回答

    1

    這對功能應該做你想要的東西沒有解決HTML與正則表達式str_replace解決的問題。

    function process($node, $replaceRules) 
    { 
        if($node->hasChildNodes()) { 
         $nodes = array(); 
         foreach ($node->childNodes as $childNode) { 
          $nodes[] = $childNode; 
         } 
         foreach ($nodes as $childNode) { 
          if ($childNode instanceof DOMText) { 
           $text = preg_replace(
            array_keys($replaceRules), 
            array_values($replaceRules), 
            $childNode->wholeText); 
           $node->replaceChild(new DOMText($text),$childNode); 
          } 
          else { 
           process($childNode, $replaceRules); 
          } 
         } 
        } 
    } 
    
    function addLinks($str_in, $replaces) 
    { 
        $replaceRules = array();  
        foreach($replaces as $k=>$v) { 
         $k = '/\b(' . $k . ')\b/i'; 
         $v = '<a href="' . $v . '">$1</a>'; 
         $replaceRules[$k] = $v; 
        } 
    
        $doc = new DOMDocument; 
        $doc->loadHTML($str_in); 
        process($doc->documentElement, $replaceRules); 
        return html_entity_decode($doc->saveHTML()); 
    } 
    

    注: 沒有必要擔心,如果不支持HTML代碼結構良好(如你的例子);然而,產出將會很好地結構化。

    信貸,這是由於’ S: 遞歸process()功能,完成大部分實際工作,從照片直接盧卡斯·拉林斯基’的回答到How to replace text in HTML來。 addLinks()函數只是一個適合您的問題的用例。

    +0

    你是男人!太感謝了!!! –

    0

    不知道爲什麼你有那麼大建設,當是這樣的:

    $str_out = preg_replace('/(' . preg_quote(implode('|', array_keys($replaces))) . ')/', $replaces[$1], $str_in); 
    

    將完成同樣的事情。當然,使用正則表達式來處理HTML是一個hazardous process。你應該使用一些xpath的DOM來更可靠地做到這一點。

    +0

    在這種情況下,什麼是$替換[$ 1]?我在問這個,因爲它會拋出一個錯誤。謝謝! –

    +0

    你的代碼似乎不起作用。有沒有人知道如何避免在這種情況下弄亂HTML實體? 再次感謝! –

    相關問題