regex help getting a value from url - php

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.

Related

How do I remove http, https from string in php

Example user input
http://domain.com/
hTTp://domain.com/Cars/
hTtp://www.domain.com/pAge/
I want a php function to make the output like
domain.com
domain.com/Cars/
www.domain.com/pAge/
Let me know :)
You don't need regular expressions here, just use parse_url and str_replace:
$url = 'hTtp://www.domain.com/pAge/';
$url = str_replace( parse_url( $url, PHP_URL_SCHEME ) . '://', '', $url );
Consider using parse_url() to get an array with the different parts of the url and rebuild it as a string any way you want.
Consider using a regex, with preg_replace
$converted = preg_replace('#^h+t+p+s+?://#i', '', $stringtoprocess);
Maybe the easiest way might be
echo str_replace('//','',strstr($url, '//'));

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

Regex to get ID of pastebin.com

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);

PHP preg_replace Help

I need help replacing a link like google.com into http://www.google.com
$url = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2",$url);
$output = htmlspecialchars(urldecode($url));
I'm using an iframe like:
<iframe src='$url'></iframe>
However, if the src="google.com" instead of "http://google.com" it will not work. So, how can I transform google.com into http://www.google.com?
Here's a non regex hack way to do it.
$url = 'google.com';
function addHTTP($url) {
return 'http://'.str_replace('http://','',$url);
}
$url = "www.google.com";
if(!preg_match("/^https/i",$url))
$url = "http://$url";
There are better ways to do this, but this will work:
if(!preg_match("#^http:\/\/#", $url)) {
$url = "http://".$url;
}
$url = 'http://' . $url;
the simpliest way possible :o
How about checking if http:// is on the beginning of it and if not tag it on? Like so:
$url = 'google.com';
if (!preg_match('#^http://#', $url)) {
$url = 'http://'.$url;
}
Just for fun, here's one that uses just preg_replace by taking advantage of a negative lookahead. However, I agree with the other solutions here, that it is probably best to just to a preg_match and then a string concatenation.
$url = preg_replace('#^(?!https?://)#', 'http://', $url);
If you just want to make your RegEx match google.com e.a., all you have to do is make www. optional. Please note that this may introduce other problems, such as end.begin being recognized as an URL.
/([^\w\/])((www\.)?[a-z0-9\-]+\.[a-z0-9\-]+)/i

regex part of url

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];

Categories