user123
user123

Reputation: 5407

replace words from text which matched with array value

My array $b is here:

rray(3) {
  [0]=>
  string(14) "8989243three56"
  [1]=>
  string(15) "402three1345233"
  [2]=>
  string(13) "5023one345233"
}

Which I am getting at the end of my code in http://ideone.com/97HHmT

At last I want to check, if any value of $b is present in $s the main string or not. If present then it should be replace by *.

like input is my long STRING with some Numbers 402three1345233 4023one345233 then result should be my long STRING with some Numbers ************ ************* .

What change I should do in http://ideone.com/97HHmT?

Upvotes: 0

Views: 50

Answers (2)

Brewal
Brewal

Reputation: 8189

Assuming you want to replace occurences of $b values in your string with the exact same number of * than the strlen of the occurences :

Demo

$str = 'my long STRING with some Numbers 402three1345233 5023one345233';

foreach ($b as $occurence) {
    $str = str_replace($occurence, str_repeat('*', strlen($occurence)), $str);
}

See str_repeat()

Upvotes: 1

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

try with str_replace()

$str = 'my long STRING with some Numbers 402three1345233 4023one345233';
echo str_replace($b, '**********', $str);
// output :- my long STRING with some Numbers ********** **********

Upvotes: 1

Related Questions