I'm creating highlights of the searchwords on the results page.
This is what I use now:
$resmessage = preg_replace ( '/' . preg_quote ( $searchword, '/' ) . "/iu", '<span class="searchword" >' . $searchword . '</span>', $resmessage );
But when the word is an title name on a attachment is will break the layout.
example text:
test ok /n[attachment=1]test-2.png[/attachment]
The result:
test ok test-2.png" title="test-2.png" rel="lightbox[imagelink2]"> test-2.png" style="max-height:800px;" alt="" />
So I want exclude none character before searchword. What is the regex to do that, I have tried many options.
This should do it for you.
\s
is a white space (tab, new line, or space),+
says one or more occurances of a white space character.Output:
You can replace it with whatever you want the second parameter of preg_replace is what you want there. http://php.net/manual/en/function.preg-replace.php
So
would give you