开发者

trying to show definitions for words that are in a glossary

I'm trying to use regex to parse some plain text and add a definition from a glossary to any words that match it. I'm doing it like this:

for ( $i = 0; $i < count($terms); $i++ ) {
    $search = '|(?<=\b)('.preg_quote($terms[$i]['title']).')(?=\b)|i';
    $replace = '<a class="tt2" rel="tooltip" title="'.$terms[$i]['pageBody'].'">$1</a>';
    $string = preg_replace($search,$replace,$string);
}

I then return $string, however if one of the definitions contains a word which is defined later then the definition开发者_开发技巧 gets added to that also, so I want to no include words that are within the title="". I've tried putting (?!<=title=") but that doesn't work if the definition is multiple words and the match is not the first word.

Any ideas?

example input text:

Over 40,000 fossils, rocks and minerals are evidence of vital changes that Devon’s landscape has seen over the past 490 million years. The 4.5 million years old meteorite is a relict of the early days of our planet. Ichthyosaur skeletons represent a time when large reptiles dominated the oceans and dinosaurs ruled the land. Mammoth tusks are remains of the latest Ice Age during which most of today’s landscape features developed.


You can try in 2 passes :

1 - replace $term[$i]['title'] by something like XXX$iXXX
2 - replace XXX$iXXX by <a ... title="$terms[$i]['pageBody'] ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