Vince Kronlein Vince Kronlein - 6 months ago 13
PHP Question

XPath contains() Search for Exact Match

Is it possible to search a DOMDocument object with fn:contains and return true on only an exact match for a word?

I have a text replacement snippet that I did not write myself that does internal link replacements for keywords. But as written it also replaces partial words instead of only the full word.

Here is the snippet:

$autolinks = $this->config->get('autolinks');
if (isset($autolinks) && (strpos($this->data['description'], 'iframe') == false)
&& (strpos($this->data['description'], 'object') == false)):
$xdescription = mb_convert_encoding(html_entity_decode($this->data['description'], ENT_COMPAT, "UTF-8"), 'HTML-ENTITIES', "UTF-8");
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML('<div>'.$xdescription.'</div>');
libxml_use_internal_errors(false);
$xpath = new DOMXPath($dom);
foreach ($autolinks as $autolink):
$keyword = $autolink['keyword'];
$xlink = mb_convert_encoding(html_entity_decode($autolink['link'], ENT_COMPAT, "UTF-8"), 'HTML-ENTITIES', "UTF-8");
$target = $autolink['target'];
$tooltip = isset($autolink['tooltip']);
$pTexts = $xpath->query(
sprintf('///text()[contains(., "%s")]', $keyword)
);
foreach ($pTexts as $pText):
$this->parseText($pText, $keyword, $dom, $xlink, $target, $tooltip);
endforeach;
endforeach;
$this->data['description'] = $dom->saveXML($dom->documentElement);
endif;


In example:

If my keyword is "massage" *massage*r is partially matched and converted to a link, when only the whole word massage should be converted, not massager.

Answer

This actually turned out to be incredibly simple, I just added a space onto the end of the $keyword variable so now it only returns true when the entire word is found.

foreach ($autolinks as $autolink):
    $keyword    = trim($autolink['keyword']) . ' ';
    $xlink      = mb_convert_encoding(html_entity_decode($autolink['link'], ENT_COMPAT, "UTF-8"), 'HTML-ENTITIES', "UTF-8");
    $target     = $autolink['target'];
    $tooltip    = isset($autolink['tooltip']);                          
    $pTexts     = $xpath->query(
        sprintf('///text()[contains(., "%s")]', $keyword)
    );
    foreach ($pTexts as $pText):
        $this->parseText($pText, $keyword, $dom, $xlink, $target, $tooltip);
    endforeach;
endforeach;

thank you to everyone who tried to help.