Let's say I have this:
$hello = "Hello, is StackOverflow a helpful website!? Yes!";
and I want to strip punctuation so it outputs as:
hello_is_stackoverflow_a_helpful_website_yes
How can I do that?
# to keep letters & numbers
$s = preg_replace('/[^a-z0-9]+/i', '_', $s); # or...
$s = preg_replace('/[^a-z\d]+/i', '_', $s);
# to keep letters only
$s = preg_replace('/[^a-z]+/i', '_', $s);
# to keep letters, numbers & underscore
$s = preg_replace('/[^\w]+/', '_', $s);
# same as third example; suggested by @tchrist; ^\w = \W
$s = preg_replace('/\W+/', '_', $s);
for string
$s = "Hello, is StackOverflow a helpful website!? Yes!";
result (for all examples) is
Hello_is_StackOverflow_a_helpful_website_Yes_
Enjoy!
Hello, world!
I'll get hello__world
when there is suppose to be one underscore for space not... two.
function strip_punctuation($string) {
$string = strtolower($string);
$string = preg_replace("/[:punct:]+/", "", $string);
$string = str_replace(" +", "_", $string);
return $string;
}
First the string is converted to lower case, then punctuation is removed, then spaces are replaced with underscores (this will handle one or more spaces, so if someone puts two spaces it will be replaced by only one underscore).
[:alnum:]
and [:punct:]
. \pP
relies on Unicode stuff, idk how that works in PHP considering that PHP has awful unicode support.
Commented
Apr 16, 2011 at 22:51
\p{General_Category=Punctuation}
, which is usually abbreviated as though it were a binary property, like \p{Punctuation}
. Unfortunately PCRE doesn’t have good Unicode property support. The reason I would never trust POSIX charclasses is because they’re too subject to breakage based on vendor locales and user settings. It almost never handles Unicode correctly, even though this is required by UTS#18’s RL1.2. I mistrust and mislike anything that doesn’t do Unicode.
\pP
“punctuation property” always works on all Unicode punctuation, no matter what sort of silly locale setting is or is not in play or how well the vendor has implemented them or whether they have kept up with the Unicode Standard, which they are all hopelessly very slow at.
Without regular expressions:
<?php
$hello = "Hello, is StackOverflow a helpful website!? Yes!"; // original string
$unwantedChars = array(',', '!', '?'); // create array with unwanted chars
$hello = str_replace($unwantedChars, '', $hello); // remove them
$hello = strtolower($hello); // convert to lowercase
$hello = str_replace(' ', '_', $hello); // replace spaces with underline
echo $hello; // outputs: hello_is_stackoverflow_a_helpful_website_yes
?>
I'd go with something like this:
$str = preg_replace('/[^\w\s]/', '', $str);
I don't know if that's more broad than you're looking for, but it sounds like what you're trying to do.
I also notice you've replaced spaces with underscores in your sample. The code I'd use for that is:
$str = preg_replace('/\s+/', '_', $str);
Note that this will also collapse multiple spaces into one underscore.