Phppre_replace () highlight text to highlight the words inin & nbsp; the & nbsp; rooming, & nbsp; he & nbsp; got & nbsp; into & nbsp; the & nbsp; room, & nbsp; when & nbsp; he's & nbsp; o php pre_replace () highlight text
You want to highlight the word in the following text
In the rooming, he got into the room, when he's ordered an inexpensive.
I wrote it like this, but into, inexpensive, rooming, and in are highlighted, and no space is available.
$ Pttn = "$ newrow ";
$ Str = preg_replace ("/\ s ($ newrow) \ s/I", $ pttn, $ str );
How can I only display in, instead of into, inexpensive, and rooming ??
------ Solution --------------------
There is no error in writing this way. it will only replace in, but you just need to add a space before in.
Write Better
$str = "in the rooming, he got into the room, when he's ordered an inexpensive.";
$newrow = 'in';
$pttn = "$newrow";
echo $str = preg_replace("/\b($newrow)\b/i",$pttn, $str);
In the rooming, he got into the room, when he's ordered an inexpensive.
\ B indicates the word boundary
------ Solution --------------------
If you only want to implement it once, the answers from the upstairs moderators are completely correct.
If you want to develop products, you need to consider at least a little more special situations, such as \ s.
$ Str = "in the rooming, he got into the room, when he's ordered an inexpensive. \ s ";
$ Newrow = '\ s'; // This will change the effect.
$ Pttn = "$ newrow ";
Echo $ str = preg_replace ("/\ B ($ newrow) \ B/I", $ pttn, $ str );
I had a hard time, so I could replace \ s.
$ Str = "\ s in the rooming, he got into \ s the room, when he's ordered an inexpensive. \ s ";
$ Newrow = "\ s ";
$ Newrow2 = addslashes ($ newrow );
$ Pttn = "$ newrow ";
// I did not understand the details. \ B is invalid here. output the result first.
Echo $ str = preg_replace ("/(\ s +
------ Solution --------------------
^) ($ Newrow2) (\ s +
------ Solution --------------------
$)/I "," \ 1 ". $ pttn." \ 3 ", $ str );
------ Solution --------------------
Should I replace the reserved word \ s .?
Addslashes ($ newrow); just a moment.
It's a strange thing. when the reserved word \ s is marked red, the boundary character \ B becomes invalid. can you explain it?
Reference:
Are you kidding?
What is preg_quote?
\ S is a reserved word of the regular expression. if it is used as a matching string, it must be transferred!
------ Solution --------------------
Reference:
I want to ask a question that I have never understood:
What are the essential meanings of \ 1 \ 2 \ 3 and $1?
Each pair of "()" in the regular expression rule string represents a backward reference, which can be used by subsequent rules. It will also appear in the results
Since it can be used, you need to know which pair. Therefore, it is stipulated that the value starts from 1 in the order of appearance.
\ 1 indicates the matching content in the first pair () in the rule string
In the result, \ 1 is used to indicate the matched content in the first pair ().
As for $1, php allows this write because it is expressed in js.
Writing web applications is always inseparable from php, js, and html. isn't similar syntax components written in a similar way?