i have a string say "hey how are you going Mr. Matt" now i want to be able replace all characters in this string including double quotes that aren't [a-z][A-Z] letters how do i achieve it in php?
http://php.net/manual/en/function.preg-replace.php
<?php
$string = '"hey how are you going Mr. Matt"';
$pattern = '/[^a-zA-Z]/';
$replacement = '-';
echo preg_replace($pattern, $replacement, $string);
?>
Live example (codepad).
/[^a-zA-Z]/
is the regular expression. It is basically just a set ([
]
) containing a-z
and A-Z
. It is however inverted (^
), so it matches all non-alphabetic characters, and replaces them.
$clean_string = preg_replace('/[^a-zA-Z]/','',$string);
Will replace everything that's not a-zA-Z. Add a space in the [] if you need them, or a /s if you want all whitespace.
精彩评论