Remove urls that have certain domain in them - php

I have a set of urls for example
http://t3.gstatic.com/images?q=tbn:ANd9GcRfLZhH0jpyUJxGtsiHcldUPiNQsosLdR9xgcYqVWyRWGYS4qtt
http://feeds.feedburner.com/~r/DrudgeReportFeed/~4/zSLWG4ybmjw
I want to remove any url that has feeds.feedburner.com in it. What regular expression would I use? (php)

Why use regex? Use parse_url.
$urlData = parse_url($url);
if ($urlData['host'] != 'feeds.feedburner.com'){
// Not a feedburner url
}
Shorthand, by the way, is as follows:
if (parse_url($url, PHP_URL_HOST) != 'feeds.feedburner.com'){
// same outcome
}

Use this regexp:
/feeds\.feedburner\.com/

Related

Get vine video id using php

I need to get the vine video id from the url
so the output from link like this
https://vine.co/v/bXidIgMnIPJ
be like this
bXidIgMnIPJ
I tried to use code form other question here for Vimeo (NOT VINE)
Get img thumbnails from Vimeo?
This what I tried to use but I did not succeed
$url = 'https://vine.co/v/bXidIgMnIPJ';
preg_replace('~^https://(?:www\.)?vine\.co/(?:clip:)?(\d+)~','$1',$url)
basename maybe?
<?php
$url = 'https://vine.co/v/bXidIgMnIPJ';
var_dump(basename($url));
http://codepad.org/vZiFP27y
Assuming it will always be in that format, you can just split the url by the / delimiter. Regex is not needed for a simple url such as this.
$id = end(explode('/', $url));
Referring to as the question is asked here is a solution for preg_replace:
$s = 'https://vine.co/v/bXidIgMnIPJ';
$new_s = preg_replace('/^.*\//','',$s);
echo $new_s;
// => bXidIgMnIPJ
or if you need to validate that an input string is indeed a link to vine.co :
$new_s = preg_replace('/^(https?:\/\/)?(www\.)?vine\.co.*\//','',$s);
I don't know if that /v/ part is always present or is it always v... if it is then it may also be added to regex for stricter validation:
$new_s = preg_replace('/^(https?:\/\/)?(www\.)?vine\.co\/v\//','',$s);
Here's what I am using:
function getVineId($url) {
preg_match("#(?<=vine.co/v/)[0-9A-Za-z]+#", $url, $matches);
if (isset($matches[0])) {
return $matches[0];
}
return false;
}
I used a look-behind to ensure "vine.co/v/" always precedes the ID, while ignoring if the url is HTTP or HTTPS (or if it lacks a protocol altogether). It assumes the ID is alphanumeric, of any length. It will ignore any characters or parameters after the id (like Google campaign tracking parameters, etc).
I used the "#" delimiter so I wouldn't have to escape the forward slashes (/), for a cleaner look.
explode the string with '/' and the last string is what you are looking for :) Code:
$vars = explode("/",$url);
echo $vars[count($vars)-1];
$url = 'https://vine.co/v/b2PFre2auF5';
$regex = '/^http(?:s?):\/\/(?:www\.)?vine\.co\/v\/([a-zA-Z0-9]{1,13})$/';
preg_match($regex,$url,$m);
print_r($m);
1. b2PFre2auF5

match part of a url

I would like to get all matches for any url's that have index.php?route=forum/ in them
Example urls to filter are:
http://test.codetrove.com/index.php?route=forum/forum
http://test.codetrove.com/index.php?route=forum/forum_category&forum_path=2
So i need the match to be true if it contains index.php?route=forum/ the http and domain can be anything like http or https or any domain.
Any idea's?
Rather than using a baseball bat to bludgeon a spider, take a look at strpos().
$string = "index.php?route=forum/";
if (strpos($url, $string) !== false) {
//we have a match
}
You can use regex :
/index\.php\?route=forum\/.*/
Or with the $_GET variable
if(preg_match('/forum\/.*/', $_GET['route'])) {
echo 'yahoo';
}
One possibility is to use the php strpos function documented here
$IsMatch = strpos ( $url , "index.php?route=forum/");

how to define end of string

