正则表达式:替换&而且,。 :/ - _

时间:2017-01-04 12:57:44

标签: php arrays regex string

目前我不知道如何解决我的问题? 我想用空格替换那些字符:

$replaceSpace = ["-", "–", "_", "/"];

那些带有“”(没有/删除):

$replaceNothing = [":", ",", ";", "`", "#", "'", "´", "–", "!", "(", ")", ".", "@", "’", "+", "™"];

我该怎么做? - 如果它们介于单词之间,或者如果它们独立,那也是如此!

问候:)

1 个答案:

答案 0 :(得分:3)

使用str_replace()

示例:

// Outputs F because A is replaced with B, then B is replaced with C, and so on...
// Finally E is replaced with F, because of left to right replacements.
$search  = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);

所以在你的情况下,它将是:

$subject = "sth-sth-sth";
$replaceSpace = ["-", "–", "_", "/"];
echo str_replace($replaceSpace, " ", $subject);