I need to get the ID part of a pastebin link,
which is setup like http://pastebin.com/{id}, i have tired alot of different regex i am also using preg_match in php
preg_match("~http://pastebin.com/([0-9a-zA-Z]+)~", $url, $match);
print_r($match);
or
$url = "http://pastebin.com/a65d46";
$parsed = parse_url($url);
echo trim($parsed['path'])." is ID you needed";
Instead of regex, try using parse_url to extract the path
regex would be overkill for this.
$url = "http://pastebin.com/Ugj1eqCN"
$pos = strpos($url,"pastebin.com/");
echo substr($url,$pos+13);
Related
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.
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
I'm Stuck try to get domain using preg_replace,
i have some list url
download.adwarebot.com/setup.exe
athena.vistapages.com/suspended.page/
prosearchs.com/se/tds/in.cgi?4&group=5¶meter=mail
freeserials.spb.ru/key/68703.htm
what i want is
adwarebot.com
vistapages.com
prosearchs.com
spb.ru
any body can help me with preg_replace ?
i'm using this http://gskinner.com/RegExr/ for testing :)
using preg_replace, if the number of TLDs is limited:
$urls = array( 'download.adwarebot.com/setup.exe',
'athena.vistapages.com/suspended.page/',
'prosearchs.com/se/tds/in.cgi?4&group=5¶meter=mail',
'freeserials.spb.ru/key/68703.htm' );
$domains = preg_replace('|([^.]*\.(?:com|ru))/', '$1', $urls);
matches everything that comes before .com or .ru which is not a period. (to not match subdomains)
You could however use PHPs builtin parse_url function to get the host (including subdomain) – use another regex, substr or array manipulation to get rid of it:
$host = parse_url('http://download.adwarebot.com/setup.exe', PHP_URL_HOST);
if(count($parts = explode('.', $host)) > 2)
$host = implode('.', array_slice($parts, -2));
Following code assumes that every entry is exactly at the beginning of the string:
preg_match_all('#^([\w]*\.)?([\w]*\.[\w]*)/#', $list, $m);
// var_dump($m[2]);
P.S. But the correct answer is still parse_url.
Why use a regular expression? Of course it is possible, but using this:
foreach($url in $url_list){
$url_parts = explode('/', $url);
$domains[] = preg_replace('~(^[^\.]+\.)~i','',$url_parts[0]);
}
$domains = array_unique($domains);
will do just fine;
maybe a more generic solution:
tested by grep, I don't have php environment, sorry:
kent$ echo "download.adwarebot.com/setup.exe
dquote> athena.vistapages.com/suspended.page/
dquote> prosearchs.com/se/tds/in.cgi?4&group=5¶meter=mail
dquote> freeserials.spb.ru/key/68703.htm"|grep -Po '(?<!/)([^\./]+\.[^\./]+)(?=/.+)'
output:
adwarebot.com
vistapages.com
prosearchs.com
spb.ru
So I'm trying to get the id from a url for youtube..
here is the url
http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2
then there's also - in the url too.
it wouldn't let me post another url but it's the same as above but with the id ucvkO0x-mL4
how can I grab between videos/ and /related (the id) with regex?
I tried to use txt2re.com which is what I always use, but it's not working for this case..
thanks!
No need for even regex, just a simple strpos and substr will do it. Or just use explode like this:
<?php
$url = 'http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2';
//BY STRPOS/SUBSTR
echo substr($url, 42, strpos($url, '/related', 42) - 42);
//BY EXPLODE
$parts = explode('/', $url);
echo $parts[6];
?>
(?:.*)videos/(.*?)/related\?v=2
This is how I would do it
$url = 'http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2';
preg_match('#.*videos/([0-9a-zA-Z_\-]{11})/related.*#', $url, $matches);
print_r($matches);
But, #shamittomar is right about strpos and substr
using regex you can do it by
$url = "http://gdata.youtube.com/feeds/api/videos/kffacxfA7G4/related?v=2";
preg_match('/videos\/(.+)\/related/',$url,$match);
$id = $match[1];
i have this
http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNFyHi2aIJIV7kAlui1Sd_MQGosiBA&url=http://ksa.daralhayat.com/ksaarticle/192445
i want to get the value of url= only
$url = html_entity_decode($url);
$parts = parse_url($url);
parse_str($parts['query'],$params);
echo $params['url'];
For completeness the you can preg_match with that:
/^.*url\=(.*)$/
But you should prefer the parse_url() method that is really faster than a RegEx.