You could do:
$str = preg_replace('/@\w+/', '', $str);
Question
I want to remove words from sentence if word contains @, I am using php.
Input: Hi I am @RaghavSoni
Output: Hi I am
Thank You.
Solution
You could do:
$str = preg_replace('/@\w+/', '', $str);
OTHER TIPS
This is not a good way, but it works :
<?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.