I have easy question. I need to verify a string and validate a whitelist domain like this:
$WList = array('mega.co.nz','mediafire.com','putlocker.com','');
$Dominio = str_replace("www.","",parse_url($EnlaceUrl,PHP_URL_HOST));
if(in_array($Dominio,$WList)){//ok}
but this method doesnt retieve me domains like:
www42.zippyshare.com,www51.zippyshare.com,www71.zippyshare.com,www23.zippyshare.com
how resolve this problem? :)
Try this which removes all that begin with www until the first dot (inclusive):
$Dominio = preg_replace('~^www[^.]*\.~', '', parse_url($EnlaceUrl,PHP_URL_HOST));
You can use this:
if (preg_match('/[\w\d-]+\.(\w{3,4}|(\w{2,3}\.\w{2}))$/', $Dominio, $match))
$Dominio = $match[1];
It will convert anything.domainname.suffix into domainname.suffix so you can test against your list.
Yet another preg_match example :
if(preg_match("/(?:([^.]+).)?([^.]+).([^\\/]+)/", $Dominio, $m)) {
$Dominio = $m[2] . '.' $m[3];
}
Related
I need to get domain name from URL excluding "www" and ".com" or ".co.uk" or anything other.
Example-
I have following urls like-
http://www.example.com
http://www.example.co.uk
http://subdomain.example.com
http://subdomain.example.co.uk
There will be anything at ".com" , ".org" , ".co.in", ".co.uk".
I try this it work for me.
$original_url="http://subdomain.example.co.uk"; //try with all urls above
$pieces = parse_url($original_url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
echo strstr( $regs['domain'], '.', true );
}
Output- example
I get this from Here
Get domain name from full URL
(?:https?:\/\/)?(?:www\.)?(.*)\.(?=[\w.]{3,4})
Try this.See demo.Grab the capture.
http://regex101.com/r/bW3aR1/2
You should use the PHP function parse_url() in combination with a str_replace() or regex, or maybe even an explode. It depends on a few things:
Things to note:
Will there always be a subdomain?
Will there be a specific list of allowed subdomains?
I would do something like this:
<?php
$url = 'http://www.something.com';
$parts = explode('.', parse_url($url, PHP_URL_HOST));
echo $parts[1]; // "something"
I have this string:
$str="http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg";
Is there a built-in php function that can shorten it by removing the ._SL110_.jpg part, so that the result will be:
http://ecx.images-amazon.com/images/I/418lsVTc0aL
no, there's not any built in URL shortener php function, if you want to do something similar you can use the substring or create a function that generates a short link and stores the long and short value somewhere in database and display only the short one.
well, it depends if you need a regexp replace (if you don't know the complete value) or if you can do a simple str_replace like below:
$str = str_replace(".SL110.jpg", "", "http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg");
You can use preg_replace().
For example preg_replace("/\.[^\.]+\.jpg$/i", "", $str);
I would recommend using:
$tmp = explode("._", $str);
and then using $tmp[0] for your purpose, if you make sure the part you want to get rid of is always separated by "._" (dot-underscore) symbols.
You can try
$str = "http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg";
echo "<pre>";
A.
echo strrev(explode(".", strrev($str), 3)[2]) , PHP_EOL;
B.
echo pathinfo($str,PATHINFO_DIRNAME) . PATH_SEPARATOR . strstr(pathinfo($str,PATHINFO_FILENAME),".",true), PHP_EOL;
C.
echo preg_replace(sprintf("/.[^.]+\.%s$/i", pathinfo($str, PATHINFO_EXTENSION)), null, $str), PHP_EOL;
Output
http://ecx.images-amazon.com/images/I/418lsVTc0aL
See Demo
you could do this substr($data,0,strpos($data,"._")), if what you want is to strip everything after "._"
No, it is not (at least not directly). Such URL shorteners usually generate unique ID and remember your original URL and generated ID. When you enter such url, you start a script, which looks for given ID and then redirect to target URL.
If you want just cut of some portion of your string, then assuming that filename format is as you shown, just look for 1st dot and substr() to that place. Or
$tmp = explode('.', $filename);
$shortName = $tmp[0];
If suffix ._SL110_.jpg is always there, then simply str_replace('._SL110_.jpg', '', $filename) could work.
EDIT
Above was example for filename only. Whole code would be:
$url = "http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg";
$urlTmp = explode('/', $url);
$fileNameTmp = explode( '.', $urlTmp[ count($urlTmp)-1 ] );
$urlTmp[ count($urlTmp)-1 ] = $fileNameTmp[0];
$newUrl = implode('/', $urlTmp );
printf("Old: %s\nNew: %s\n", $url, $newUrl);
gives:
Old: http://ecx.images-amazon.com/images/I/418lsVTc0aL._SL110_.jpg
New: http://ecx.images-amazon.com/images/I/418lsVTc0aL
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
string '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg' (length=85)
what i need is just
http://localhost/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
what is the best way doing it ? i mean useing strlen ? substr_replace ? substr ? im a bit confused what is the best way doing this? becouse there is many ways to do this.
edit* there is no newbie tag :|
// get from database red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
$image_path = $this->data['products'][0]['image_small'];
$exploded = end(explode('/', $image_path));
$myurl = DOMAIN;
$myfullurl = $myurl."/storage/".$exploded;
// it works!, but let see the comments maybe there is a better way :)
Here is how you can get the image part:
$str = '/home/adam/Projects/red/storag/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
$exploded = end(explode('/', $str));
echo $exploded;
Result:
22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
Now you can concatenate it with whatever eg:
$new_str = 'http://localhost/storage/' . $exploded;
echo $new_str;
Result:
http://localhost/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg
And It is most likely you want to concatenate the image path with your document root which you do like this:
$img_path = $_SERVER['DOCUMENT_ROOT'] . $exploded;
The idea is that you explode the string with explode function by specifying / as delimiter. This gives you array, now you use the end function to get the ending part of the array which is your image actually.
If the path prefix represents your document root path, then you can do this to strip it:
$path = '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
$_SERVER['DOCUMENT_ROOT'] = '/home/adam/Projects/red/';
if (substr($path, 0, strlen($_SERVER['DOCUMENT_ROOT'])) === $_SERVER['DOCUMENT_ROOT']) {
$uriPath = substr($path, strlen(rtrim($_SERVER['DOCUMENT_ROOT'], '/')));
echo $uriPath;
}
I suggest you check if the string contains /home/adam/Projects/red, and if it does, you use substr to get the part after it, and you glue it with http://localost.
$path = '/home/adam/Projects/red/storage/*snip*.jpg';
$basePath = "/home/adam/Projects/red";
if (strpos($path, $path) !== false)
$url = 'http://localhost' . substr($path, strlen($basePath));
This one's pretty much the easiest
str_replace(
"/home/adam/Projects/red",
"http://localhost",
"/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg"
);
$string = '/home/adam/Projects/red/storage/22ff0bc0662bd323891844f6ed342cce2603490ec0_tumb_2.jpg';
str_replace('/home/adam/Projects/red', 'http://localost', $string)
Is there a way I can strip out the variables from a link using PHP for example, if I have a link that reads http://localhost/link/index.php?s=30&p=3 how would I strip out ?s=30&p=3 so my link reads http://localhost/link/index.php
list($url) = explode("?", $longUrl, 2);
Edit (suggested by Hoohah):
Also you can use strstr() (PHP 5.3.0 onward):
echo strstr($longurl, "?", TRUE);
PHP has a built in function for this.
It is parse_url(). Take a look at the possible return values. In this case we can use scheme, host, and path.
For example:
<?php
$info = parse_url("http://localhost/link/index.php?s=30&p=3");
echo $info["scheme"] . "://" . $info["host"] . $info["path"];
// Output: http://localhost/link/index.php
?>
Live example
The advantage of this method over using explode() is that it gives you control over whether you want to show the username, password, and port if included. The code above will not show any of these, so http://user:pass#localhost:81/link/index.php?s=30&p=3 will return http://localhost/link/index.php, stripping the username, password, and port number, which is what I assume you'd want. Username, password, and port are available as $info["user"], $info["pass"], and $info["port"].
The explode() method fails if the password contains question marks. This method doesn't fail even with ? and # signs in the password.
As a final note, if you are going to be dealing with port numbers, usernames, and passwords, you can use the code below (which has one added line) to strip usernames and passwords but keep port number:
<?php
$info = parse_url("http://user:__?**####&?ss#localhost:80/link/index.php?s=30&p=3");
// If port is present add a colon before it, if not make it an empty string.
isset($info["port"]) ? $port = ":" . $info["port"] : $port ="";
echo $info["scheme"] . "://" . $info["host"] . $port . $info["path"];
// Outputs: http://localhost:80/link/index.php
?>
Live example
Finally, you really should not be using username and password in the link. From RFC2396
Some URL schemes use the format "user:password" in the userinfo
field. This practice is NOT RECOMMENDED, because the passing of
authentication information in clear text (such as URI) has proven to
be a security risk in almost every case where it has been used.
Try this:
$pos = strpos($url, '?');
if($pos !== FALSE) {
$url = substr($url, 0, $pos);
}
$url = 'http://localhost/link/index.php?s=30&p=3';
$d = explode("?", $url);
$stripped = reset($d);
$fullUrl = "http://localhost/link/index.php?s=30&p=3#dontreplace";
$urlData = parse_url($fullUrl);
$url = str_replace('?'.$urlData['query'],'',$fullUrl);
This solution takes into account that you could have a hashtag after the paremeters and it does not replace it.
If you just don't care of what is after the ? then use the other answers
$url = strtok($url,"?");
I haven't tested this but you could probably do something like:
$my_url = 'http://localhost/link/index.php?s=30&p=3';
$split = explode('?', $my_url);
$new_url = $split[0];
parse_url('http://localhost/link/index.php?s=30&p=3',PHP_URL_PATH);
If U need to parse the requested url, U can simply take it from globals:
// http://localhost/foo/bar?foo=bar&Foo1=Bar1
var_dump($_SERVER['QUERY_STRING']); // string(17) "foo=bar&Foo1=Bar1"
If symfony/http-foundation component is used, U can get query string from its Request class as follows:
$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$queryString = $request->getQueryString();