Reputation: 3697
I am wanting to replace all non letter and number characters i.e. /&%#$
etc with an underscore (_)
and replace all '
(single quotes) with ""blank (so no underscore).
So "There wouldn't be any" (ignore the double quotes) would become "There_wouldnt_be_any".
I am useless at reg expressions hence the post.
Cheers
Upvotes: 20
Views: 142585
Reputation: 4291
$newstr = preg_replace('/[^a-zA-Z0-9\']/', '_', "There wouldn't be any");
$newstr = str_replace("'", '', $newstr);
I put them on two separate lines to make the code a little more clear.
Note: If you're looking for Unicode support, see Filip's answer below. It will match all characters that register as letters in addition to A-z
.
Upvotes: 43
Reputation: 63797
If you by writing "non letters and numbers" exclude more than [A-Za-z0-9]
(ie. considering letters like åäö
to be letters to) and want to be able to accurately handle UTF-8 strings \p{L}
and \p{N}
will be of aid.
\p{N}
will match any "Number"\p{L}
will match any "Letter Character", which includes
Documentation PHP: Unicode Character Properties
$data = "Thäre!wouldn't%bé#äny";
$new_data = str_replace ("'", "", $data);
$new_data = preg_replace ('/[^\p{L}\p{N}]/u', '_', $new_data);
var_dump (
$new_data
);
output
string(23) "Thäre_wouldnt_bé_äny"
Upvotes: 42
Reputation: 20394
do this in two steps:
and use preg_replace
:
$stringWithoutNonLetterCharacters = preg_replace("/[\/\&%#\$]/", "_", $yourString);
$stringWithQuotesReplacedWithSpaces = preg_replace("/[\"\']/", " ", $stringWithoutNonLetterCharacters);
Upvotes: 4