0

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?

  • 2
    Dup of [Remove everything except letters](http://stackoverflow.com/questions/7264274/remove-everything-except-letters), [How do I remove non alphanumeric characters in a string? (including ß, ...)](http://stackoverflow.com/questions/7271607/how-do-i-remove-non-alphanumeric-characters-in-a-string-including-ss-e-etc), [Remove all non-matching characters in PHP string?](http://stackoverflow.com/questions/3849359/remove-all-non-matching-characters-in-php-string), [Removing Non Alphanumeric Characters With PHP](http://stackoverflow.com/questions/659025/removing-non-alphanumeric-characters-with-php) – outis Sep 09 '11 at 20:40
  • hey thanks for the response but i didn't talk about numeric though just [a-z][A-Z] not [0-9] thanks :) –  Sep 09 '11 at 21:46
  • The first question is exactly the same as yours. The third is generic enough that it tells you how to solve your specific question. The others are close enough that the answers are easily altered to match your needs and have additional information on other letters in Unicode. – outis Sep 09 '11 at 23:55

2 Answers2

1

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.

Håvard
  • 9,900
  • 1
  • 41
  • 46
0
$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.

Rich Bradshaw
  • 71,795
  • 44
  • 182
  • 241