[英]How to get part of string with Regular expression?
這是我的字符串: http : //www.something.com/path/tt 0425235 / somethingelse /
而且我只需要獲取“ 0425235”(在“ tt”和“ /”之間)
使用以下網址進行測試:http://rubular.com/
#http://www\.imdb\.com/title/tt(\d+)/#
簡單而完美;)
<?php
$match = array();
$url = 'http://www.something.com/path/tt0425235/somethingelse/bla/bla/bla/bla/?bla=bla';
preg_match( '/\/path\/([a-z]+([\d]+))+\/?/i' , $url , $match );
print_r( $match );
?>
再見!
$url = 'http://www.something.com/path/tt0425235/';
list($id) = preg_split('~^.*/tt(\d+)/$~', $url, 2, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) + array(NULL);
如果找不到您的模式,則$id
將為NULL
。
/http:\/\/[\w+-_\.]+\/path\/tt(\d+)\//
要么
~http://[\w+-_\.]+/path/tt(\d+)/~
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.