Rajdeep Rath
Rajdeep Rath

Reputation: 81

Trouble with finding regex expression in php

I a newbie to regx. I am unable to get things right with his. Can some one help me with this. I have a web page source code, where in i need to locate this pattern:

http://cdn1b.mobile.website.com/videos/201102/18/174092/240P_245K_174092.mp4?rs=125&ri=600&s=1394217550&e=1394228350&h=b99d1d9d38da8ba3ab99601de0cf794e

I need to get only one instance of this even if there are more. But i am getting selection from first http in the page to the last mp4?rs=125&ri=600&s=1394217550&e=1394228350&h=b99d1d9d38da8ba3ab99601de0cf794e

in the page. I am using php.

Edited: This is what i was trying. (sorry if its stupid)

(http(s?):).*\.(mp4|flv|mkv|avi)(\?rs=[A-Za-z0-9=]+).*.(ri=[A-Za-z0-9=]+).*.(s=[A-Za-z0-9=]+).*.(e=[A-Za-z0-9=]+).*.(h=[A-Za-z0-9=]+)

Edited: Here is a pastebin of what i am getting with my expression

http://pastebin.com/trmNzMti

Upvotes: 1

Views: 91

Answers (2)

Sparkup
Sparkup

Reputation: 3754

This should do it :

preg_match_all("/(http(s?):)([^\s]+)\.(mp4|flv|mkv|avi)(\?rs=[A-Za-z0-9=]+)([^\s]+)(ri=[A-Za-z0-9=]+)([^\s]+)(s=[A-Za-z0-9=]+)([^\s]+)(e=[A-Za-z0-9=]+)([^\s]+)(h=[A-Za-z0-9=]+)/", $html, $matches, PREG_SET_ORDER);

// each occurrence
foreach ($matches as $val) {
    echo "matched: " . $val[0] . "\n";
}

// first occurrence
echo $matches[0][0]

Changed * to ([^\s]+) matches anything except spaces, you can add other characters you wish to exclude between desired matches.

Upvotes: 2

Javad
Javad

Reputation: 4397

If you want to find a url in string by regExp check this link which has a full patterns for different requests

If you have the url string and want to find a parameter in the query string use parse_url() command parse_url

Example:

$query = parse_url('http://cdn1b.mobile.website.com/videos/201102/18/174092/240P_245K_174092.mp4?rs=125&ri=600&s=1394217550&e=1394228350&h=b99d1d9d38da8ba3ab99601de0cf794e');
//to get whatever after http or https and before the filename in url you can use this:
$specifict_section = $query['host'].str_replace(basename($query['path'], '', $query['path']));

$query_parts = explode('&', $query['query']);
$params = array(); 
foreach ($query_parts as $param) { 
    $item = explode('=', $param); 
    $params[$item[0]] = $item[1]; 
} 

// Do your stuffs with $params
print_r( $params );

Upvotes: 1

Related Questions