I have thousands of urls which have ids i want to get only ids from url for example
This is my array
Array
(
[0] => http://www.videoweed.es/file/f62f2bc536bad
[1] => http://www.movshare.net/video/5966fcb2605b9
[2] => http://www.nowvideo.sx/video/524aaacbd6614
[3] => http://vodlocker.com/pbz4sr6elxmo
)
I want ids from above links
f62f2bc536bad
5966fcb2605b9
524aaacbd6614
pbz4sr6elxmo
I have use parse_url function but its return me path which include all things after slash(/) like /file/pbz4sr6elxmo
<?php
foreach($alllinks as $url){
$parse = parse_url($url);
echo $parse['path'];
}
?>
Output
/pbz4sr6elxmo
/video/5966fcb2605b9
/file/f62f2bc536bad
/video/524aaacbd6614
You can try with explode -
$alllinks = array
(
'http://www.videoweed.es/file/f62f2bc536bad',
'http://www.movshare.net/video/5966fcb2605b9',
'http://www.nowvideo.sx/video/524aaacbd6614',
'http://vodlocker.com/pbz4sr6elxmo'
);
foreach($alllinks as $url){
$temp = explode('/', $url);
echo $temp[count($temp) - 1].'<br/>';
}
Output
f62f2bc536bad
5966fcb2605b9
524aaacbd6614
pbz4sr6elxmo
This will only help if the the url structure is same, i.e. the last part is the id
If the URLs always ends with the id you can simply do
$url = 'http://www.videoweed.es/file/f62f2bc536bad';
$url_split = explode('/', $url);
$code = $url_split[count($url_split) - 1];
Try this:
$alllinks = array(
'http://www.videoweed.es/file/f62f2bc536bad',
'http://www.movshare.net/video/5966fcb2605b9',
'http://www.nowvideo.sx/video/524aaacbd6614',
'http://vodlocker.com/pbz4sr6elxmo'
);
foreach($alllinks as $url){
$parts = explode('/', $url);
echo end($parts).'<br/>';
}
Related
So I wrote some code that should simply take this
title=title&description=description&image=(some image here)&color=ff0000
And return this
echo $meta["title"] //title
echo $meta["description"] //description
echo $meta["image"] //(some image here)
echo $meta["color"] //ff0000
Instead, it only returns title for some reason...
<?php
$url = $_SERVER["REQUEST_URI"];
$url = substr($url, 2);
$url = base64_decode($url);
// title=title&description=description&image=(some image here)&color=ff0000
// confusing part
parse_str($url, $meta);
?>
What about smth like this?
$string = 'title=title&description=description&image=(some image here)&color=ff0000';
$array = explode("&", $string);
$finalArray = [];
foreach ($array as $arr) {
$singleArr = explode("=",$arr);
$finalArray[$singleArr[0]] = $singleArr[1];
}
print("<pre>".print_r($finalArray,true)."</pre>");
Which will print out:
Array
(
[title] => title
[description] => description
[image] => (some image here)
[color] => ff0000
)
Instead of reinventing the wheel :) (thx #u_mulder)
$urlString = parse_url($string, PHP_URL_QUERY);
parse_str($urlString, $urlArray);
print_r($urlArray["image"]); // Or what ever paramneter, like
foreach($urlArray as $key => $value) {
echo $urlArray[$key];
}
I need to compare URLs and remove duplicates from array but I want compare only host from url. I need skip http and https and www and others like last slash when I compare.
So when I have array:
$urls = array(
'http://www.google.com/test',
'https://www.google.com/test',
'https://www.google.com/example',
'https://www.facebook.com/example',
'http://www.facebook.com/example');
Result will be only
http://www.google.com/test
http://www.google.com/example
http://www.facebook.com/example
I tried to compare like :
$urls = array_udiff($urls, $urls, function ($a, $b) {
return strcmp(preg_replace('|^https?://(www\\.)?|', '', rtrim($a,'/')), preg_replace('|^https?://(www\\.)?|', '', rtrim($b,'/')));
});
But it return me empty array.
<?php
$urls = array(
'http://www.google.com/test',
'https://www.google.com/test',
'https://www.google.com/example',
'https://www.facebook.com/example',
'http://www.facebook.com/example');
$MyArray = [];
for($i=0;$i<count($urls);$i++) {
preg_match_all('/www.(.*)/', $urls[$i], $matches);
if (!in_array($matches[1], $MyArray))
$MyArray[] = $matches[1];
}
echo "<pre>";
print_r($MyArray);
echo "</pre>";
And the output is
Array
(
[0] => Array
(
[0] => google.com/test
)
[1] => Array
(
[0] => google.com/example
)
[2] => Array
(
[0] => facebook.com/example
)
)
trimmed and keeping only the host name
Try this approach :
<?php
function parseURLs(array $urls){
$rs = [];
foreach($urls as $url){
$segments = parse_url($url);
if(!in_array($segments['host'], $rs))
$rs[] = $segments['host'];
}
return $rs;
}
Then :
<?php
$urls = array(
'http://www.google.com',
'https://www.google.com',
'https://www.google.com/',
'https://www.facebook.com',
'http://www.facebook.com'
);
$uniqueURLs = parseURLs($urls);
print_r($uniqueURLs);
/* result :
Array
(
[0] => www.google.com
[1] => www.facebook.com
)
*/
You need to Loop through the URL's, Parse URL with PHP's url_parse() function and use array_unique to remove duplicates from array, so we are checking both the host and path ..
I have written a class for you:
<?php
/** Get Unique Values from array Values **/
Class Parser {
//Url Parser Function
public function arrayValuesUrlParser($urls) {
//Create Container
$parsed = [];
//Loop Through the Urls
foreach($urls as $url) {
$parse = parse_url($url);
$parsed[] = $parse["host"].$parse["path"];
//Delete Duplicates
$result = array_unique($parsed);
}
//Dump result
print_r($result);
}
}
?>
Using the Class
<?php
//Inlcude tghe Parser
include_once "Parser.php";
$urls = array(
'http://www.google.com/test',
'https://www.google.com/test',
'https://www.google.com/example',
'https://www.facebook.com/example',
'http://www.facebook.com/example');
//Instantiate
$parse = new Parser();
$parse->arrayValuesUrlParser($urls);
?>
You can do it in one file if you don't need to seperate files but you will have to remove include_once if you are using one php file. This class is also on PHP Classes, did it for fun !
Best of Luck !
As the title suggests, my goal is to add a rewrite statement in .htaccess to replace a dynamic substring in the middle of a URL. Additionally, I need all occurrences of "|" to be changed to "+".
For instance, I'd like to take this URL
www.domain.com/?t=p&c=some+string&skus=string1|string2|string3
And redirect it to:
www.domain.com/?q=string1+string2+string3
Basically, the substring to replace starts at "t=" and ends at "skus".
Is this possible? Is this easier to do with PHP?
You can use the parse_url function
this would do the trick
$url = "www.domain.com/?t=p&c=some+string&skus=string1|string2|string3";
echo "<pre>";
print_r($decoded = parse_url($url));
print_r($parsed = queryToArray($decoded['query']));
$parms = explode("|", $parsed['skus']);
print_r($parms);
echo "</pre>";
function queryToArray($qry)
{
$result = array();
//string must contain at least one = and cannot be in first position
if(strpos($qry,'=')) {
if(strpos($qry,'?')!==false) {
$q = parse_url($qry);
$qry = $q['query'];
}
}else {
return false;
}
foreach (explode('&', $qry) as $couple) {
list ($key, $val) = explode('=', $couple);
$result[$key] = $val;
}
return empty($result) ? false : $result;
}
the result should look like this
Array
(
[path] => www.domain.com/
[query] => t=p&c=some+string&skus=string1|string2|string3
)
Array
(
[t] => p
[c] => some+string
[skus] => string1|string2|string3
)
Array
(
[0] => string1
[1] => string2
[2] => string3
)
Use PHP built-in function when possible. So in this case use parse_url and parse_str.
$parts = parse_url($url);
parse_str($parts['query'], $query);
$params = explode('|', $query['skus']);
could any one show me how in php i can get different bitrate(resolution) sub m3u8 urls if we have the main playlist m3u8 using get_data method?The following is data i have from get_data method but i want to get m3u8 urls for each resolution. Could any one show me how this can be done?Thanks in advance.
$returned_content = get_data(''.$m3u8Url);
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
main playlist m3u8:
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1628000,RESOLUTION=852x480,CODECS="avc1.77.30,mp4a.40.2"
http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/erewewrwrtf34324343443243434344/test1.mpegts/playlist-dfasdfasdfaw4q3243241.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=128000,RESOLUTION=256x144,CODECS="avc1.66.30,mp4a.40.2"
http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/fgdhgfhgjhghfdsdf45454545345435/test1.mpegts/playlist-adfdfghgjdt5t45454542.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=464000,RESOLUTION=426x240,CODECS="avc1.77.30,mp4a.40.2"
http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/764563564565445fsdf4r3dfdfdffdf/test1.mpegts/playlist-eertyeryry564534rrtr3.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=828000,RESOLUTION=640x360,CODECS="avc1.77.30,mp4a.40.2"
http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/fgsfdgdfgfdg5435345456745264554/test1.mpegts/playlist-fgsfghdghjt4353454544.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2128000,RESOLUTION=1024x576,CODECS="avc1.77.30,mp4a.40.2"
http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/sfdgsdfgfdgfdgfdgfd465436546576/test1.mpegts/playlist-fghdjhygjujdfgsaf4455.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=3692000,RESOLUTION=1280x720,CODECS="avc1.64001f,mp4a.40.2"
http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/sfdghgjyuktyurty546565466453645/test1.mpegts/playlist-safdghhgfjjyj45345546.m3u8
First off, you need to get data source, then process them (explode() the values, as your sample data is in line breaks), then group them by two's, and in the end loop them. Consider this example:
<?php
$curl_output = '#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1628000,RESOLUTION=852x480,CODECS="avc1.77.30,mp4a.40.2"
http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/erewewrwrtf34324343443243434344/test1.mpegts/playlist-dfasdfasdfaw4q3243241.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=128000,RESOLUTION=256x144,CODECS="avc1.66.30,mp4a.40.2"
http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/fgdhgfhgjhghfdsdf45454545345435/test1.mpegts/playlist-adfdfghgjdt5t45454542.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=464000,RESOLUTION=426x240,CODECS="avc1.77.30,mp4a.40.2"
http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/764563564565445fsdf4r3dfdfdffdf/test1.mpegts/playlist-eertyeryry564534rrtr3.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=828000,RESOLUTION=640x360,CODECS="avc1.77.30,mp4a.40.2"
http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/fgsfdgdfgfdg5435345456745264554/test1.mpegts/playlist-fgsfghdghjt4353454544.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2128000,RESOLUTION=1024x576,CODECS="avc1.77.30,mp4a.40.2"
http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/sfdgsdfgfdgfdgfdgfd465436546576/test1.mpegts/playlist-fghdjhygjujdfgsaf4455.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=3692000,RESOLUTION=1280x720,CODECS="avc1.64001f,mp4a.40.2"
http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/sfdghgjyuktyurty546565466453645/test1.mpegts/playlist-safdghhgfjjyj45345546.m3u8';
// process the string
$pieces = explode("\n", $curl_output); // make an array out of curl return value
unset($pieces[0]); // remove #EXTM3U
$pieces = array_map('trim', $pieces); // remove unnecessary space
$pieces = array_chunk($pieces, 2); // group them by two's
?>
Formatted pieces should look something like this:
Array
(
[0] => Array
(
[0] => #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1628000,RESOLUTION=852x480,CODECS="avc1.77.30,mp4a.40.2"
[1] => http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/erewewrwrtf34324343443243434344/test1.mpegts/playlist-dfasdfasdfaw4q3243241.m3u8
)
[1] => Array
(
[0] => #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=128000,RESOLUTION=256x144,CODECS="avc1.66.30,mp4a.40.2"
[1] => http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/fgdhgfhgjhghfdsdf45454545345435/test1.mpegts/playlist-adfdfghgjdt5t45454542.m3u8
)
...
Then, on the html loop and them, and inside the loop process the links:
<?php foreach($pieces as $key => $value): ?>
<a href="<?php echo $value[1]; ?>">Watch this in
<?php
$value[0] = explode(',', $value[0]);
foreach($value[0] as $index => $element) {
if(stripos($element, 'RESOLUTION') !== false) {
echo $element;
}
}
?>
</a><br/>
<?php endforeach; ?>
The HTML Markup should now look something like this:
<a href="http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/erewewrwrtf34324343443243434344/test1.mpegts/playlist-dfasdfasdfaw4q3243241.m3u8">Watch this in
RESOLUTION=852x480 </a>
<a href="http://me.mysite.com/media/l3/ertetertyrtut34534234324f3esrere/fgdhgfhgjhghfdsdf45454545345435/test1.mpegts/playlist-adfdfghgjdt5t45454542.m3u8">Watch this in
RESOLUTION=256x144 </a>
If I understood the quetion right you need to parse a string and get resolution.
function findResolution($string){
$array = explode(",",$string);
foreach ($array as $item){
if (strpos($item,"RESOLUTION")!==false){
return str_replace("RESOLUTION=","",$item);
}
}
}
Why not fopen/fgets?
function parseHLS($file) {
$return = array();
$i = 0;
$handle = fopen($file, "r");
if($handle) {
while(($line = fgets($handle)) !== FALSE) {
if(strpos($line,"EXT-X-STREAM-INF") !== FALSE) {
if ($c=preg_match_all ("/.*?(BANDWIDTH)(.*?)(,)(RESOLUTION)(.*?)(,)/is", $line, $matches)) {
$return['data'][$i]['bandwidth'] = str_replace("=","",$matches[2][0]);
$return['data'][$i]['resolution'] = str_replace("=","",$matches[5][0]);
}
}
if(strpos($line,".ts") !== FALSE) {
$return['data'][$i]['url'] = str_replace(array("\r","\n"),"",$line);
$i++;
}
}
fclose($handle);
}
return $return;
}
That gets you an array with bandwidth, resolution and url keys for each iteration in the original file.
I'm trying to get a part of the URL on my website
In this situation:
http://mywebsite/filexx/yyyyy/abaete/374
$url2 = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(preg_match("/\/(\d+)$/",$url2,$matches))
{
$meuid = $matches[1];
}
its works, but lets put 2 different situations:
http://mywebsite/filexx/yyyyy/abaete/374/?p=1
// i'm try to get the 374 (ID)
http://mywebsite/filexx/yyyyy/374/?p=1
// here the same
so I want to get last part if integer ( 374 )
or the part before the query 374/?p=1. So I want the 374.
Thanks.
I'll just make my comment an answer:
<?php
$string = 'http://mywebsite/filexx/yyyyy/abaete/374/?g=123';
$matches = array();
preg_match_all('/.*?\/(\d+)\/?/s', $string, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
?>
It will also ignore the /?getval1=1&getval2=2&etc=etc
Output
Array
(
[0] => Array
(
[0] => http://mywebsite/filexx/yyyyy/abaete/374/
)
[1] => Array
(
[0] => 374
)
)
$url = 'http://mywebsite/filexx/yyyyy/abaete/374/?p=1';
$explodedUrl = explode('/',$url);
$countArray = count($explodedUrl);
if(strpos($explodedUrl[$countArray-1],'?') === FALSE){
$yourId = $explodedUrl[$countArray-1];
} else {
$yourId = $explodedUrl[$countArray-2];
}
$yourId contains your Id
http://mywebsite/filexx/yyyyy/abaete/374
$url2 = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(preg_match('~([0-9]+)\/\?p=~', $url2, $matches))
{
$meuid = $matches[1];
}
This gets the numbers before /?p=.