I am trying to improve upon/fix errors on some existing code that somebody else wrote. It is a script to read through text and highlight words that are in the glossary, by wrapping them in <a></a> tags.
Can somebody explain the following regexp to me?
/\bCreditor[A-Za-z]?\b(?=([^"]“[^”]")[^"]*$)/i
In use:
$glossary_title = "Creditor"; //just a hardcoded example
$glossary_search = '/\\b'.$glossary_title.'[A-Za-z]*?\\b(?=([^"]*"[^"]*")*[^"]*$)/i';
$glossary_replace = '<a....>$0</a>';
$content_temp = preg_replace($glossary_search, $glossary_replace, $content);
The problem I am having with the above is it would ideally also match and wrap in <a></a> tags Creditors, Creditor’s as they are the same word as creditor for glossary sake.
Also the reg ex above doesn’t work with words with apostraphies ’ which it ideally should, like Creditor’s Petition.
Another problem is if a word in the content is already linked (either from the glossary parser or an existing, pre-parser link) it should not be matched and highlighted. Also from this example, Creditor’s Petition is linked as it is a word in the glossary, but the sub-word creditor shouldn’t be linked even it is in the glossary - it would create a nested <a> (eg. <a><a>Creditor’s</a> Petition</a>)and be all messy.
An example input and desired output:
yada yada yada <a href=“already-linked-in-content”>creditor</a> yada yada yada <em>creditor</em> yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada Creditor’s Petition yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada Creditor’s. yada yada and this following one won’t get highlighted cos it is part of another word creditoryada yada yada yada yada yada yada
yada yada yada <a href=“already-linked-in-content”>creditor</a> yada yada yada <em><a href=“glossary-url-1”>creditor</a></em> yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada <a href=“glossary-url-2”>Creditor’s Petition</a> yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada <a href=“glossary-url-1”>Creditor’s</a>. yada yada and this following one won’t get highlighted cos it is part of another word creditoryada yada yada yada yada yada yada
Any help would be greatly appreciated. Thanks