替换除字母、数字、空格和下划线之外的所有字符[关闭]

问题描述 投票:0回答:3

我希望替换字符串中除字母、数字、空格和下划线之外的所有字符。

有人可以举个例子吗?

php regex preg-replace ereg-replace
3个回答
92
投票

霍雷肖

A mote it is to trouble the mind's eye.
In the most high and palmy state of Rome,
A little ere the mightiest Julius fell,
The graves stood tenantless and the sheeted dead
Did squeak and gibber in the Roman streets:
As stars with trains of fire and dews of blood,
Disasters in the sun; and the moist star
Upon whose influence Neptune's empire stands
Was sick almost to doomsday with eclipse:
And even the like precurse of fierce events,
As harbingers preceding still the fates
And prologue to the omen coming on,
Have heaven and earth together demonstrated
Unto our climatures and countrymen.--
But soft, behold! lo, where it comes again!

Re-enter Ghost
I'll cross it, though it blast me. Stay, illusion!
If thou hast any sound, or use of voice,
Speak to me:
If there be any good thing to be done,
That may to thee do ease and grace to me,
Speak to me:

Cock crows
If thou art privy to thy country's fate,
Which, happily, foreknowing may avoid, O, speak!
Or if thou hast uphoarded in thy life
Extorted treasure in the womb of earth,
For which, they say, you spirits oft walk in death,
Speak of it: stay, and speak! Stop it, Marcellus.

马塞勒斯

Shall I strike at it with my partisan?

31
投票
[^0-9a-zA-Z_\s] 

是你要替换的。


8
投票
<?php
$string = 'April 15, 2003';
$pattern = '/[^\w ]+/';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
?>
© www.soinside.com 2019 - 2024. All rights reserved.