正则表达式梦Night [英] Regular Expression Nightmare
问题描述
大家好,
我似乎可以解决这个reg-ex,任何帮助将不胜感激,
我想将搜索到的单词替换为HREF,但要避免HREF嵌套,我现在正在做的事情是,我在开始和结束处放置了垃圾``< ##''和``##>"已更改的文本中搜索到的单词中的单词,然后替换不在< ##和##>
之间的单词
Hello Guys,
there is this reg-ex that i can seem to solve, any help would be appreciated,
i wana replace the searched word into an HREF,but avoid the nesting of HREFs, what im doin rite now, is that i place garbage ''<##'' and ''##>'' in the begining and end of the searched word in the text that has already been changed and then replace the words not in between <## and ##>
// placing <## and ##>
$temptext = preg_replace('/(<a([^>]+)>)(.*?)(<\/a>)/is', "$1<##$3##>$4", $blogvalue["content"]);
//then search the word not in <## and ##>
$search = "/([^<##a-zA-Z'=:\"]".mysql_escape_string($value["keyword"]).")/is";
// replace it with the HREF
$replace = ' <a href="'.$value[" url="].'">'.mysql_escape_string($value["keyword"]).'</a>';
//assign it to the new content
$newContent = preg_replace($search, $replace, $temptext);
现在的问题是,这也改变了HREF内部的链接,
例如,如果我的内容中的某些链接已经更改,例如
< a href ="www.brown.com">布朗</a>我想更改内容中其余的棕色",它会忽略锚点内的棕色",但会更改www.brown.com中的棕色" ...
任何想法
[修改:添加了前置标记,尽管我不确定这是什么语言... $使我想到了PHP,但没有以//开头的注释.
now the problem is that this also changes the link inside the HREF,
for example, if i have some link already changed in my content like
<a href="www.brown.com">Brown</a> and i wana change the rest of the ''Brown'' in the content, it would ignore the ''Brown'' inside the anchors, but would change the ''brown'' in www.brown.com .....
any ideas
[Modified: added pre tags, though I''m not sure what language that is...the $ made me think PHP, but not with comments starting with //]
推荐答案
temptext = preg_replace(' /(< a([^>] +)>)()( .*?)(< \/a>)/是',"
temptext = preg_replace('/(<a([^>]+)>)(.*?)(<\/a>)/is', "
1< ##
3 ##>
这篇关于正则表达式梦Night的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!