1

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.

4
  • What do you mean by none character? Whitespace?... or what is $searchword so I can run the test locally. Commented Jun 15, 2015 at 23:42
  • like ] searchword = test Commented Jun 15, 2015 at 23:52
  • So if white space trails the search term replace it? From example output should be <span class="searchword" >test</span>ok /n[attachment=1]test-2.png[/attachment], is that right? Commented Jun 16, 2015 at 0:00
  • Or bold is the best way, so you have: [b]test[/b]ok n[attachment=1]test-2.png[/attachment], There must be a space after the searchword to find the word Commented Jun 16, 2015 at 0:09

2 Answers 2

2

You can do it by using regex backtracking control verbs (the (*SKIP)(*FAIL) part).

This will allow you to match any string that is not inside BBcode (either between tags or the tag name itself):

$searchword = "test";

$resmessage  = "test attachment ok \n[attachment=1]test-2.png[/attachment] test ";
$resmessage .= "[test]test[/test] ok [attachment=1]my-test-2.png[/attachment] test";

$pattern = '/\[.+?\].*?\[\/.+?\](*SKIP)(*FAIL)|' . 
    preg_quote($searchword, '/') . 
    "/iu";

$resmessage = preg_replace(
    $pattern, 
    '<span class="searchword">' . $searchword . '</span>', 
    $resmessage
);

This will return:

<span class="searchword">test</span> attachment ok 
[attachment=1]test-2.png[/attachment] <span class="searchword">test</span> 
[test]test[/test] ok [attachment=1]my-test-2.png[/attachment] 
<span class="searchword">test</span>
Sign up to request clarification or add additional context in comments.

8 Comments

This will break the bbcode. Same as in my first post.
Could you provide an example of the code that breaks?
Sorry, that's not what I meant. I didn't mean the broken code, I need the original string before it goes through any processing.
original string: test ok /n[attachment=1]test-2.png[/attachment]
It works fine for me: ideone.com/fork/a0X5Nv, access it and click run, you'll get: <span class="searchword">test</span> ok /n[attachment=1]test-2.png[/attachment]
|
1

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.

<?php
$resmessage = "test ok /n[attachment=1]test-2.png[/attachment]";
$searchword = 'test';
echo preg_replace ( '/' . preg_quote ( $searchword, '/' ) . "\s+/iu", '<span  class="searchword" >' . $searchword . '</span>', $resmessage);

Output:

<span  class="searchword" >test</span>ok /n[attachment=1]test-2.png[/attachment]

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

echo preg_replace ( '/' . preg_quote ( $searchword, '/' ) . "\s+/iu", '[b]' . $searchword . '[/b]', $resmessage);

would give you

[b]test[/b]ok /n[attachment=1]test-2.png[/attachment]

5 Comments

Works almost. When the Title is just "test" then isn't highlighted
Can you provide a full sample on input with test in it? In your above example would it become testok test-2.png" title="test-2.png" rel="lightbox[imagelink2]"> test-2.png" style="max-height:800px;" alt="" />? You could change the + to a * but that's the equivalent of not checking for the whitespace because it no longer would be required.
<?php $resmessage = 'test'; $searchword = 'test'; echo preg_replace ( '/' . preg_quote ( $searchword, '/' ) . "\s+/iu", '<span class="searchword" >' . $searchword . '</span>', $resmessage);
No, I mean when $resmessage, the string you are searching, would have the search term without a space and you'd want to find it. For example what if you had tester, testing, testosterone, testimonials, etc. in your string would you still want to only find test? If so all would become [b]test[/b]....ending of word, is that what you'd want?
What I want is to highlight all search terms, except in attachments or url bbcodes. Because the bbcode changed to html, and breaks the image result, and showed the html as in: s24.postimg.org/xmgd4jgl1/Untitled.jpg So I want a regex to search words except [*] is for the searchword

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.