[英]Regular expression for extracting certain URLs?
我已经尽力了,但正则表达式并不是我的菜。 :(
我需要提取以某个文件扩展名结尾的某些 URL。 例如,我希望能够解析一个大段落并提取所有以*.txt
结尾的 URL。 例如,
Lorem ipsum dolor sit amet, consectetur adipiscing elit。 Nulla hendrerit aliquet erat at ultrices。 Donec eu nunc nec nibh http://www.somesite.com/somefolder/blahblah/etc/something.txt iaculis dictum。 Quisque nisi neque,vulputate quis pellentesque blandit,faucibus eget nisl。
我需要能够从上述段落中删除 http://www.somesite.com/somefolder/blahblah/etc/something.txt但要提取的 URL 数量会有所不同。 它将根据用户输入的内容是动态的。 它可以有 3 个以*.txt
结尾的链接和 3 个不以*.txt
结尾的链接。 我只需要提取那些以*.txt
结尾的内容。 谁能给我我需要的代码?
您可以使用/(?<=\s)http:\/\/\S+\.txt(?=\s)/
找到您需要的内容
意思是:
假设这些都是正确的 URL,那么它们中不会有任何空格。 我们可以利用这一事实使正则表达式变得非常简单:
preg_match_all("/([^ ]+\.(txt|doc))/i", $text, $matches);
// ([^ ]+ Match anything, except for a space.
// \. A normal period.
// (txt|doc) The word "txt" or "doc".
// )/i Case insensitive (so TXT and TxT also work)
如果您不需要匹配多个文件扩展名,那么您可以将“(txt|doc)”更改为“txt”。
$matches
将包含许多 arrays,您需要密钥编号 0 或 1。为了使数组更易于阅读,您可以使用:
preg_match_all("/(?P<matched_urls>[^ ]+\.(txt|doc))/i", $text, $matches);
这将使$matches
看起来像这样:
array([0] => array(), [1] => array(), [2] => array(), ["matched_urls"] => array());
应该很明显您需要哪个密钥。
怎么样:
$str = 'Lorem ipsum dolor sit amet. Donec eu nunc nec nibh http://www.somesite.com/somefolder/blahblah/etc/something.txt. Lorem ipsum dolor sit amet. Donec eu nunc nec nibh http://www.somesite.com/somefolder/blahblah/etc/something.doc.';
preg_match_all('#\b(http://\S+\.txt)\b#', $str, $m);
解释:
# : regex delimiter
\b : word boundary
( : begin capture group
http:// : litteral http://
\S+ : one or more non space
\. : a dot
txt : litteral txt
) : end capture group
\b : word boundary
# : regex delimiter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.