Jimbo
Jimbo

Reputation: 364

Find and replace a string that may be partially inside a html tag

Could anybody suggest a possible solution to the following please.

I have a string of text "Suspendisse potenti" for example.

I need to search some html for the string and wrap it in a span tag:

so that

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque 
scelerisque neque in eros convallis egestas. Phasellus lacus massa, 
laoreet cursus scelerisque id, posuere ac mauris. In hac habitasse 
platea dictumst. Aenean eros dui, congue ut ultrices vitae, convallis
quis felis. Ut non ante non nunc tempus ultrices et et ipsum. 
Suspendisse potenti. In sed mauris vel nibh dapibus 
pellentesque eu id dolor. Morbi dictum quam eleifend ante mattis rutrum. 
Mauris nisl ligula, consectetur eget gravida vel, varius vel metus. 
Nullam a ante metus.</p>

would become

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque 
scelerisque neque in eros convallis egestas. Phasellus lacus massa, 
laoreet cursus scelerisque id, posuere ac mauris. In hac habitasse 
platea dictumst. Aenean eros dui, congue ut ultrices vitae, convallis
quis felis. Ut non ante non nunc tempus ultrices et et ipsum. 
<span class="search-match" id="search-term-0">Suspendisse potenti</span>. 
In sed mauris vel nibh dapibus pellentesque eu id dolor. Morbi dictum 
quam eleifend ante mattis rutrum. Mauris nisl ligula, consectetur eget 
gravida vel, varius vel metus. Nullam a ante metus.</p>

I have this working using: $text = preg_replace_callback( '/(' . preg_quote($searchTerm, '/') . '*)/', "Search::addSearchId", $text );

The problem I have is when part of the string in the text I'm searching is in a html tag.

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque 
scelerisque neque in eros convallis egestas. Phasellus lacus massa, 
laoreet cursus scelerisque id, posuere ac mauris. In hac habitasse 
platea dictumst. Aenean eros dui, congue ut ultrices vitae, convallis
quis felis. Ut non ante non nunc tempus ultrices et et ipsum. 
Suspendisse <a href="/link">potenti</a>. In sed mauris vel nibh dapibus 
pellentesque eu id dolor. Morbi dictum quam eleifend ante mattis rutrum. 
Mauris nisl ligula, consectetur eget gravida vel, varius vel metus. 
Nullam a ante metus.</p>

or with the search term and extra text within a tag

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque 
scelerisque neque in eros convallis egestas. Phasellus lacus massa, 
laoreet cursus scelerisque id, posuere ac mauris. In hac habitasse 
platea dictumst. Aenean eros dui, congue ut ultrices vitae, convallis
quis felis. Ut non ante non nunc tempus ultrices et et ipsum. 
Suspendisse <a href="/link">potenti In sed</a> mauris vel nibh dapibus 
pellentesque eu id dolor. Morbi dictum quam eleifend ante mattis rutrum. 
Mauris nisl ligula, consectetur eget gravida vel, varius vel metus. 
Nullam a ante metus.</p>

This obviously misses the text as it's searching for just the plain string without any tags.

Does anybody have any handy regex that can match the plain string as well as the string with html in it?

This is to create some anchors at the top of a page after being referred from a search page, I want to highlight the search term so I need to wrap the whole string.

Upvotes: 3

Views: 132

Answers (1)

Cylian
Cylian

Reputation: 11182

Try this

$result = preg_replace_callback('%\b(Suspendisse potenti)(?!</\w+)\b%im', 'compute_replacement', $subject);

function compute_replacement($groups) {
    // You can vary the replacement text for each match on-the-fly
    // $groups[0] holds the regex match
    // $groups[n] holds the match for capturing group n
    return '<span>$1</span>';
}

Upvotes: 3

Related Questions