仅替换前面没有哈希符号的整个单词匹配项
我通过删除此数组中找到的字符串来清理字符串:
$regex = array("subida", " de"," do", " da", "em", " na", " no", "blitz");
这是我正在使用的 str_replace()
:
for ($i = 0;$i < 8; $i++){
$twit = str_replace($regex[$i], '', $twit);
}
如何让它仅删除一个单词(如果它正是字符串中的单词) ,我的意思是,我有以下短语:
#blitz na subida do alfabarra blitz
它将返回我:
# alfabarra
我不希望第一个 blitz
被删除,因为它前面有一个哈希 (#
) ,我希望它输出:
#blitz alfabarra
I'm sanitizing a string by removing strings found in this array:
$regex = array("subida", " de"," do", " da", "em", " na", " no", "blitz");
And this is the str_replace()
that I'm using:
for ($i = 0;$i < 8; $i++){
$twit = str_replace($regex[$i], '', $twit);
}
How do I make it only remove a word if it's exactly the word in string, I mean, I have the following phrase:
#blitz na subida do alfabarra blitz
it will return me:
# alfabarra
I don't want the first blitz
to be removed because it is preceded by a hash (#
), I want it to output:
#blitz alfabarra
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这假设您的字符串中没有
/
。如果是这样,请显式运行preg_quote()
,并将/
作为第二个参数。它还假设您想要匹配单词,所以我修剪了每个单词。
键盘。
This assumes that none of your strings have
/
in them. If so, runpreg_quote()
explicitly with/
as the second argument.It also assumes you want to match the words, so I trimmed each word.
Codepad.
在未能提出一个包罗万象的正则表达式解决方案之后,以下内容可能有用:
解决基于正则表达式的解决方案中字边界的一些复杂问题。
After failing to come up with a catch-all regex solution, the following may be useful:
Gets round a few complications with word boundaries in regex-based solutions.
试试这个:
Try this:
要使用您的单词集根据需要清理葡萄牙语短语,需要以编程方式为正则表达式引擎准备每个单词。
如果单词以空格(完全无关紧要的单词)开头,则不需要浮动单词边界,只需转义任何特殊字符并附加单词边界即可。
如果单词不以空格开头,则:
然后转义特殊字符并附加单词边界。
代码:(演示)
我添加了
u
模式修饰符,以防多字节字符出现进入游戏。您的示例文本并未表明可能存在大写字符。To sanitize your Portuguese phrase as desired using your set of words, each word will need to be programmatically prepared for the regex engine.
If the word starts with a space (an entirely insignificant word) then there is no need for a leafing word boundary, simply escape any special characters and append a word boundary.
If the word does not start with a space, then:
Then escape special characters and append a word boundary.
Code: (Demo)
I have added the
u
pattern modifier in case multibyte characters come into play. Your sample text doesn't indicate the possibility of uppercase characters.