简体   繁体   English

查找具有特殊字符的字符串的最后一个单词

[英]Find last word of a string that has special characters

I am trying to add a span tag to the last word of a string. 我正在尝试将span标记添加到字符串的最后一个单词。 It works if the string has no special characters. 如果字符串没有特殊字符,它就有效。 I can't figure out the correct regex for it. 我无法弄清楚正确的正则表达式。

$string = "Onun Mesajı";
echo preg_replace("~\W\w+\s*\S?$~", ' <span>' . '\\0' . '</span>', $string);

Here is the Turkish character set : ÇŞĞÜÖİçşğüöı 这是土耳其字符集: ÇŞĞÜÖİçşğüöı

You need to use /u modifier to allow processing Unicode characters in the pattern and input string. 您需要使用/u修饰符来允许在模式和输入字符串中处理Unicode字符。

preg_replace('~\w+\s*$~u', '<span>$0</span>', $string); 
                       ^

Full PHP demo : 完整的PHP演示

$string = "Onun Mesajı";
echo preg_replace("~\w+\s*$~u", '<span>$0</span>', $string);

Also, the regex you need is just \\w+\\s*$ : 另外,你需要的正则表达式只是\\w+\\s*$

  • \\w+ - 1 or more alphanumerics \\w+ - 1个或更多的字母数字
  • \\s* - 0 or more whitespace (trailing) \\s* - 0个或更多个空格(尾随)
  • $ - end of string $ - 结束字符串

Since I removed the \\W from the regex, there is no need to "hardcode" the leading space in the replacement string (removed, too). 由于我从正则表达式中删除了\\W ,因此无需对替换字符串中的前导空格进行“硬编码”(也已删除)。

You should use the u modifier for regular expressions to set the engine into unicode mode: 您应该使用正则表达式的u修饰符将引擎设置为unicode模式:

<?php
$subject = "Onun äöüß Mesajı";
$pattern = '/\w+\s*?$/u';
echo preg_replace($pattern, '<span>\\0</span>', $subject);

The output is: 输出是:

Onun äöüß <span>Mesajı</span>

This regex will do the trick for you, and is a lot shorter then the other solutions: 这个正则表达式将为您提供技巧,并且比其他解决方案短得多:

[ ](.*?$)

Here is an example of it: 这是一个例子:

$string = "Onun Mes*ÇŞĞÜÖİçşğüöıajı";
echo preg_replace('~[ ](.*?$)~', ' <span>' .'${1}'. '</span>', $string);

Will echo out: 将呼应:

Onun <span>Mes*ÇŞĞÜÖİçşğüöıajı</span>

The way this regex works is that we look for any characters without space in lazy mode [ ].*? 这个正则表达式的工作方式是我们在惰性模式[ ].*?查找没有空格的任何字符[ ].*? .
then we add the $ identifier, so it matches from the end of the string instead. 然后我们添加$ identifier,所以它匹配字符串的结尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM