Created
October 17, 2015 08:16
-
-
Save technion/5ca01ca420725e17cd3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def instacheck(name) | |
unless /[a-z0-9._]{,30}/.match(name) | |
return false | |
end | |
if /\.\./.match(name) | |
return false | |
end | |
if /^\d+$/.match(name) | |
return false | |
end | |
if /^\./.match(name) | |
return false | |
end | |
true | |
end |
Thanks, @technion I tried that before and didn't get it.
You will need to delimit your pattern with for example '/' in preg_match()
and it is not explained on the functions manual website. Maybe a basic to know? Well, that`s why I asked here, because pasting the regex from above did not work straight. I found some info here: https://infoheap.com/php-regex-delimiter-examples/
If you want to get the username out of an instagram or twitter URL, in PHP, this works well:
$url = "https://instagram.com/some.username";
$pattern = "/(?:(?:http|https):\/\/)?(?:www\.)?(?:instagram\.com|instagr\.am|twitter\.com)\/([A-Za-z0-9-_\.]+)/im";
preg_match($pattern, $url, $matches);
echo $matches[1]; // Yeah! This returns "some.username"
My regex did no include any such characters. I can't comment on subsequent advice from anyone else. My code already checked for leading dots and did not fail any tests people brought up relating to someone elses code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@verstecken You would pick one of several options in this thread and paste the regex into your function as per its manual page: https://www.php.net/manual/en/function.preg-match.php