简体   繁体   English

如何从PHP中的此组合中删除点

[英]How to remove dots from this combination in PHP

I have this combination in a string: 我在字符串中有此组合:

"I am tagging @username1.blah. and @username2.test. and @username3."

I tried this: 我尝试了这个:

preg_replace('/\@^|(\.+)/', '', 'I am tagging @username1.blah. and @username2.test. and @username3. in my status.');

But the result is: 但是结果是:

"I am tagging @username1blah and @username2test and @username3 in my status"

The above result is not what I wanted. 上面的结果不是我想要的。

This is what I want to achieve: 这是我要实现的目标:

"I am tagging @username.blah and @username2.test and @username3 in my status."

Could someone help me what I have done wrong in the pattern? 有人可以帮我解决我在模式中做错的事情吗?

Many thanks, Jon 非常感谢,乔恩

I don't like regex very much, but when you are sure that the dots you want to remove are always followed by a space, you could do something like this: 我不太喜欢正则表达式,但是当您确定要删除的点始终带有空格时,可以执行以下操作:

php > $a = "I am tagging @username1.blah. and @username2.test. and @username3.";
php > echo str_replace(". ", " ", $a." ");
I am tagging @username1.blah and @username2.test and @username3

尝试这个:

preg_replace('/\.(\s+|$)/', '\1', $r);

This will replace dots at the end of "words" that are starting with @ 这将替换以@开头的“单词”末尾的点

$input = "I am tagging @username1.blah. and @username2.test. and @username3. in my status.";
echo preg_replace('/(@\S+)\.(?=\s|$)/', '$1', $input);

(@\\S+)\\.(?=\\s|$) will match a dot at the end of a non whitespace ( \\S ) series when the dot is followed by whitespace or the end of the string ( (?=\\s|$) ) (@\\S+)\\.(?=\\s|$)将与非空格( \\S )系列末尾的点匹配,当该点后跟空格或字符串的末尾( (?=\\s|$)

preg_replace('/\.( |$)/', '\1', $string);

怎么样:

preg_replace("/(@\S+)\.(?:\s|$)/", "$1", $string);
/\@\w+(\.\w+)?(?<dot>\.)/

这将匹配所有点并在点组中将其命名

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

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