【发布时间】:2009-04-30 06:37:30
【问题描述】:
考虑这个字符串
hello awesome <a href="" rel="external" title="so awesome is cool"> stuff stuff
我可以使用什么正则表达式来匹配没有出现在锚标题属性中的awesome?
到目前为止,这就是我想出的(可悲的是它不起作用)
/[^."]*(awesome)[^."]*/i
编辑
我使用Alan M's advice 并使用正则表达式来捕获每个单词并将其发送到回调。感谢艾伦 M 的建议。这是我的最终代码。
$plantDetails = end($this->_model->getPlantById($plantId));
$botany = new Botany_Model();
$this->_botanyWords = $botany->getArray();
foreach($plantDetails as $key=>$detail) {
$detail = preg_replace_callback('/\b[a-z]+\b/iU', array($this, '_processBotanyWords'), $detail);
$plantDetails[$key] = $detail;
}
还有 _processBotanyWords()...
private function _processBotanyWords($match) {
$botanyWords = $this->_botanyWords;
$word = $match[0];
if (array_key_exists($word, $botanyWords)) {
return '<a href="' . PATH_BASE . 'articles/botany-words/#botany-word-' . str_replace(' ', '-', strtolower($word)) . '" title="' . trim($botanyWords[$word]) . '">' . $word . '</a>';
} else {
return $word;
}
}
希望有一天能帮助别人!再次感谢您的所有回答。
【问题讨论】: