pregmatch for url. regex - php

I have a regex that validate a specific url but it not really working. I want to validate urls like this -----> https : // example.co.nz/#![RANDOM_KEYS_HERE].
I want to do it only with https. Most importantly, the input of the user need to match https : // example.co.nz/#! but after the #!, the user can put anything he like.
Here is the code:
I know that the code is fked up xD I have a basic knowledge in that lol
#^https://example+\.[co\.nz][a-z0-9-_.]+\.[a-z]{2,4}#i
If anyone could help me to do it, it would be great! thanks!

Erm... not even close. Your regex reads as follows:
Starting from the beginning of the string...
Match literally https://exampl
Match one or more e
Match a literal .
Match one of any of these: cnoz.
Match one or more of these: a-z0-9-_.
Match a literal .
Match between 2 and 4 letters
This is nothing like what you're looking for. After all, I don't think you want this to pass:
https://exampleeeeeeeeeeee.complete.and.total.failure.-_-.lol
Instead, try this:
(^https://example\.co\.nz/#!(.*))
This regex reads as follows:
Starting from the beginning of the string...
Match literally https://example.co.nz/#!
Capture everything thereafter

Try this out:
^https:\/\/example\.co\.nz\/\#\!(.*)$
The parentheses at the end will do a sub-expression match which should allow you to pull out the ID.
if (preg_match('/^https:\/\/example\.co\.nz\/\#\!(.*)$/', $searchString, $matches)) {
$id = $matches[1];
}

if (preg_match('%^https://example\.co\.nz/#!(.+)$%i', $subject)) {
# Successful match
} else {
# Match attempt failed
}
Or you can get your [RANDOM_KEYS_HERE] part with this one
if (preg_match('%^https://example\.co\.nz/#!(.+)$%i', $subject, $regs)) {
$result = $regs[0];
} else {
$result = "";
}

You don't need regexp there. You just need to find out if string starts with some substring. Check this:
if(strpos($url, 'https://example.co.nz/#!')===0)
{
echo 'url is OK';
}
else
{
echo 'url is wrong';
}
http://www.php.net/manual/en/function.strpos.php
I hope this helps.

Related

Match URL link and ignore the page behind

I have this code that will match the url and than do something, its working good but when the url has some page behind like /board/users/somename/replies/page/2/ the code will not work. How can I only detect this part of url /board/users/somename/replies/ and ignore the rest behind? Thanks for helping.
$pageURL = $_SERVER['REQUEST_URI'];
$myurl = '/board/users/somename/replies/';
if (strtolower($pageURL) == strtolower($myurl) ) {
echo "right";
}else{
echo "wrong";
}
You just need to match the start of the string, like this:
if ( strpos(strtolower($pageURL), strtolower($myurl)) === 0 )
You also have to ensure that the leading and trailing slashes match up, so I would suggest you modify the first two lines of your code like this:
$pageURL = '/' . trim($_SERVER['REQUEST_URI'], '/');
$myurl = '/board/users/somename/replies';
EDIT: I had got the slashes wrong. That's fixed now.
simply try a regular expression
if preg_match('/'.$myurl.'/', $pageUrl) {}
this will match any url that has $myurl in it. preg_match returns 1 if the regular expression matches, and 0 if it doesn't.
if you want to exclude the URL's with page/ in it, try a negative lookahead via regex
if preg_match('/(?!'.$myurl.'page\/)'.$myurl.'/', $pageUrl) {}
The negative lookahead makes sure myurl/page/ is not in the URL and then you simply match for your url.
edit: for further information on preg_match check the php manual at preg_match
You can also use PHP stristr function to match the beggining of a string. (case-insensitive)
Try this:
$pageURL = $_SERVER['REQUEST_URI'];
$myurl = '/board/users/somename/replies/';
if (stristr($pageURL, $myurl) !== FALSE) {
echo "right";
} else {
echo "wrong";
}
I hope it helps!

PHP regex - string contains 'facebook' with all domains

I need to check, if users input URL contains facebook(.any domain). This is what I have:
preg_match("/.(facebook)+\.[A-Z]{2,4}/", $input);
But on typying www.facebook.com it returns false. Could someone help me with this? I am not very good in regex.
That is because you are only matching for uppercase letters in the last part. You may want to make it match case independent by adding a modifier:
preg_match("/.(facebook)+\.[A-Z]{2,4}/i", $input);
The next things are:
you don't need to put "facebook" into a group
You don't need to quantify facebook
if you want to match a dot, then escape it
So end up with this:
preg_match("/\.facebook\.[A-Z]{2,4}/i", $input);
you can also try this along with the answer of #stema
if(strpos($url, "facebook") !== FALSE)
{
echo "exists";
}
else
{
echo "not exists";
}

PHP regex parse my visible link

I want to check my link in a website, but I also want to check is it visible. I wrote this code:
$content = file_get_contents('tmp/test.html');
$pattern = '/<a\shref="http:\/\/mywebsite.com(.*)">(.*)<\/a>/siU';
$matches = [];
if(preg_match($pattern, $content, $matches)) {
$link = $matches[0];
$displayPattern = '/display(.?):(.?)none/si';
if(preg_match($displayPattern, $link)) {
echo 'not visible';
} else {
echo 'visible';
}
} else {
echo 'not found the link';
}
It works, but not perfect. If the link is like this:
<a class="sg" href="http://mywebsite.com">mywebsite.com</a>
the fist pattern won't work, but if I change the \s to (.*) it gives back string from the first a tag. The second problem is the two pattern. Is there any way to merge the first with negation of the second? The merged pattern has 2 results: visible or not found/invisible.
I'll try to guess.
You are having a problem if your code(one that you fetch with file_get_contents) looks like this
<a class="sg" href="http://mywebsite.com">mywebsite.com</a>
.
.
.
mywebsite.com
Your regex will return everything from first </a> tag because dot matches a new line(I guess you need it turned on, but if you dont, its 's' flag, so remove it)
Therefore
.*
will keep searching everything, so you need to make it greedy
(when its greedy it will stop searching once it finds what its looking for), like this
.*?
Your regex should look like this then
<a.*?href="http:\/\/mywebsite.com(.*?)">(.*?)<\/a>

Determining if a URL contains a certain hostname

I need to make a regular expression for php preg_match that does the following matching.
This is the function
function isValidURL($url,$searchfor){
return preg_match("/\b.$searchfor \b/i", $url);
}
I need to find the somedomain.com in the following
Possible Strings entering the function
http://www.somedomain.com
http://somedomain.com
http://www.somedomain.com/anything
http://somedomain.com/anything
http://anything/somedomain.com
So I need a regular expression that does this
http://www.somedomain.com Will Match
http://somedomain.com Will Match
http://www.somedomain.com/anything Will Match
http://somedomain.com/anything Will Match
but
http://anything/somedomain.com Will NOT match
What about using parse_url()?
if( strpos(parse_url($url, PHP_URL_HOST), 'somedomain.com') !== false )
{
// hostname contains 'somedomain.com'.
}
Try this...
$url = "http://komunitasweb.com/";
if (preg_match('/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):? (d+)?/?/i', $url)) {
echo "Your url is ok.";
} else {
echo "Wrong url.";
}
Copied from a google searh on "php url regular expression". Check google out, awesome tool. :-)
All this requires is a placeholder for the URL beginning. Excluding slashes with a negated character class [^/] might already be sufficient:
function isValidURL($url,$searchfor){
return preg_match("~http://[^/\s]*\.$searchfor(/|$|\s)~i", $url);
}
Note that this fails some edge cases, like user:pw# pairs. And no idea if your $searchfor was supposed to contain the TLD already. Also don't forget to preg_quote it.

