繁体   English   中英

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

[英]Find a string between two other strings using a regex in php?

php中是否有一个功能可以搜索字符串并基于它在两个定义的字符集之间找到子字符串?

我正在尝试使用php脚本搜索一些文件并从中获取字符串。

我需要的字符串是一个URL,每个文件都有一个看起来像这样的字符串,其中包含不同的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 >>  
<?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
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM