- print_r: documentation ( source)
- preg_match_all: documentation ( source)
- preg_quote: documentation ( source)
- preg_replace: documentation ( source)
<?php
$newString = "he 800s.";
$originalString = $newString;
$regex = "/[$#]*(\d+(\.|\,|\s)?\d*(\.|\,|\s)?\d*){3,}[%.\)]*/";
preg_match_all($regex, $originalString, $matches);
foreach($matches[0] as $match) {
if ($match[0] == "$" || $match[0] == "#") {
continue;
}
$stringLength = strlen($match);
if ($match[$stringLength - 1] == "%" || $match[$stringLength - 1] == "." || $match[$stringLength - 1] == ")") {
continue;
}
$replacement = '';
for ($i = 0; $i < strlen($match); $i++) {
if ($match[$i] == '-' || $match[$i] == '+' || $match[$i] == ' ' || $match[$i] == '.' || $match[$i] == ',' || $match[$i] == '(' || $match[$i] == ')') {
$replacement .= $match[$i];
} else {
$replacement .= 'X';
}
}
$match = preg_quote($match);
$newString = preg_replace("/" . $match . "/U", $replacement, $newString);
}
print_r("ORIGINAL STRING: " . $originalString . "\n");
print_r("FILTERED STRING: " . $newString . "\n");
print_r("MATCHES: \n");
print_r($matches);