获取特定字符串,包括特定字符之间的特定字 - php

时间:2015-05-23 13:28:29

标签: php

我是php的初学者,我不知道如何解决这个问题。

我有包含段落和链接的文本文件,这是来自文本文件的示例:

$string = ("Lorem 'Ipsum' is simply: dummy 'http://www.exmplelink1.com/blah/blah/file 1 b.txt' text of the printing 'http://www.exmplelink1.com/blah/blah/file 1 c.txt' and typesetting industry. Lorem Ipsum 'http://www.exmplelink2.com/blah/file.txt' has been, the 'industry's standard'");

请注意字符串中有许多单引号和两个域名:exmplelink1和exmplelink2

  • 我如何才能获得exmplelink1链接(单引号之间的完整链接)在这种情况下有两个链接:

    'http://www.exmplelink1.com/blah/blah/file 1 b.txt'
    'http://www.exmplelink1.com/blah/blah/file 1 c.txt'
    

谢谢你的帮助:)

1 个答案:

答案 0 :(得分:1)

这将仅适用于 exmplelink1 ,使用file_get_contents将文件数据读取为字符串&然后使用下面的代码来获得所需 exmplelink1

$re = "/'http:\\/\\/www\\.exmplelink1.*?'/m"; 
$str = ("Lorem 'Ipsum' is simply: dummy 'http://www.exmplelink1.com/blah/blah/file 1 b.txt' text of the printing 'http://www.exmplelink1.com/blah/blah/file 1 c.txt' and typesetting industry. Lorem Ipsum 'http://www.exmplelink2.com/blah/file.txt' has been, the 'industry's standard'"); 

preg_match_all($re, $str, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';

请参阅正则表达式https://regex101.com/r/gE5uX6/2