Only Allow IMDB LINK REGEX solution PHP

I want a regex solution to allow only
http://www.imdb.com/title/ttANYNumberWOrdetc/ links
Otherwise SHOW us error.. Incorrect link
I am not too good with regex
I just create this petren ..
preg_match('/http:\/\/www.imdb.com\/title\/(.*)\//is', 'http://www.imdb.com/title/tt0087469/', $result);
Its show me corect result but i think i missed some thing..
Thanks,
How about something like this: http://(?:www\.)?imdb.com/title/tt[^/]+/.
Example:
<?php
if ( preg_match('#^http://(?:www\.)?imdb\.com/title/tt[^/]+/$#', 'http://www.imdb.com/title/tt0448303/') )
echo 'Matches' . PHP_EOL;
Explanation:
The regular expression matches a string that starts with http:// followed either by imdb.com or www.imdb.com, then /title/tt followed by any character except for a / and that ends with a /.
The # is the delimiter, the ^ indicated the beginning of the string and the $ the end.
This should work:
if (preg_match("#^(http://www.|https://www.)imdb.com/title/tt([a-zA-Z0-9]+)(?:/)(?:^[a-zA-Z0-9]+)?$#s", 'http://www.imdb.com/title/tt0364845/', $matches)) {
echo 'yay';
} else {
echo 'nay';
}

Categories