Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:从数组中的值中删除正则表达式字符串_Php_Arrays_Regex - Fatal编程技术网

PHP:从数组中的值中删除正则表达式字符串

PHP:从数组中的值中删除正则表达式字符串,php,arrays,regex,Php,Arrays,Regex,我有一个数组$text: Array ( [0] => "(This is so cool!)" [1] => "look at @[48382669205:275:JAY Z] and @[28940545600:27:Beyonc\303\251]'s page" [2] => "@[30042869909:32:Van Jones] on Bowe Bergdahl drama" ) 我想匹配删除@[0-9]文本,因此我的输出如下所示:

我有一个数组
$text

Array
(
    [0] => "(This is so cool!)"
    [1] => "look at @[48382669205:275:JAY Z] and @[28940545600:27:Beyonc\303\251]'s page"
    [2] => "@[30042869909:32:Van Jones] on Bowe Bergdahl drama"    
)
我想匹配删除@[0-9]文本,因此我的输出如下所示:

Array
(
    [0] => "(This is so cool!)"
    [1] => "look at JAY Z and Beyonc\303\251's page"
    [2] => "Van Jones on Bowe Bergdahl drama"    
)

我尝试了很多方法(preg_replace等),但是这个正则表达式太复杂了,我无法得到我想要的结果!非常感谢您的帮助。

您可以用
\1
替换
/\[\d+:\d+:([^\]+)\]/

<?php

$array = array(
    "(This is so cool!)",
    "look at @[48382669205:275:JAY Z] and @[28940545600:27:Beyonc\303\251]'s page",
    "@[30042869909:32:Van Jones] on Bowe Bergdahl drama"    
);

$array = preg_replace('/@\[\d+:\d+:([^\]]+)\]/', '\1', $array);

print_r($array);

正则表达式尸检

Array
(
    [0] => (This is so cool!)
    [1] => look at JAY Z and Beyoncé's page
    [2] => Van Jones on Bowe Bergdahl drama
)

  • @
    -符号处的文字
  • \[
    -文字
    [
    字符-您需要将其转义为
    [
    是正则表达式字符
  • \d+
    -匹配1到无限次的数字
  • -文字冒号
  • \d+
    -匹配1到无限次的数字
  • -文字冒号
  • ([^\]]+)
    -匹配任何非文字字符的匹配组
    ]
    字符匹配1到无限次(意味着它将匹配,直到它匹配到
    ]
    ):
  • \]
    -文字
    ]
    字符
    • 一点也不难

      <?php
      $array = array(
          "(This is so cool!)",
          "look at @[48382669205:275:JAY Z] and @[28940545600:27:Beyonc\303\251]'s page",
          "@[30042869909:32:Van Jones] on Bowe Bergdahl drama"    
      );
      $pattern = '#@\[(\d+:){2}(.*?)\]#';
      
      $result[$k] = preg_replace($pattern, "$2", $array);
      

      请参阅needed[1]=>“看看JAY Z和碧昂斯的页面”@brotheryura这只是因为
      已展开。+1撤回了我的答案,因为我没有正确阅读问题。无需复制您的伟大作品。:)<代码>数组映射
      不是必需的,
      preg\u replace
      可用于数组。然而+1。@M42很好的调用-在这段代码中绝对没有理由使用
      array\u map
      。preg\u replace可以对数组进行操作并返回数组。使用数组而不是for循环涉及的代码更少,运行速度更快,因此my-1