仅替换前面没有哈希符号的整个单词匹配项

发布于 2024-11-04 17:59:41 字数 555 浏览 0 评论 0原文

我通过删除此数组中找到的字符串来清理字符串:

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

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

发布评论

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

评论(4

魄砕の薆 2024-11-11 17:59:41

这假设您的字符串中没有 / 。如果是这样,请显式运行 preg_quote(),并将 / 作为第二个参数。

它还假设您想要匹配单词,所以我修剪了每个单词。

$words = array("subida", " de"," do", " da", "em", " na", " no", "blitz");

$words = array_map('trim', $words);

$words = array_map('preg_quote', $words);

$str = preg_replace('/\b[^#](?:' . implode('|', $words) . ')\b/', '', $str);

键盘

This assumes that none of your strings have / in them. If so, run preg_quote() explicitly with / as the second argument.

It also assumes you want to match the words, so I trimmed each word.

$words = array("subida", " de"," do", " da", "em", " na", " no", "blitz");

$words = array_map('trim', $words);

$words = array_map('preg_quote', $words);

$str = preg_replace('/\b[^#](?:' . implode('|', $words) . ')\b/', '', $str);

Codepad.

丑丑阿 2024-11-11 17:59:41

在未能提出一个包罗万象的正则表达式解决方案之后,以下内容可能有用:

$words = array("subida", " de", " do", " da", "em", " na", " no", "blitz");
$words = array_map('trim', $words);

$str = '#blitz *blitz ablitz na subida do alfabarra blitz# blitz blitza';

$str_words = explode(' ', $str);
$str_words = array_diff($str_words, $words);
$str = implode(' ', $str_words);
var_dump($str);

解决基于正则表达式的解决方案中字边界的一些复杂问题。

After failing to come up with a catch-all regex solution, the following may be useful:

$words = array("subida", " de", " do", " da", "em", " na", " no", "blitz");
$words = array_map('trim', $words);

$str = '#blitz *blitz ablitz na subida do alfabarra blitz# blitz blitza';

$str_words = explode(' ', $str);
$str_words = array_diff($str_words, $words);
$str = implode(' ', $str_words);
var_dump($str);

Gets round a few complications with word boundaries in regex-based solutions.

凉月流沐 2024-11-11 17:59:41

试试这个:

for($i=0; $i<$regex('count'); $i++){
    foreach($regex[$i] as $key) {
        if ( is_string($key) ) {
            $twit = str_replace($regex[$i],'', $twit);
        }
    }
}

Try this:

for($i=0; $i<$regex('count'); $i++){
    foreach($regex[$i] as $key) {
        if ( is_string($key) ) {
            $twit = str_replace($regex[$i],'', $twit);
        }
    }
}
花落人断肠 2024-11-11 17:59:41

要使用您的单词集根据需要清理葡萄牙语短语,需要以编程方式为正则表达式引擎准备每个单词。

如果单词以空格(完全无关紧要的单词)开头,则不需要浮动单词边界,只需转义任何特殊字符并附加单词边界即可。

如果单词不以空格开头,则:

  • 可选地匹配前导空格,
  • 要求单词之前有单词边界,并且
  • 不允许前面有哈希符号。

然后转义特殊字符并附加单词边界。

代码:(演示

$subs = array_map(
    fn($v) => (str_starts_with($v, ' ') ? '' : ' ?\b(?<!#)')
        . preg_quote($v) . '\b',
    $subpatterns
);
        
echo preg_replace(
         '~' . implode('|', $subs) . '~u',
         '',
         $str
     );
// #blitz alfabarra

我添加了 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:

  • optionally match a leading space,
  • require a word boundary before the word, and
  • do not allow a preceding hash symbol.

Then escape special characters and append a word boundary.

Code: (Demo)

$subs = array_map(
    fn($v) => (str_starts_with($v, ' ') ? '' : ' ?\b(?<!#)')
        . preg_quote($v) . '\b',
    $subpatterns
);
        
echo preg_replace(
         '~' . implode('|', $subs) . '~u',
         '',
         $str
     );
// #blitz alfabarra

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.

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