0

I want to remove a word from string If containing that character and store in another variable. Example:

$a = 'This is bla bla @@ani bla bla.'; 

So I want to remove word containing @@ and store in another world.

So after removing $a will have This is bla bla bla bla.

And @@ani should store in another variable

Any help will greatly appreciated

yivi
  • 42,438
  • 18
  • 116
  • 138
Aniket
  • 17
  • 4
  • 1
    Possible duplicate of [How to check if a string contains a specific word in PHP?](http://stackoverflow.com/questions/4366730/how-to-check-if-a-string-contains-a-specific-word-in-php) – Syed Waqas Bukhary Dec 26 '16 at 07:54

1 Answers1

3

First you should separate words then look for the word contains "@@":

$a='This is bla bla @@ani bla bla. ';

$array = explode(' ',$a); //containig all words

$result = [];
foreach($array as $value){
    if(strpos($value,"@@") > -1){
        $result[] = $value;
    }
}  

$result contains all words matching "@@"

UPDATE:

now for removing these words from the original String :

$finalString = str_replace($result, "", $a);

$finalString is $a but without words containing "@@".

Ahmad Mobaraki
  • 7,426
  • 5
  • 48
  • 69
  • It successfully stored word. Containing @@ in another variable but now $a variable contains only ok all data before @@ is gone – Aniket Dec 26 '16 at 08:24
  • Few errors I fixed. First you should separate words then look for the word contains "@@": $a='This is bla bla @@ani bla bla. '; $fs=$a; $array = explode(' ',$a); //containig all words $result = []; foreach($array as $value){ if(strpos($value,"@@") > -1){ $result[] = $value; } } $fs=str_replace($result[0],' ', $fs); $fs contains $a data but word containing "@@" removed $result contains all words matching "@@" It worked now.. Thank u – Aniket Dec 26 '16 at 08:45