I am trying to print a preg_replace
result outside the loop it is made.
This is my code
$search = "Lola and Chris";
$query_gdpr_names = "select name FROM gdpr_names";
$result_gdpr_names = mysqli_query($connect, $query_gdpr_names);
while ($row_gdpr_names = mysqli_fetch_assoc($result_gdpr_names)) {
$name = $row_gdpr_names['name'];
$regex = "/\b" . $name . "\b/ui";
$search = preg_replace($regex, 'removed', $search);
}
echo $search;
If I try to print the $search
inside the loop it will print each iteration as expected. But if I try to print it outside the loop, it comes empty.
If I try to use str_replace instead, it will print outside correctly
$search = str_replace($name, 'removed', $search);
Any ideas what I am doing wrong with the preg_replace?