想要使用 PHP 仅从字符串中查找和删除单词

发布于 2024-12-26 13:02:52 字数 547 浏览 2 评论 0原文

我有特定的需要从字符串中删除单词,但是当该单词具有点 (.) 字符时我遇到问题。

让我们看看这里是字符串以及我到目前为止尝试过的内容?

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

尘曦 2025-01-02 13:02:52

这是因为点可以匹配任何字符。

问题还在于 \b 确实匹配单词边界,即单词字符后跟非单词字符的位置,或者非单词字符后跟单词字符的位置。但由于点不是单词字符,也不是空格,因此它不会匹配。

也许你应该尝试这样做:

preg_replace('/\bKa(\W|$)/i', '', $result)

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:

preg_replace('/\bKa(\W|$)/i', '', $result)
遮云壑 2025-01-02 13:02:52

原因是 \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".

你好,陌生人 2025-01-02 13:02:52

你需要转义点 ie 。而不是 .

preg_replace('/\bKa\.\b/i', '', $result); 

You need to escape the dot i.e. . instead of .

preg_replace('/\bKa\.\b/i', '', $result); 
做个少女永远怀春 2025-01-02 13:02:52

也许这会按照你想要的方式工作?

preg_replace('/\bKa[\.]?(\s|$)/i', '', $result);

perhaps this will work the way you want it to?

preg_replace('/\bKa[\.]?(\s|$)/i', '', $result);
若有似无的小暗淡 2025-01-02 13:02:52

这是一个基于前瞻的正则表达式,适用于您的情况:

$result = 'Ka. Hello Ka. Kashish. Ka.';
$result = preg_replace('/(?<=\b)Ka\.(?=(\W|$))/i', '', $result);

输出:

string(17) " Hello  Kashish. "

Here is a lookahead based regex that will work for your case:

$result = 'Ka. Hello Ka. Kashish. Ka.';
$result = preg_replace('/(?<=\b)Ka\.(?=(\W|$))/i', '', $result);

OUTPUT:

string(17) " Hello  Kashish. "
梦纸 2025-01-02 13:02:52

rtrim 用于删除字符串右侧选定的字符。

以下是如何从句子末尾删除点的示例:

$result1=rtrim($result, '.');
echo $result1;

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:

$result1=rtrim($result, '.');
echo $result1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文