简体   繁体   English

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

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

Is there a function in php to search a string and find a sub string based on it being between two defined character sets? php中是否有一个功能可以搜索字符串并基于它在两个定义的字符集之间找到子字符串?

I am trying to search through a few files using a php script and grab a string from them. 我正在尝试使用php脚本搜索一些文件并从中获取字符串。

the string i need is a URL, each file has a string that looks like this with a different url inside 我需要的字符串是一个URL,每个文件都有一个看起来像这样的字符串,其中包含不同的URL

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

I have all the files opening in a foreach loop, so how can i extract the url between the 2 strings $a and $b? 我在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);

?>

will result to: 将导致:

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.

相关问题 正则表达式仅在其他两个特定字符串之间查找特定字符串 - Regex find specific string only between two other specific strings PHP正则表达式在两个字符串之间找到一个字符串 - php regex finding a string between two strings 如何在PHP中使用正则表达式查找和替换两个标签之间的字符串? - How to find and replace string between two tags using regex in PHP? 正则表达式/ BBCode解析器以查找两个字符串之间的字符串(两个字符串之间)并将其替换 - Regex / BBCode Parser to find string between two strings (Between two strings) and replace it 提取一个字符串,该字符串与PHP中其他两个字符串之间的区别 - Extract a string that is the difference between two other strings in PHP 如何使用PHP中的正则表达式在其他两个字符串之间获取字符串? - How I get the string between two other strings using regular expressions in PHP? PHP-使用正则表达式从两个字符串之间提取 - php - Extracting from between two strings using regex PhP 在两个不同的字符串之间查找(并替换)字符串 - PhP Find (and replace) string between two different strings 两个字符串之间的正则表达式 - 包括最后一个字符串 - Regex between two strings - include last string 如何在其他两个已知字符串之间匹配一个字符串,而使用REGEX则不能匹配其他字符串? - How can I match a string between two other known strings and nothing else with REGEX?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM