PHP:如果包含@,就从句子中删除单词


PHP: remove word from sentence if it contains @

如果单词包含@,我想从句子中删除单词,我使用的是php

输入:嗨,我是@RaghavSoni

输出:嗨,我是

谢谢。

你可以做:

$str = preg_replace('/@'w+/', '', $str);

这不是一个好方法,但它有效:

<?php
$input="Hi I am @RaghavSoni";
$inputWords = explode(' ', $input);
foreach($inputWords as $el)
{
    if($el[0]=="@" )
    {
        $input = str_replace($el, "", $input);
    }
}
echo $input;
 ?> 
while(strpos($string, '@') !== false) {
    $location1 = strpos($string, "@");
    $location2 = strpos($string, " ", $location1);
    if($location2 !== false) {
        $length = $location2 - $location1;
        $string1 = substr($string, 0, $location1);
        $string2 = substr($string, $location2);
        $string = $string1 . $string2;
    }
}
echo $string;
echo str_replace("@RaghavSoni", "", "Hi I am @RaghavSoni.");
# Output: Hi I am.