I want to add "/info" in the end of urls. If the "/info" already exists, I would like to leave as it is.
I'm currently using:
if(strpos($url, "/info") === false){
$url .= "/info";
}
But the above code works only if the url doesn't contain "/" at the end.
For example: if the url is http://www.domain.com then it works perfectly and the output is http://www.domain.com/info. If the url is http://www.domain.com/ then it shows http://www.domain.com//info.
How to avoid this?
Trim the domain, then check the last five characters (in case the "/info" string appears elsewhere in the URL).
$url = rtrim($url,'/');
if(substr($url,-5)!='/info') $url .= '/info';
You just need to take that into consideration.
if(substr($url, -5) != '/info') {
if(substr($url, -1) == "/")
$url.="info";
else
$url.="/info";
}
Note I modified the first if to only check for '/info' at the end of the url; as Gareth did ;)
consider following url :
http://www.domain.com/info/test/
if you use strpos in that way , you will get wrong result.
instead you can use substr and rtrim :
$url = (substr($url,-5) != '/info') ? rtrim($url, "/") . '/info' : $url;

Determine User Input Contains URL

I have a input form field which collects mixed strings.
Determine if a posted string contains an URL (e.g. http://link.com, link.com, www.link.com, etc) so it can then be anchored properly as needed.
An example of this would be something as micro blogging functionality where processing script will anchor anything with a link. Other sample could be this same post where 'http://link.com' got anchored automatically.
I believe I should approach this on display and not on input. How could I go about it?
You can use regular expressions to call a function on every match in PHP. You can for example use something like this:
<?php
function makeLink($match) {
// Parse link.
$substr = substr($match, 0, 6);
if ($substr != 'http:/' && $substr != 'https:' && $substr != 'ftp://' && $substr != 'news:/' && $substr != 'file:/') {
$url = 'http://' . $match;
} else {
$url = $match;
}
return '' . $match . '';
}
function makeHyperlinks($text) {
// Find links and call the makeLink() function on them.
return preg_replace('/((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])/e', "makeLink('$1')", $text);
}
?>
You will want to use a regular expression to match common URL patterns. PHP offers a function called preg_match that allows you to do this.
The regular expression itself could take several forms, but here is something to get you started (also maybe just Google 'URL regex':
'/^(((http|https|ftp)://)?([[a-zA-Z0-9]-.])+(.)([[a-zA-Z0-9]]){2,4}([[a-zA-Z0-9]/+=%&_.~?-]))$/'
So your code should look something this:
$matches = array(); // will hold the results of the regular expression match
$string = "http://www.astringwithaurl.com";
$regexUrl = '/^(((http|https|ftp):\/\/)?([[a-zA-Z0-9]\-\.])+(\.)([[a-zA-Z0-9]]){2,4}([[a-zA-Z0-9]\/+=%&_\.~?\-]*))*$/';
preg_match($regexUrl, $string, $matches);
print_r($matches); // an array of matched patterns
From here, you just want to wrap those URL patterns in an anchor/href tag and you're done.
Just how accurate do you want to be? Given just how varied URLs can be, you're going to have to draw the line somewhere. For instance. www.ca is a perfectly valid hostname and does bring up a site, but it's not something you'd EXPECT to work.
You should investigate regular expressions for this.
You will build a pattern that will match the part of your string that looks like a URL and format it appropriately.
It will come out something like this (lifted this, haven't tested it);
$pattern = "((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:##%/;$()~_?\+-=\\\.&]*)";
preg_match($pattern, $input_string, $url_matches, PREG_OFFSET_CAPTURE, 3);
$url_matches will contain an array of all of the parts of the input string that matched the url pattern.
You can use $_SERVER['HTTP_HOST'] to get the host information.
<?php
$host = $SERVER['HTTP_HOST'];
?>
Post

How to determine if URL ends in /site-map

I need determine if the current URL ends in /site-map
For example: site.com/site-map
Or
site.com/somedirectory/site-map
Is there a PHP method to pull this value?
You could use substr to check the last 9 characters:
$url = $_SERVER['REQUEST_URI'];
if (substr($url,-9)=="/site-map")
edit to accommodate the url ending with /site-map/ occasionally you could do this:
$url = $_SERVER['REQUEST_URI'];
if (substr($url,-9)=="/site-map" || substr($url,-10)=="/site-map/")
Here's a preg_match() solution. Likely to be a bit slower than strpos() & substr(), but more flexible.
$url = $_SERVER['REQUEST_URI'];
if (preg_match("/\/site-map$/", $url)) {
// it ends in /site-map
}

Categories