简体   繁体   English

我的网址的preg_match()正则表达式模式

[英]preg_match() regex pattern for my url

I tried to make a pattern for this url: 我试图为此网址制作一个模式:

http://media.network.com/flv/ea03addc753ff8d80f6be4b49a414cfd/4f3d2012/5016.flv

I just want to retrive the numbers 5016 - fileid from the url.. so i used this code 我只想从网址中检索数字5016- fileid ..所以我使用了这段代码

preg_match( '#http://media\.gilmanetwork\.com/flv/([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)\.flv#', $markup, $matches );

so if i use $matches[3] , I should get the number 5016 right? 所以如果我使用$matches[3] ,我应该得到数字5016对吗?

You've got a big mistake there. 你那里有个大错误。 gilmanetwork versus network . gilmanetworknetwork Try 尝试

'#http://media.network.com/flv/([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)\.flv#'

Which results in 导致

<?php
$markup = "http://media.network.com/flv/ea03addc753ff8d80f6be4b49a414cfd/4f3d2012/5016.flv";
preg_match( '#http://media.network.com/flv/([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)\.flv#', $markup, $matches );
print_r($matches);
// Array ( [0] => http://media.network.com/flv/ea03addc753ff8d80f6be4b49a414cfd/4f3d2012/5016.flv [1] => ea03addc753ff8d80f6be4b49a414cfd [2] => 4f3d2012 [3] => 5016 ) 

Take a look at parse_url() , combined with pathinfo() : 看一看parse_url() ,结合pathinfo()

$url      = 'http://media.network.com/flv/ea03addc753ff8d80f6be4b49a414cfd/4f3d2012/5016.flv';
$filename = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_FILENAME);

echo $filename;  // 5016

This approach has an advantage over regular expressions in that the format of the URL can change - and potentially pretty extensively – but so long as the filename of the resource is its ID number, this code will always produce the correct result. 与正则表达式相比,此方法的优势在于URL的格式可以更改(并且可能会更改得很广泛),但是只要资源的文件名是其ID号,此代码始终会产生正确的结果。

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

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