【问题标题】:How to replace the comma by colon within the parenthesis using preg_replace如何使用 preg_replace 在括号内用冒号替换逗号
【发布时间】:2015-10-29 12:42:00
【问题描述】:

我想仅使用 preg_replace 将括号内的逗号 (,) 替换为冒号 (:)

输入:

`parent_id` int(10) unsigned DEFAULT NULL,
`type` enum('text','textarea','mail','uri','date','image','checkbox','menu','menu_option','group','frame','history') NOT NULL DEFAULT 'text',

输出:

`parent_id` int(10) unsigned DEFAULT NULL,
    `type` enum('text':'textarea':'mail':'uri':'date':'image':'checkbox':'menu':'menu_option':'group':'frame':'history') NOT NULL DEFAULT 'text',

请给我们建议

【问题讨论】:

  • 用“','”替换“':'”不符合您的要求吗?请注意字面的撇号。
  • 先捕获"(" ")"之间的整个字符串,然后做你想做的事。
  • 但你为什么要这么做?

标签: php regex preg-replace


【解决方案1】:

使用

echo preg_replace('/\'[\s]*\,[\s]*\'/', "':'", $input);

\' 匹配字符 ' 字面意思

[\s]* 将删除单引号和逗号之间的所有空格

【讨论】:

  • 有没有可能得到如下结果
  • 输出 => 'text':'textarea':'mail':'uri':'date':'image':'checkbox':'menu':'menu_option':'group' :'框架':'历史'
【解决方案2】:

试试这个

$a="place your input here";
$b=preg_replace('/\,/', ':', $a); 
echo $a."<br/>";
echo $b;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2016-04-19
    • 2022-12-30
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    相关资源
    最近更新 更多