Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could split the string into "sentences" (split on a full stop (what about exclamation marks, question marks, ...)).</p> <p>Then find the sentence with the matching word.</p> <p>Then split that sentence into "words" and add some tags in two words before and after the matching word. Since you only have one sentence to work on, you'd include a check to make sure you didn't go out of bounds of the word array.</p> <p>Then join the words back together, and join all the sentences back together.</p> <hr> <p>Alternatively, you could use regex and <a href="http://www.php.net/manual/en/function.preg-replace.php"><code>preg_replace</code></a> (although that may not be a road you want to go down, especially if you have an option such as splitting on plaintext - there's a quote that goes something like "You have a problem and you want to use regex for it. Now you have two problems."):</p> <pre><code>$string = preg_replace("/\\b(\\w+ +){0,2}$query( +\\w+){0,2}\\b/i", '&lt;strong&gt;$0&lt;/strong&gt;', $string); </code></pre> <p>The regex works like this (backslashes are escaped above):</p> <pre><code>\b | match a word boundary (ie match whole words) (\w+ +) | match a "word" followed by spaces (to separate it from the next word) {0,2} | match 0 to 2 of these such words (it will match as many as possible | up to 2) $query | match the '$query' string ( +\w+) | regex for space (separating $query) followed by a word {0,2} | match 0 to 2 of these words (as many as possible up to 2) \b | match a word boundary (ie match whole words) </code></pre> <p>The <code>/i</code> at the end means "case insensitive".</p> <p>The replacement string, <code>&lt;strong&gt;$0&lt;/strong&gt;</code>, means to replace with all the matched words surrounded by the 'strong' tags.</p> <p>The reason this works is that <strong>the regular expression does not allow a full stop to be matched</strong>. So it will grab <em>up to</em> 2 words either side of <code>$query</code> but is forbidden from going over full stops.</p> <p>There are the usual caveats (that you would have with any method you used) -- do you want the bolding to go over question marks? exclamation marks? Is an apostrophe allowed in a word? What will you do about non-full-stop punctuation between words? etc.</p> <p>I'd recommend refining the above regex (if you want to use regex, that is) to:</p> <ul> <li>allow apostrophes in a word: change <code>\w+</code> to <code>[\w']+</code> (escape your for PHP backslashes too)</li> <li>allow various punctuation symbols <em>between</em> words: change the <code>+</code> to something like <code>[\s\-&amp;,]+</code> (means "space", "-", "&amp;", "," are allowed between words -- add more to your liking, but don't put "." in to prevent the bold-ing from going over full stops).</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload