match part of a url - php

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

Related

fetching particular value after applying string manupulation in url

I am working on query string pattern for a redirection application. Can we use multiple OR statement in strstr. ex. if i am not sure whether it will be & or & or $ or / then is it possible to use the other signs in OR condition with $code= strstr($code, '/',+1);
Explanation:
the url will come to our redirect application like this
https://www.sample.com?key=1234~rety~1234~retu&c=12&k=12
OR
https://www.sample.com?key=1234~rety~1234~retu/c=12/k=12
OR
https://www.sample.com?key=1234~rety~1234~retu$c=12$k=12
the only variable for our purpose is key(whose name can be changed but data will come int he same pattern) all i want is to get data with ~ pattern.
I am doing following:
parse_str(parse_url($url, PHP_URL_QUERY), $parts);
$keys = array_keys($parts);
$size=sizeof($keys);
$total=substr_count($uri, '~');
$var=$keys[0];
$value=$parts[$var];
for($i=0;$i<$size;$i++){
$var=$keys[$i];
$value=$parts[$var];
$total=substr_count($value, '~');
if($total==3){
$digit_code = preg_split('/\~+/', $value);
$digit_code = array_filter($digit_code);
$digit_code = array_values($digit_code);
$project_id=$digit_code[0];
$country_id=$digit_code[1];
$vendor=$digit_code[2];
$code=$digit_code[3];
//$code= strstr($code, '/',+1);
}
But it is not working when url is coming like this
https://www.sample.com?key=1234~rety~1234~retu/c=12/k=12
or
https://www.sample.com?key=1234~rety~1234~retu$c=12$k=12
This is where using regex could be applicable:
preg_match('/((?:\/|&|\$).*)/', $code, $matches);
$code = $matches[1];
This will match a / or & or $ followed by the rest of the string.
http://au1.php.net/preg_match
are you referring in PHP redirection? or a query string in database to fetch data?
you can read strstr manual.
I found the solution:
I just put
if (strpos($code,'/') !== false) {
$code= strstr($code, '/',+1);
}
if (strpos($code,'$') !== false) {
$code= strstr($code, '$',+1);
}
once after for loop

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

Remove urls that have certain domain in them

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/

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

check url for a specific piece of text

Lets say i capture the url as $url = $_SERVER['REQUEST_URI'] or $_GET['q'].
How can i check for the condition if the url contains a piece of text say "x"?
What regex would be required for it
If you use preg_match with a variable value, make sure you use preg_quote to escape any special characters:
$look_for = "x";
if (preg_match( "/".preg_quote($look_for, "/")."/i" , $url) ) {
...
You can also use the strpos() function
http://php.net/manual/en/function.strpos.php
This is one example from there:
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
In addition to the functions for finding the string, you may also want to check out Drupal's arg() function: http://api.drupal.org/api/drupal/includes--path.inc/function/arg/6
I think preg_match would serve you well:
if (preg_match( "/x/i" , $url) ) {
}
If your working with a url from the wild you might want to use php's parse_url(), http://www.php.net/manual/en/function.parse-url.php , that will make it easy to work with. If it is a drupal URL your probably going to want to use Drupal's arg() function, http://api.drupal.org/api/drupal/includes--path.inc/function/arg/6 , but it can have issues with the Pathauto module

Categories