2011-07-26 53 views
1

我有一个字符串,它看起来像:正则表达式< >之间

Pretext<thecontentineed> 

我想写一个正则表达式,将使用的preg_match

我已经试过拉‘thecontentineed’从字符串:

$string = "Pretext<thecontentineed>";  
preg_match("/<.*?>/" , $string, $output); 

但是,返回一个空的数组。

回答

7
$string = "Pretext<thecontentineed>";  
preg_match("/<(.*?)>/" , $string, $output); 

您与()忘了()

3
if (preg_match('/<(.*?)>/', $string, $output)) { 
    echo $output[1]; 
} 
3
$string = "Pretext<thecontentineed>"; 
preg_match("/\<([^>]+)\>/" , $string, $output); 
print_r($output); 
0
if (preg_match('/Pretext<(.*?)>/', $string, $output)) { 
    echo $output[1]; 
} 
1

您还没有指定任何捕获组:

preg_match('/<(.*)?>/', $string, $matches); 

的()指示正则表达式模式以捕捉括号内的任何匹配,并且st将它们放入$ matches数组中。

相关问题