How to remove the last word from the next line, that is, “dark pink”:

$str = 'Чехол Hoco Business Litchi для iPad 2/3/4 темно-розовый'; 

Or correct my version:

 preg_replace('/^\s*((\S+[\s$]+){1,9}).*/', '\\1', $str); 
  • 2
    So it is more logical, it seems to me, to replace everything from the last space to the end of the line with an empty line: preg_replace ("/ \\ s \\ S + $ / u", "", $ text); - ReinRaus

1 answer 1

Reply from comment.


To remove the last word in a line, it is logical to replace everything from the last space to the end of the line:

 =\s\S+$= 

Example:

 $str = 'Чехол Hoco Business Litchi для iPad 2/3/4 темно-розовый'; $res = preg_replace('=\s\S+$=', '', $str); var_dump($res); 

Fiddle: https://repl.it/CgKL .