【问题标题】:get specific string that include specific word between specific characters - php获取包含特定字符之间特定单词的特定字符串 - php
【发布时间】:2015-05-23 13:28:29
【问题描述】:

我是 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'
    

谢谢你的帮助:)

【问题讨论】:

  • 使用正则表达式。像'http.*?' 这样的东西应该可以工作。
  • 我可能会补充,我真的不知道正则表达式:(
  • 我应该替换 '?'与域名?因为我有太多不同域名的链接,我只想获取特定域的链接

标签: php


【解决方案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

【讨论】:

  • 我可以建议在模式中转义主机名中的点吗?这一次并不重要,但一般来说......
猜你喜欢
  • 2016-05-05
  • 2012-10-22
  • 2014-09-23
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 2017-08-02
  • 1970-01-01
相关资源
最近更新 更多