I want to work around email addresses and I want to explode them using php's explode function.
It's ok to separate the user from the domain or the host doing like this:
list( $user, $domain ) = explode( '#', $email );
but when trying to explode the domain to domain_name and domain_extention I realised that when exploding them using the "." as the argument it will not always be foo.bar, it can sometimes be foo.ba.ar like fooooo.co.uk
so how to separate "fooooo.co" from "uk" and let the co with the fooooo. so finally I will get the TLD separated from the other part.
I know that co.uk is supposed to be treated as the TLD but it's not official, like fooooo.nat.tn or fooooo.gov.tn
Thank You.
Just use strripos() to find the last occurrence of ".":
$blah = "hello.co.uk";
$i = strripos($blah, ".");
echo "name = " . substr($blah, 0, $i) . "\n";
echo "TLD = " . substr($blah, $i + 1) . "\n";
Better use imap_rfc822_parse_adrlist or mailparse_rfc822_parse_addresses to parse the email address if available. And for removing the “public suffix” from the domain name, see my answer to Remove domain extension.
Expanding on Oli's answer...
substr($address, (strripos($address, '.') + 1));
Will give the TLD without the '.'. Lose the +1 and you get the dot, too.
end(explode('.', $email)); will give you the TLD. To get the domain name without that, you can do any number of other string manipulation tricks, such as subtracting off that length.
Related
I'm working with some code used to try and find all the website URLs within a block of text. Right now we've already got checks that work fine for URLs formatted such as http://www.google.com or www.google.com but we're trying to find a regex that can locate a URL in a format such as just google.com
Right now our regex is set to search for every domain that we could find registered which is around 1400 in total, so it looks like this:
/(\S+\.(COM|NET|ORG|CA|EDU|UK|AU|FR|PR)\S+)/i
Except with ALL 1400 domains to check in the group(the full thing is around 8400 characters long). Naturally it's running quite slowly, and we've already had the idea to simply check for the 10 or so most commonly used domains but I wanted to check here first to see if there was a more efficient way to check for this specific formatting of website URLs rather than singling every single one out.
You could use a double pass search.
Search for every url-like string, e.g.:
((http|https):\/\/)?([\w-]+\.)+[\S]{2,5}
On every result do some non-regex checks, like, is the length enough, is the text after the last dot part of your tld list, etc.
function isUrl($urlMatch) {
$tldList = ['com', 'net'];
$urlParts = explode(".", $urlMatch);
$lastPart = end($urlParts);
return in_array($lastPart, $tldList);
}
Example
function get_host($url) {
$host = parse_url($url, PHP_URL_HOST);
$names = explode(".", $host);
if(count($names) == 1) {
return $names[0];
}
$names = array_reverse($names);
return $names[1] . '.' . $names[0];
}
Usage
echo get_host('https://google.com'); // google.com
echo "\n";
echo get_host('https://www.google.com'); // google.com
echo "\n";
echo get_host('https://sub1.sub2.google.com'); // google.com
echo "\n";
echo get_host('http://localhost'); // localhost
Demo
i want to extract only domain name from given URL for any kind of TLD's . TLD can have any number in character or my be two in size
<?php $domain_name = 'http://www.subdomain.domain.co.in' ;
$ParsedURL = parse_url($domain_name);
$domain_name = preg_replace("/^([a-zA-Z0-9].*\.)?([a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z.]{2,})$/", '$2', $ParsedURL['host']);
$domain_name = current(explode('.', $domain_name));
print_r($domain_name);
so above solution is satisfying only limited URLs ,it fails when TDL's are three or more in characters e.g org.in ,
Again i am getting different results when there is no subdomain in url .
Kindly help me out as i only want "domain" from Given URL
Try this regex /^\.{1}.*\.[a-zA-Z0-9]{2,3}(\.[a-zA-Z0-9]{2,3})?$/ the only thing would be to strip the first '.' from the matched string: <?php substr($domain_name, 1);?>
I have a text.
$text='userpics/115/X3WGOC0009JA.jpg';
I want to add a letter p before X3WGOC0009JA.jpg, so my output will be
$text='userpics/115/pX3WGOC0009JA.jpg';
---^
I am new to php, so I don't really know what to try, I was hoping you could guide me in the right direction
You can explode by the slash by one way.
$exploded_text = explode('/', $text);
$new_text = $exploded_text[0] . $exploded_text[1] . 'p' . $exploded_text[2];
It's not the best way, but it will work.
Based on his question, I think all he wants to do is:
$text='userpics/115/'.'p'.'X3WGOC0009JA.jpg';
First, I would get the filename using strrpos and substr:
$text = 'userpics/115/X3WGOC0009JA.jpg';
$prepend_filename = 'p';
$last_slash_pos = strrpos($text, '/');
if ($last_slash_pos === false) throw new Exception('No slashes found');
$path = substr($text, 0, $last_slash_pos);
$filename = substr($text, $last_slash_pos + 1); // Add one to skip slash
And then you can add the p (as specified in $prepend_filename) using this:
$new_path = $path . DIRECTORY_SEPARATOR . $prepend_filename . $filename;
Have you tried just setting you variables and concatenation if you doing this a bunch.
$p = 'p';
$new = "userpics/115/" . $p . "X3WGOC0009JA.jpg";
There is a function, substr_replace(), which can insert a string at a point you want.
We combine this with strRpos() which we can use to find the first slash, LOOKING IN REVERSE:
$string = substr_replace($string, 'p', strrpos($string, '/')+1 );
This will insert 'p' in $string. At the position of the '/' in $string. The +1 corrects the 'cursor' to the character AFTER the slash.
Why not use the explode functions?
Very simple: Those are slow. String functions like strpos() and substr_replace() are VERY fast, especially on small strings.
Arrays are WAY slower in php, so don't go there unless you have to. For simple string- manipulation you should use simple string functions (sounds easy when you put it like that doesnt it?).
In a simple test I benchmarked the explode variant like user3758531's VS the string variant like mine:
100.000 tries with arrays: 1.5 sec
100.000 tries with strings: 0.9 sec
In this one situation, with this one action timing doesnt really matter. But apply this way of thinking thoughout the website and you will notice it speeding up/slowing down.
I want to create Unique ID for my site users in this format
pubID-john_34032
So I want to do this using the email string.
I have this pseudo code (but I want it faster and more logical if that makes sense)
How do I write my Pseudo code as PHP?
<?php
$var e(mysqlescape(get(emailform));
$var r(rand(3,34);
$var ex=(pubID-);
$var e=(replace(#/.); //users may enter 1#site.com so replace the at and .
$var e(first4strTo(e)); // get first four letters, example 1sit
$var e(e)+r(); // e is now 1site-1547
$var printStr e+ex //join the 1site-1547 with pubID- and done
?>
Unique Id in PHP there is uniqid. You can append that to whatever string. Why reinvent the wheel?
Use str_replace() to remove the # and . characters then use uniqid() to generate a unique identifier. Finally, concatenate them all together.
$email = 'user#gmail.com';
$str = 'pubID-' . str_replace(array('#', '.'), '', $email) . '_' . uniqid();
echo $str;
I don't have the reputation points to respond on Emmanuel N's answer.
Here's how to use uniqid and just grab the first part of the email address.
uniqid(substr($email, 0, strpos($email, '#')) . '_')
I fail to see your reasoning here. Why: pubID-john_34032
Couldn't you use the the user's name and append an auto incrementing id generated by your database? Something like:
calvinfroedge_23923
Then all you have to do is replace non whitelisted characters in the names and put an underscore between the name and the id.
www.example.com
foo.example.com
foo.example.co.uk
foo.bar.example.com
foo.bar.example.co.uk
I've got these URL's here, and want to always end up with 2 variables:
$domainName = "example"
$domainNameSuffix = ".com" OR ".co.uk"
If I someone could get me from $url being one of the urls, all the way down to $newUrl being close to "example.co.uk", it would be a blessing.
Note that the urls are going to be completely "random", we might end up having "foo.bar.example2.com.au" too, so ... you know... ugh. (asking for the impossible?)
Cheers,
We had a few questions like this before, but I can't find a good one right now either. The crux is, this cannot be done reliably. You would need a long list of special TLDs (like .uk and .au) which have their own .com/.net level.
But as general approach and simple solution you could use:
preg_match('#([\w-]+)\.(\w+(\.(au|uk))?)\.?$#i', $domain, $m);
list(, $domain, $suffix) = $m;
The "domainNameSuffix" is called a top level domain (tld for short), and there is no easy way to extract it.
Every country has it's own tld, and some countries have opted to further subdivide their tld. And since the number of subdomains (my.own.subdomain.example.com) is also variable, there is no easy "one-regexp-fits-all".
As mentioned, you need a list. Fortunately for you there are lists publicly available: http://publicsuffix.org/
You will need to maintain a list of extensions for most accurate results I believe.
$possibleExtensions = array(
'.com',
'.co.uk',
'.com.au'
);
// parse_url() needs a protocol.
$str = 'http://' . $str;
// Use parse_url() to take into account any paths
// or fragments that may end up being there.
$host = parse_url($str, PHP_URL_HOST);
foreach($possibleExtensions as $ext) {
if (preg_match('/' . preg_quote($ext, '/') . '\Z/', $host)) {
$domainNameSuffix = $ext;
// Strip extension
$domainName = substr($str, 0, -strlen($ext));
// Strip off http://
$domainName = substr($domainName, 7);
var_dump($domainName, $domainNameSuffix);
break;
}
}
If you never have any paths or extra stuff, you can of course skip the parse_url() and the http:// adding and removal.
It worked for all your tests.
There isn't a builtin function for this.
A quick google search lead me to http://www.wallpaperama.com/forums/php-function-remove-domain-name-get-tld-splitter-split-t5824.html
This leads me to believe you need to maintain a list of valid TLD's to split URLs on.
Alright chaps, here's how I solved it, for now. Implementation of more domain names will be done as well, at some point in the future. Don't know what technique I'll use, yet.
# Setting options, single and dual part domain extentions
$v2_onePart = array(
"com"
);
$v2_twoPart = array(
"co.uk",
"com.au"
);
$v2_url = $_SERVER['SERVER_NAME']; # "example.com" OR "example.com.au"
$v2_bits = explode(".", $v2_url); # "example", "com" OR "example", "com", "au"
$v2_bits = array_reverse($v2_bits); # "com", "example" OR "au", "com", "example" (Reversing to eliminate foo.bar.example.com.au problems.)
switch ($v2_bits) {
case in_array($v2_bits[1] . "." . $v2_bits[0], $v2_twoPart):
$v2_class = $v2_bits[2] . " " . $v2_bits[1] . "_" . $v2_bits[0]; # "example com_au"
break;
case in_array($v2_bits[0], $v2_onePart):
$v2_class = $v2_bits[1] . " " . $v2_bits[0]; # "example com"
break;
}