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?
$searchstring from that loop, since it replaces a match with the textremoved. You should check your result set. If, for example, it had every letter of the alphabet as a name entry, then your replacement logic would remove all letters. You should step through the loop in debug mode and check.