Exploding a URL - php

What am I doing wrong? I currently have the following code below and I am trying to just get the secondary url which links to the direct MP3 path.
$date = $html2->find('strong > a',0);
$explode = explode ("http://www.example.com/?dl_name=", $date->href);
print $explode;
It returns ARRAY.

explode returns an array of strings, try using print_r($explode) and then use $explode[0], $explode[1]...

Could you resolve this with str_replace:
$secondary_url = str_replace("http://www.example.com/?dl_name=", '', $date->href);
You will get the rest of this string in $secondary_url.
Reference: str_replace

Related

How to get a part of url from php

how can I use php to get exactly the id from google play url.
Example:
Google Play Url: https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en
I want to get the com.zing.zalo . Thank you!
Simple Way
use preg_match or get the id from the url using $_GET['id'], if
you get this from url as other answer did.
$Url = "https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en";
preg_match("/[^?]+(?:\?id=([^&]+).*)?/", "$Url", $matches);
echo $matches[1]; //com.zing.zalo
Working Example here Check online
The Longest way:
Simply you can use some PHP function to get it. Lets you have the following url.
$Url = "https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en";
so what you need to explode the url using ? which is only one on the url.
$arr = explode("?", $Url);
From that array you need to store only the second part cause you need query string. So take only $arr[1]. Now explode again the $arr[1] with the & sign which is divide the rest of the url i mean $arr[1].
$arr2 = explode("&", $arr[1]);
Now you are all set, use another explode function to get the com.zing.zalo from the $arr2[0].
$idval = explode("=", $arr2[0]);
Result, Just echo the second part of the $idval array.
echo $idval[1]; //com.zing.zalo
Use $_GET['id'] to get the query string value of id
<?php
echo $_GET['id'];
?>
You could do this with regex: (?:\?id=)(.*)\b (I'm sure there's a more effective regex for this, but this accomplishes what you require)
preg_match('/(?:\?id=)(.*)\b/', 'https://play.google.com/store/apps/details?id=com.zing.zalo&hl=en', $matches);
print_r($matches);
Returns:
Array
(
[0] => ?id=com.zing.zalo&
[1] => com.zing.zalo
)

How to get last part of a string?

I have this string:
"application/controllers/backend"
I want get:
backend
of course the backend it's dynamic, so could be change, so I'm looking for a solution that allow me to get only the last part of the string. How I can do that?
You can take the advantage of basename() to get the last part
in your case, it will be
basename("application/controllers/backend");
Output:
backend
Some thing like this :
echo end(explode("/", $url));
If this thorws error then do :
$parts = explode("/", $url);
echo end($parts);
$arr = explode ("/", $string);
//$arr[2] is your third element in the string
http://php.net/manual/en/function.explode.php
Just use
basename("application/controllers/backend");
http://php.net/manual/en/function.basename.php
And, if you want to do it with a regex:
$result = (preg_match('%.*[/\\\\](.*?)$%', $url, $regs)) ? $regs[1] : '';
You did ask initially for a solution with regex, so, although the other answers haven't involved regex, here is one approach which does.
You can use preg_match and str_replace for this:
$string = '"application/controllers/backend"';
preg_match('/[^\/]+"/', $string, $matches);
$last_item = str_replace('"','',$matches[0]);
$last_item is now a string containing the word backend.

PHP removing several things from a string at once

I have a string like this being entered into my database that I can't format before it gets stored :
image/upload/v1440427262/hglz466d8mm1pazysaoh.jpg#32e2e9a111a4f9f4aa01dbad2ca2aa403c994d28
The only part of that string that I want to use is this :
hglz466d8mm1pazysaoh.jpg
I'm trying to use strpos to remove the excess data.
So far I've managed to remove everything after and including the hashtag
($data is the original string) :
$dataclean = substr($data, 0, strpos($data, "#"));
This works as expected with $dataclean returning :
image/upload/v1440427262/hglz466d8mm1pazysaoh.jpg
But I don't know how to remove the rest of the excess data :
image/upload/v1440427262/
Also, can this all be done in one hit or does it have to be split into several operations?
Use basename:
$dataclean = basename(substr($data, 0, strpos($data, "#")));
If basename() doesn't work I would explode the string by the forward slash.
$data = 'image/upload/v1440427262/hglz466d8mm1pazysaoh.jpg#32e2e9a111a4f9f4aa01dbad2ca2aa403c994d28';
$pieces = explode("/", $data);
$dataclean = substr($pieces[3], 0, strpos($data, "#"));
As mentioned in the question, first remove values after # using
$link = substr($data, 0, strpos($data, "#"));
Then use basename() function to access filename from the URL.
For example,
$link = "http://example.com/folderPath/filename.php";
echo basename($link); // It will return filename.php
Nothing wrong with substr + strpos, but it looks like your string is a URL, so you could use parse_url to isolate the path before using basename. Just another option FYI.
basename(parse_url($yourString, PHP_URL_PATH));
You can use a regex function:
preg_match("/[a-z0-9]+\.[a-z]{3}/i", $input_line, $output_array);
Try the code

How to explode vaules in $_GET['']=1#5#6 in php?

Hi in my url i have variables stored in ../index.php?cat=1#5#8 in so on how to separate them using explode function so out put could be
arr[0]=1
arr[1]=5
arr[2]=8
Try with explode like
$arr = explode("#",$_GET['cat']);
But as #Bora said after # the remaining string will may not be sent ,so better to use '_' in place of '#' (1_5_8_....)and can explode it like
$arr = explode("_",$_GET['cat']);
Try this:
$array=$_GET['cat'];
$result = explode("#",$array);

How to get a string from url using preg_match?

I want to extract id from an url using php preg_match..
For eg: $string = 'http://www.mysite.com/profile.php?id=1111'; I need output as '1111'
using preg_match.
I tried this :
if(preg_match('/(?:https?):\/\/www\.mysite\.com\/profile\.php\?id\=[0-9]/', $string, $match) > 0){
$id = $match[1];
}
But am getting output as 'profile.php'
Can someone help me please?
Why not use parse_url with parse_str
$url_component = parse_url($string);
parse_str($url_component['query'], $output);
echo $output['id'];
if there is only one parameter in the url you can use explode function to do that easily, like
$string = 'http://www.mysite.com/profile.php?id=1111';
$ex=explode('=',$string);
$id=$ex[1];
You may also use parse_url function of php.
Hope this help,
$pattern = '/(?:https?):\/\/www\.mysite\.com\/profile\.php\?id\=([0-9]+)/';
This should work fine!
But do this with parse_url

Categories