假设我有这个:
$hello = "Hello, is StackOverflow a helpful website!? Yes!";
Run Code Online (Sandbox Code Playgroud)
我想删除标点符号,因此输出为:
hello_is_stackoverflow_a_helpful_website_yes
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
Wh1*_*Ck5 52
# 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);
Run Code Online (Sandbox Code Playgroud)
对于字符串
$s = "Hello, is StackOverflow a helpful website!? Yes!";
Run Code Online (Sandbox Code Playgroud)
结果(对于所有例子)是
Hello_is_StackOverflow_a_helpful_website_Yes_
请享用!
Raf*_*ler 15
function strip_punctuation($string) {
$string = strtolower($string);
$string = preg_replace("/[:punct:]+/", "", $string);
$string = str_replace(" +", "_", $string);
return $string;
}
Run Code Online (Sandbox Code Playgroud)
首先将字符串转换为小写,然后删除标点符号,然后用下划线替换空格(这将处理一个或多个空格,因此如果有人放置两个空格,它将仅被一个下划线替换).
qua*_*tme 10
没有正则表达式:
<?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
?>
Run Code Online (Sandbox Code Playgroud)