在php中使用正则表达式找到两个其他字符串之间的字符串?

问题描述:

在php中是否有一个函数来搜索一个字符串,并找到一个基于它在两个定义的字符集之间的子字符串?在php中使用正则表达式找到两个其他字符串之间的字符串?

我想用php脚本搜索几个文件,并从中抓取一个字符串。

字符串我需要的是一个URL,每个文件都有一个字符串,它看起来像这样用不同的网址内

/A << /URI (https://www.website/custom_ url_per_file) 
/S /URI >> //an actual linebreak in the code 

我都在foreach循环打开文件,所以,我怎么能提取2个字符串$ a和$ b之间的url?

$a = /A << /URI 
$b = /S /URI >> 
+0

你要寻找的 “非贪婪捕获组”:'/ $前缀后缀$ /'(+?)。 – georg 2014-09-23 10:04:29

<?php 

$string = '/A << /URI (https://www.website/custom_ url_per_file) 
/S /URI >> //an actual linebreak in the code'; 

preg_match('#/A << /URI \(([^\)]+)\)\s+\/S /URI >>#', $string, $matches); 

print_r($matches); 

?> 

将导致到:

Array 
(
[0] => /A << /URI (https://www.website/custom_ url_per_file) 
/S /URI >> 
[1] => https://www.website/custom_ url_per_file 
) 
+0

谢谢allot voldomazta :)) – Peter 2014-09-23 13:41:06