[英]How to put html tag around word in string
I need to identify a word in the string and then add the tag "< strong >word< /strong > " around the word.我需要识别字符串中的一个单词,然后在单词周围添加标签“<strong>word</strong>”。 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:我的 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". 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 >我需要把“filho”这个词换成“<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".但我需要保持这个词的原样,因为它可以与大写或小写“filho”或“Filho”一起出现。 And in both cases I need to wrap them by the < strong > tag.
在这两种情况下,我都需要用 <strong> 标签包装它们。
How can I identify the word and add the tags around the word I need??如何识别单词并在我需要的单词周围添加标签?
Not sure how this fits into your controller, but check preg_replace https://www.php.net/manual/en/function.preg-replace.php不确定这如何适合您的 controller,但请检查 preg_replace https://www.php.net/manual/en/function.preg-replace.ZE1BFD762321E409CEE4AC0B6E84196
Without giving the full answer (best if you work it out for yourself, then you know for next time) here are some hints:在没有给出完整答案的情况下(最好是自己解决,下次就知道了)这里有一些提示:
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.首先让替换在没有反向引用的情况下工作(因此“Hello”可能会被“hello”替换),然后在第二步中构建反向引用。
Other note: PHP has classes for manipulating HTML, but probably not going to help you in this particular case.其他注意事项:PHP 具有用于操作 HTML 的类,但在这种特殊情况下可能对您没有帮助。
Hope that helps.希望有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.