【问题标题】:Replace some pattern with string用字符串替换一些模式
【发布时间】:2017-05-02 08:43:33
【问题描述】:

我有一个包含类似值的数组

$arr = array("abc-xyz","a pqr","rty'gjg","sdhf,sjh","dhd.jkyt");

我想用下划线替换所有出现的连字符、空格、逗号、点、单引号。

如何用模式替换。

我知道我必须使用

preg_replace($pattern,$replacewith,$string);

但是我不知道怎么用。

【问题讨论】:

    标签: php arrays pattern-matching


    【解决方案1】:

    希望这个简单的preg_replace 能帮到你。

    正则表达式: [\'\.,\s-]

    1. [\'\.,\s-] 这将匹配, ,-,., 'space 中的任何一个字符

    Try this code snippet here

    <?php
    ini_set('display_errors', 1);
    $arr = array("abc-xyz","a pqr","rty'gjg","sdhf,sjh","dhd.jkyt");
    $arr=preg_replace('/[\'\.,\s-]/', "_", $arr);
    print_r($arr);
    

    【讨论】:

    • 谢谢。我会试试这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    相关资源
    最近更新 更多