替换任何正则表达式php

时间:2014-09-02 19:13:42

标签: php regex str-replace

我在php中有这个正则表达式

$array_item_aux = str_replace('/.*PUBMED=/',"",$array_item);
它应该取代这个文本 ( - | ENSR00001252129 | RegulatoryFeature | regulatory_region_variant | - | - | - | - | - | PUBMED = 21499247

用这个

21499247

我做错了什么

3 个答案:

答案 0 :(得分:2)

或者,如果您显示的字符串是整个字符串,则可以使用explode:

$array_item_aux = explode('PUBMED=', $array_item)[1];

如果您的PHP版本对于此语法而言太旧(<5.4),则可以改为使用:

$tmp = explode('PUBMED=', $array_item);
$array_item_aux = $tmp[1];

或@Sam建议:

list(, $array_item_aux) = explode('PUBMED=', $array_item);

答案 1 :(得分:1)

str_replace不使用正则表达式,请使用preg_replace

$array_item_aux = preg_replace('/.*?PUBMED=/', "", $array_item);

答案 2 :(得分:0)

您应该通过添加.*来制作? lazy instead of a greedy;但是你的主要问题是str_replace()不允许使用正则表达式进行搜索,而是使用preg_replace()

$array_item_aux = preg_replace('/.*?PUBMED=/', '', $array_item);