2010-06-29 21 views
0

我有一个正常的表达式,通过HTML内容寻找一些曾经工作的关键字,但现在失败了,我不明白为什么。 (正则表达式来自this thread查找和替换在HTML正则表达式失败

$find = '/(?![^<]+>)(?<!\w)(' . preg_quote($t['label']) . ')\b/s'; 
$text = preg_replace_callback($find, 'replaceCallback', $text); 

function replaceCallback($match) { 
     if (is_array($match)) { 
      $htmlVersion = $match[1]; 
      $urlVersion = urlencode($htmlVersion); 
      return '<a class="tag" rel="tag-definition" title="Click to know more about ' . $htmlVersion . '" href="?tag=' . $urlVersion . '">' . $htmlVersion . '</a>'; 
     } 
     return $match; 
    } 

错误消息指向preg_replace_Callback呼叫,说:

Warning: preg_replace_callback() [function.preg-replace-callback]: Unknown modifier 't' in /frontend.functions.php on line 43 
+3

HTML是不是一个正规的语言,所以正则表达式可能不是最好的工具在这里。 – 2010-06-29 09:00:33

+4

您不应该使用正则表达式来解析html。看到这里:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Oded 2010-06-29 09:00:47

+2

它会永远停止? – Gordon 2010-06-29 09:08:16

回答

0

请注意:这是不是试图提供一个修复为正则表达式。它只是在这里展示它是多么困难(我敢说,不可能)创建一个能够成功解析HTML的正则表达式。即使结构良好的XHTML也会非常困难,但结构不良的HTML对于正则表达式来说是不可行的。

我同意100%使用正则表达式来尝试HTML解析是一个非常糟糕的主意。以下代码使用提供的函数来分析一些简单的HTML标记。它绊倒了它的第二次尝试时,发现嵌套的HTML标记<em>Test<em>

$t['label'] = 'Test'; 
$text = '<p>Test</p>'; 

$find = '/(?![^<]+>)(?<!\w)(' . preg_quote($t['label']) . ')\b/s'; 
$text = preg_replace_callback($find, 'replaceCallback', $text); 

echo "Find: $find\n"; 
echo 'Quote: ' . preg_quote($t['label']) . "\n"; 
echo "Result: $text\n"; 

/* Returns: 

Find: /(?![^<]+>)(?<!\w)(Test)\b/s 
Quote: Test 
Result: <p><a class="tag" rel="tag-definition" title="Click to know more about Test" href="?tag=Test">Test</a></p> 

*/ 

$t['label'] = '<em>Test</em>'; 
$text = '<p>Test</p>'; 

$find = '/(?![^<]+>)(?<!\w)(' . preg_quote($t['label']) . ')\b/s'; 
$text = preg_replace_callback($find, 'replaceCallback', $text); 

echo "Find: $find\n"; 
echo 'Quote: ' . preg_quote($t['label']) . "\n"; 
echo "Result: $text\n"; 

/* Returns: 

Find: /(?![^<]+>)(?<!\w)(Test)\b/s 
Quote: Test 
Result: <p><a class="tag" rel="tag-definition" title="Click to know more about Test" href="?tag=Test">Test</a></p> 
Warning: preg_replace_callback() [function.preg-replace-callback]: Unknown modifier '\' in /test.php on line 25 
Find: /(?![^<]+>)(?<!\w)(\<em\>Test\</em\>)\b/s 
Quote: \<em\>Test\</em\> 

Result: 

*/ 

function replaceCallback($match) { 
    if (is_array($match)) { 
     $htmlVersion = $match[1]; 
     $urlVersion = urlencode($htmlVersion); 
     return '<a class="tag" rel="tag-definition" title="Click to know more about ' . $htmlVersion . '" href="?tag=' . $urlVersion . '">' . $htmlVersion . '</a>'; 
    } 
    return $match; 
} 
+0

好吧,我想我明白了,html对于正则表达式来说是不够常规的:) 但是,那么,如何在HTML内容中通过超链接替换单词呢? – pixeline 2010-06-29 09:43:12

+0

@pixeline::-)对不起,这个问题在整个地方都会出现。起初,正则表达式似乎是个好主意,但很少有效。无论如何,你应该在PHP中尝试[DOM functions](http://www.php.net/manual/en/book.dom.php)。 [PHPro使用PHP和DOM解析HTML](http://www.phpro.org/examples/Parse-HTML-With-PHP-And-DOM.html)教程也可能有所帮助。 – Mike 2010-06-29 09:57:36

+1

@像素像你这样的问题每天至少会出现三次。搜索*替换HTML *或类似关键字中的属性,或只浏览几页后面的问题。你想要的关键库是DOM。 – Gordon 2010-06-29 12:37:16