Regex - How to replace the last 3 words of a string with PHP

后端 未结 3 359
独厮守ぢ
独厮守ぢ 2021-01-26 20:03

Trying to wrap the last 3 words in a tag

$str = \'Lorem ipsum dolor sit amet\';
$h2 = preg_replace(\'/^(?:\\w+\\s\\w+)(\\s\\w+)+/\', \'         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-26 20:54

    Here it is:

    $h2 = preg_replace('/(\w+\s\w+\s\w+)$/', '$1', $str);
    

    Since its last three words, so make the left side(from begin) as open to have the match.

提交回复
热议问题