PHP-数组上的preg_replace无法正常工作

时间:2018-11-11 17:14:15

标签: php

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

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

我正在尝试从数组中的每个项目中删除post:并返回一个看起来像这样的项目...

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

我试图像这样使用preg_replace ...

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

但这对我不起作用,我应该怎么做?

5 个答案:

答案 0 :(得分:1)

您错过了preg_replace函数的第二个参数,该参数应替换匹配项,而您的正则表达式也有小问题,这是固定版本:

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

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

答案 1 :(得分:0)

您没有替换图案,也没有空的字符串占位符。

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

这就是您要尝试做的(还有其他args,但它们是可选的)。

$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
)

答案 2 :(得分:0)

有很多方法不涉及正则表达式,而实际上不需要像这样的简单字符串。

例如:

<?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。

或者仅使用str_replace,在许多情况下,这就像替换preg_replace一样。

<?php

$input = Array( 'post: 746', 'post: 2', 'post: 84');
$output = str_replace('post: ', '', $input);

var_dump($output);

答案 3 :(得分:0)

您可以在不使用正则表达式的情况下使用array_mapsubstr来检查前缀并返回不带前缀的字符串来做到这一点:

$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)

You can use array_map() to iterate the array then strip out any non-digital characters via filter_var() with FILTER_SANITIZE_NUMBER_INT or trim() with a "character mask" containing the six undesired characters.

You can also let preg_replace() do the iterating for you. Using preg_replace() offers the most brief syntax, but regular expressions are often slower than non-preg_ techniques and it may be overkill for your seemingly simple task.

Codes: (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));

Output: (from each technique is the same)

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

p.s. If anyone is going to entertain the idea of using explode() to create an array of each element then store the second element of the array as the new desired string (and I wouldn't go to such trouble) be sure to:

  1. split on or : (colon, space) or even post: (post, colon, space) because splitting on : (colon only) forces you to tidy up the second element's leading space and
  2. use explode()'s 3rd parameter (limit) and set it to 2 because logically, you don't need more than two elements