I need to identify a word in the string and then add the tag "< strong >word< /strong > " around the word. It turns out that there are Words that start with an uppercase or lowercase letter, so I need to identify both cases and print the result with the tag around the word.
My code is like this:
@foreach($versiculos as $v)
<p>{{ $v->palavra }} </p>
@endforeach
my controller:
public function palavras(Request $request, $palavra = null)
{
$getpalavra = palavras_br::where('slug', $palavra)->first();
$versiculos = nvi::where('palavra', 'RLIKE','[[:<:]]'.$getpalavra->palavras.'[[:>:]]')->paginate(20);
return view('palavra', compact(['versiculos','getpalavra']));
}
Example:
string: "Adão teve relações com Eva, sua mulher, e ela engravidou e deu à luz Caim. Disse ela: "Com o auxílio do Senhor tive um filho homem".
I need to take the word "filho" and replace it with "< strong >filho< / strong >
But I need to keep the word the way it is, because it can appear with upper or lower case "filho" or "Filho" . And in both cases I need to wrap them by the < strong > tag.
How can I identify the word and add the tags around the word I need??
CodePudding user response:
Not sure how this fits into your controller, but check preg_replace https://www.php.net/manual/en/function.preg-replace.php
Without giving the full answer (best if you work it out for yourself, then you know for next time) here are some hints:
- The pattern will be '[WordBreak]word[WordBreak]'
- You need "case insensitive"
- In order to keep the case the same, you need to use "back references" (all described in the manual)
Start by getting the replace to work without the back reference (so "Hello" may get replaced by "hello") and then build the back reference in as a second step.
Other note: PHP has classes for manipulating HTML, but probably not going to help you in this particular case.
Hope that helps.