I'm trying to replace some words in my string for a white space, but the words that I try to replace are different all the time, except for the first 3 characters. For example I have this string:
"Hello, my name is Lorum and toc341013697 I'm 29 years old. And I toc241053612 test and bla h blah blah toc410183666."
I would like to remove the words starting with toc. The numbers behind toc are always different, but they are always 9 characters long.
Is there a way to do this? I tried this:
$text = "Hello, my name is Lorum and toc341013697 I'm 29 years old. And I toc241053612 test and bla h blah blah toc410183666.";
$toc = substr($text, strpos($text, "toc") + strlen("toc"), 9);
$toc = "toc".$toc." ";
$find = array($toc);
$replace = array("");
$text = str_replace($find, $replace, $text);
But the he only removes the first toc.
preg_replace('/toc[0-9]{9}/','', $text);Your way will only remove the first one if you aren't even getting an error.