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.
Related
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!
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";
}
This is my regex string:
'(?!('.$exceptions.')((\W+)|$))[a-zA-Z\-_]+/?$'
$exceptions is a variable contains a string like this :
word1|word2|word3|word4|wordN
I just want to remove the section a-zA-Z which means I want to just delete the rule which checking english chars, because of unicode.
A sample :
$exception ='word1|word3|word3|word4' ;
$myword = 'a-unicode-statement-like-سلام' ;
If $myword compared with the regex rules string it will not match that because of سلام
it is not in a-zA-z range i just want remove this limitation (a-zA-Z)
Try adding something to match everything else, instead of your a-zA-Z rule.
'(?!('.$exceptions.')(.*))'
EDIT:
After reading your comment below. Maybe a better solution is to use the one proposed for this question: wordpress: how to check if the slug contains a specific word?
You can then check using something like this:
$url = $_SERVER["REQUEST_URI"];
$isException = strpos($url, 'word1');
if ($isException !== false)
{
//url contains word in exceptions!
}
From what I understand, I think you're looking for this:
$exceptions = ["word1","word2","word3"];
// or $exceptions = explode("|",$exceptions) to work with what you have already
if( in_array($string,$exceptions)) {
// word is in exceptions
}
How to match any thing dosen't contain a specific word using RegExp
Ex:
Match any string doesn't contain 'aabbcc'
bbbaaaassdd // Match this
aabbaabbccaass // Reject this
If you're just after this sequence of characters, don't use a Regular Expression. Use strpos().
if (strpos('aabbaabbccaass', 'aabbcc') !== false) {
echo 'Reject this.'
}
Note: Be sure to read the warning in the manual about strpos() return values.
You can use negative lookahead:
(?!.*?aabbcc)^.*$
Live Demo: http://www.rubular.com/r/4Exbf7UdDv
PHP Code:
$str = 'aabbaabbccaass'; //or whatever
if (preg_match('/(?!.*?aabbcc)^.*$/', $str))
echo "accepted\n";
else
echo "rejected\n";
try this to avoid some sequences of letters :
^((?!aabbcc).)*$
try this:
if(preg_match('/aabbcc/', $string) == 0) {
[ OK ]
}
else {
[ NOT OK ]
}
You can use this to describe a substring that doesn't contain aabbcc:
(?>[^a]++|a(?!abbcc))*
for the whole string, just add anchors (^ $)
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.