I want to selext url which user posted. So I want to use preg_match.
String:
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=350000
128/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=750000
500/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1000000
750/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1250000
1000/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1750000
1500/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
My PHP code:
<?php
$quality = $_POST['quality'];
$url = ''.$serviceUrl.'/'.$path.'';
$url = file_get_contents($url);
preg_match('/'.$quality.'\/prog_index.m3u8/', $url, $C);
print_r($C);
?>
Output:
Array
(
[0] => 1500/prog_index.m3u8
)
But I want like this: 1500/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94
Only based on your provided sample input, add \S+ which means to pick non-whitespace characters.
preg_match('/'.$quality.'\/prog_index.m3u8\S+/', $url, $C);
^^
Related
I'm trying to extract id, which is a whole number from a url, for example:
http://example.com/email/verify/106/8be57f01ac84747886acd7ae88c888112135fc7a
I'd like to extract only 106 from the string in PHP. The only dynamic variables in the URL would be the domain and the hash after 106/ The domain, id, and hash after 106/ are all dynamic.
I've tried preg_match_all('!\d+!', $url, $result), but it matches all number in string.
Any tips?
The pattern \b\d+\b might be specific enough here:
$url = "http://example.com/email/verify/106/8be57f01ac84747886acd7ae88c888112135fc7a";
preg_match_all('/\b\d+\b/', $url, $matches);
print_r($matches[0]);
This prints:
Array
(
[0] => 106
)
Try this. This will works.
$url = $_SERVER['REQUEST_URI']; //"http://example.com/email/verify/106/8be57f01ac84747886acd7ae88c888112135fc7a";
$str = explode('/',$url);
echo "<pre>";
print_r($str);
$id = $str[5]; // 106
Extract the value of the u2 parameter from this URL using a regular expression. http://www.example.com?u1=US&u2=HA853&u3=HPA
<?php
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA"; //my url
$pattern='/u2=[0-9A-Za-z]*/'; //R.E that url value is only digit/Alphabet
preg_match($pattern,$subject,$match);
print_r($match[0]);
?>
Output:-
u2=HA853
How can i retrieve only HA853?
The 0 group is everything that the regex matched so either use \K to ignore the previous matches of the regex,
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA"; //my url
$pattern='/u2=\K[0-9A-Za-z]*/'; //R.E that url value is only digit/Alphabet
preg_match($pattern,$subject,$match);
print_r($match[0]);
or use a second capture group:
...
$pattern='/u2=([0-9A-Za-z]*)/'; //R.E that url value is only digit/Alphabet
...
print_r($match[1]);
Why you'd need to do that though is unclear to me, http://php.net/manual/en/function.parse-str.php, seems like a simpler approach.
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA";
parse_str($subject, $output);
echo $output['u2'];
Demo: https://3v4l.org/gR4cb
Other way is to use parse_url,http://php.net/manual/en/function.parse-url.php
$subject="http://www.example.com?u1=US&u2=HA853&u3=HPA";
$query_string = parse_url($subject, PHP_URL_QUERY); // get query string
$parameters = explode('&', $query_string); //Explode with &
$array = array(); // define an empty array
foreach($parameters as $val)
{
$param= explode('=', $val);
$array[$param[0]] = $param[1];
}
echo $array['u2']; // outputs HA853
print_r($array);
Array
(
[u1] => US
[u2] => HA853
[u3] => HPA
)
I have this string
http://myipaddress:myport/mycompanyname/morethings?lovelyparameter
I want to take the word mycompanyname
any help?
I tried this:
$indexName = preg_match("http://p+:p+/","http://myipaddress:myport/mycompanyname/morethings?lovelyparameter" );
but I got this error:
preg_match(): Delimiter must not be alphanumeric or backslash
In case you don't want the preg functions, and something else from the url, you can use parse_url(). It would look like this:
$a = 'http://myipaddress:8080/mycompanyname/morethings?lovelyparameter';
$b = parse_url($a);
print_r($b);
Output:
Array
(
[scheme] => http
[host] => myipaddress
[port] => 8080
[path] => /mycompanyname/morethings
[query] => lovelyparameter
)
That way, just use something like:
$path = $b['path'];
$foo = explode('/', $path)[1];
echo $foo;
Output:
mycompanyname
Side notes:
This code won't check for malformed url, so you should do some check of your own.
If you test the url with a port number as string (as you have in the question), it won't work.
It could be done in one line:
$url = 'http://myipaddress:8080/mycompanyname/morethings?lovelyparameter';
echo explode('/', parse_url($url, PHP_URL_PATH))[1];
Output:
mycompanyname
You can use explode as
$abc = 'http://myipaddress:myport/mycompanyname/morethings?lovelyparameter';
$a = explode('/', $abc);
echo '<pre>';
print_r($a[3]);
echo '</pre>';
The explode breaks the strings into parts and returns an array of strings so you can check in array too for mycompanyname..
For the records, you were missing appropriate delimiters. A regex solution would be:
https?://.+?/(?P<company>[^/]+)/
In PHP this would be:
$regex = '~https?://.+?/(?P<company>[^/]+)/~';
$url = 'http://myipaddress:8080/mycompanyname/morethings?lovelyparameter';
preg_match($regex, $url, $match);
echo $match["company"];
// mycompanyname
I'm beginner in php and I have string like this:
$test = http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
And I want to split string to array like this:
Array(
[0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jpg
[1] => http://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)
What should I do?
$test = 'http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg';
$testurls = explode('http://',$test);
foreach ($testurls as $testurl) {
if (strlen($testurl)) // because the first item in the array is an empty string
$urls[] = 'http://'. $testurl;
}
print_r($urls);
You asked for a regex solution, so here you go...
$test = "http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg";
preg_match_all('/(http:\/\/.+?\.jpg)/',$test,$matches);
print_r($matches[0]);
The expression looks for parts of the string the start with http:// and end with .jpg, with anything in between. This splits your string exactly as requested.
output:
Array
(
[0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jpg
[1] => http://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)
you can split them if they are always like this vith substr() function reference: http://php.net/manual/en/function.substr.php but if they are dynamic in lenght. you need to get a ; or any other sign that is not likely to be used there before 2nd "http://" and then use explode function reference: http://php.net/manual/en/function.explode.php
$string = "http://something.com/;http://something2.com"; $a = explode(";",$string);
Try the following:
<?php
$temp = explode('http://', $test);
foreach($temp as $url) {
$urls[] = 'http://' . $url;
}
print_r($urls);
?>
$test = 'http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jp';
array_slice(
array_map(
function($item) { return "http://" . $item;},
explode("http://", $test)),
1);
For answering this question by regular expression I think you want something like this:
$test = "http://localhost/biochem/wp-content/uploads//godzilla-article2.jpghttp://localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg";
$keywords = preg_split("/.http:\/\//",$test);
print_r($keywords);
It returns exactly something you need:
Array
(
[0] => http://localhost/biochem/wp-content/uploads//godzilla-article2.jp
[1] => localhost/biochem/wp-content/uploads/life-goes-on-wpcf_300x111.jpg
)
I have this string
$url = offer?offer_id={{offer_category_id}}&item{{offer_title}}
Is there a way how I can create a php array with the text inside the {{ }} thus resulting an array similar to
$array[0] = 'offer_category_id'
$array[1] = 'offer_title'
This is what I have but its not working as wanted
preg_match("/{{([^\"]*)\}}/", $url , $cols);
This code will give your values:
$str = 'offer?offer_id={{offer_category_id}}&item{{offer_title}}';
if ( preg_match_all('~{\s*{([^}]*)}~i', $str, $m) )
print_r ( $m[1] );
OUTPUT:
Array
(
[0] => offer_category_id
[1] => offer_title
)
Use preg_match_all:
preg_match_all("/{{([^\"}]+)\}}/", $url , $cols);
i think it should be (you might need to play with the flag to get the desired order):
preg_match_all("/{{([^}]+)\}}/", $url , $cols, PREG_SET_ORDER);