想要使用 PHP 仅从字符串中查找和删除单词
我有特定的需要从字符串中删除单词,但是当该单词具有点 (.) 字符时我遇到问题。
让我们看看这里是字符串以及我到目前为止尝试过的内容?
$result = 'Hello Ka Kashish.';
$result = preg_replace('/\bKa\b/i', '', $result);
我将得到预期的结果 'Hello Kashish.'
但如果字符串如下所示,它不起作用
$result = 'Hello Ka. Kashish.';
$result = preg_replace('/\bKa.\b/i', '', $result);
它会给我结果 'Hello Ka.'。卡西什。'
为什么这个 .(点) 不起作用? 请给我解决方案。
如果我可以通过任何其他方式实现这个词的删除,请告诉我。 我只想删除单词而不是字符集,因为“Ka”单词将被删除,但“Ka”不会从“Kashish”中删除。 请帮我。
提前致谢
I hav especific need of removing word from string, But I am having problem when that word has dot (.) character.
Lets see here is the string and what I have tried so far?
$result = 'Hello Ka Kashish.';
$result = preg_replace('/\bKa\b/i', '', $result);
I will get the expected result 'Hello Kashish.'
But if the string is like below, It is not working
$result = 'Hello Ka. Kashish.';
$result = preg_replace('/\bKa.\b/i', '', $result);
It gives me result 'Hello Ka. Kashish.'
Why this .(dot) is not working?
Please give me solution.
And if I can achive this word removal in any other way, pLease let me know.
I want to remove only word not set of charaters, as 'Ka' word will be removed, but 'Ka' will not be removed from 'Kashish'.
Please help me.
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是因为点可以匹配任何字符。
问题还在于
\b
确实匹配单词边界,即单词字符后跟非单词字符的位置,或者非单词字符后跟单词字符的位置。但由于点不是单词字符,也不是空格,因此它不会匹配。也许你应该尝试这样做:
This is because the dot can match any character.
The problem, too, is that
\b
really matches a word frontier, ie a position where a word character is followed by a non word character, or a non word character is followed by a word character. But as a dot is not a word character and neither is a space for that matter, it won't match.Maybe you should try that instead:
原因是 \b 代表单词边界。即单词字符和非单词字符之间的边界。请参阅 http://www.regular-expressions.info/wordboundaries.html
之间的边界句号“。”并且空格“”不是单词边界,因此模式匹配失败。两者都不 ”。”也不是反斜杠“。”会起作用的。您需要删除第二个“\b”。
分别地, ”。”表示“任何字符”,因此使用反斜杠“.”的目的是确保它只匹配句号,正如其他人指出的那样。在重新设计模式以在没有第二个“\b”的情况下工作时,请务必注意这一点。
The reason is that \b represents a word boundary. I.e. a boundary between a word character and a non-word character. See http://www.regular-expressions.info/wordboundaries.html
The boundary between a full stop "." and a space " " is not a word boundary, so the pattern match fails. Neither "." nor a back-slashed "." will work. You need to remove the second "\b".
Separately, "." means "any character", so the purpose of using back-slash "." is to ensure it matches only a full-stop, as others have pointed out. This is important to note when re-designing your pattern to work without the second "\b".
你需要转义点 ie 。而不是 .
You need to escape the dot i.e. . instead of .
也许这会按照你想要的方式工作?
perhaps this will work the way you want it to?
这是一个基于前瞻的正则表达式,适用于您的情况:
输出:
Here is a lookahead based regex that will work for your case:
OUTPUT:
rtrim 用于删除字符串右侧选定的字符。
以下是如何从句子末尾删除点的示例:
rtrim is used to remove selected characters from right side of the string.
Here is an example of how to remove dot from the end of the sentence: