代码之家  ›  专栏  ›  技术社区  ›  fightstarr20

PHP-preg_替换在未按预期工作的数组上

php
  •  -1
  • fightstarr20  · 技术社区  · 6 年前

    我有一个PHP数组,看起来像这样。。

    Array
    (
        [0] => post: 746
        [1] => post: 2
        [2] => post: 84
    )
    

    我想把 post: 从数组中的每个项返回一个如下所示的项。。。

    Array
    (
        [0] => 746
        [1] => 2
        [2] => 84
    )
    

    我试过用这样的替代品。。。

    $array = preg_replace('/^post: *([0-9]+)/', $array );
    print_r($array);
    

    5 回复  |  直到 6 年前
        1
  •  1
  •   Madhur Bhaiya    6 年前

    preg_replace 函数,这是用什么替换匹配的,也就是你的正则表达式有小问题,下面是固定版本:

    preg_replace('/^post:\s*([0-9]+)$/', '$1', $array );
    

    演示 : https://3v4l.org/64fO6

        2
  •  0
  •   ivanivan    6 年前

    你没有替换的模式,也没有空的字符串占位符。

    mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject)
    

    $array = preg_replace('/post: /', '', $array );
    

    应该这么做。

    <?php
    
    $array=array("post: 746",
            "post: 2",
            "post: 84");
    
    $array = preg_replace('/^post: /', '', $array );
    print_r($array);
    ?>
    
    Array
    (
        [0] => 746
        [1] => 2
        [2] => 84
    )
    
        3
  •  0
  •   The fourth bird    6 年前

    您可以不使用正则表达式 array_map substr 要检查前缀并返回不带前缀的字符串,请执行以下操作:

    $items = [
        "post: 674",
        "post: 2",
        "post: 84",
    ];
    
    $result = array_map(function($x){
        $prefix = "post: ";
        if (substr($x, 0, strlen($prefix)) == $prefix) {
            return substr($x, strlen($prefix));
        }
        return $x;
    }, $items);
    
    print_r($result);
    

    结果:

    Array
    (
        [0] => 674
        [1] => 2
        [2] => 84
    )
    
        4
  •  0
  •   gview    6 年前

    有很多方法不涉及正则表达式,而正则表达式对于分解这样一个简单的字符串是不需要的。

    例如:

    <?php
    
    $input = Array( 'post: 746', 'post: 2', 'post: 84');
    
    $output = array_map(function ($n) {
        $o = explode(': ', $n);
        return (int)$o[1];
    }, $input);
    
    var_dump($output);
    

    还有一种可能更快:

    <?php
    
    $input = Array( 'post: 746', 'post: 2', 'post: 84');
    
    $output = array_map(function ($n) {
        return (int)substr($n, strpos($n, ':')+1);
    }, $input);
    
    var_dump($output);
    

    如果输出中不需要整数,只需删除对int的强制转换。

    <?php
    
    $input = Array( 'post: 746', 'post: 2', 'post: 84');
    $output = str_replace('post: ', '', $input);
    
    var_dump($output);
    
        5
  •  0
  •   mickmackusa Tom Green    6 年前

    你可以用 array_map() 迭代数组,然后通过 filter_var() FILTER_SANITIZE_NUMBER_INT trim()

    你也可以 preg_replace() 为你做迭代。使用 替换()

    代码:( Demo )

    $array = ["post: 746", "post: 2", "post: 84"];
    
    // remove all non-integer characters
    var_export(array_map(function($v){return filter_var($v, FILTER_SANITIZE_NUMBER_INT);}, $array));
    
    // only necessary if you have elements with non-"post: " AND non-integer substrings
    var_export(preg_replace('~^post: ~', '', $array));
    
    // I shuffled the character mask to prove order doesn't matter
    var_export(array_map(function($v){return trim($v, ': opst');}, $array));
    

    输出:(每种技术都相同)

    array (
      0 => '746',
      1 => '2',
      2 => '84',
    )
    

    p、 如果有人想用 explode() 要为每个元素创建一个数组,然后将数组的第二个元素存储为新的所需字符串(我不会遇到这样的麻烦),请确保:

    1. : (冒号、空格)甚至 post: : (仅冒号)强制您整理第二个元素的前导空格,然后
    2. 使用 的第三个参数( limit 2 因为从逻辑上讲,你不需要两个以上的元素