PHP regex - string contains 'facebook' with all domains - php

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";
}

Related

Regex match any single word or nothing

There have been several questions on this but none of them have lead me to the answer, they're all too specific or too long and complex. Mine is more general and simple. I have a pattern like this:
/you(?:.*)? see/
I want it to match all these sentences except the last:
you can see
you could see
you see
you could not see
At the moment it matches everything but it also matches the last sentence. I need it to only match any one single word or no word. I also tried this pattern but it doesn't quite work either:
/you(?:[^\s]+)? see/
This one does the job:
/^you(?:\s+\w+)?\s+see$/
Where (?:\s+\w+)? is an optional non capture group that matches 1 or more spaces followed by 1 or more word character (ie. [a-zA-Z-9_])
In action:
$strings = array(
'you can see',
'you could see',
'you see',
'you could not see',
);
foreach ($strings as $str) {
if (preg_match('/^you(?:\s+\w+)?\s+see$/', $str)) {
echo "$str : matches\n";
} else {
echo "$str : doesn't match\n";
}
}
Output:
you can see : matches
you could see : matches
you see : matches
you could not see : doesn't match
I would have deleted the question since I solved it shortly afterwards but there was already a response. I found just changing the pattern to this solved the problem:
/you(?:\s[^\s]+)? see/

How to match any thing dosen't contain a specific word using RegExp

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 (^ $)

pregmatch for url. regex

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.

Regex with + sign

I'm using this code to validate string.
$countrecipient ='0123456789';
preg_match('/^[0]{1}[1]{1}[0-9]{1}[0-9]{7}?$/', $countrecipient)
How if I want to validate if the number contain "+" sign in front or not?
Such as :
$countrecipient ='+0123456789';
and still need to validate the rest of the string.
I tried this:
if(preg_match('/^[+]{1}[6]{1}[0]{1}[1]{1}[0-9]{1}[0-9]{7}?$/', $countrecipient))
{
echo "Ok";
}
else
{
echo "Error";
}
It works in my pc but I'm not sure why my customer is complaining it shows him error.
Thank you.
For an optional plus in front you could use:
preg_match('/^\+?[0]{1}[1]{1}[0-9]{1}[0-9]{7}?$/', $countrecipient);
Notice how I have escaped the + with a backslash? This is because it is a regex keyword which means 1 instance or more.
$countrecipient ='0123456789';
preg_match('/^[0]{1}[1]{1}[0-9]{1}[0-9]{7}?$/', $countrecipient)
You're making things unnecessarily complicated. "[0]{1}[1]{1}[0-9]{1}" reduces to simply "01[0-9]".
To have an optional + on the front, your basic idea of using [+] should work. Let's see...
$countrecipient ='+0123456789';
if(preg_match('/^[+]{1}[6]{1}[0]{1}[1]{1}[0-9]{1}[0-9]{7}?$/', $countrecipient))
...again this can be simplified, but it simplies to /^[+]601[0-9][0-9]{7}?$/. Where did the 6 after the + come from? Does this account for your problem?
Plus has a special meaning in PCRE, it's called quantifier and has meaning of {1,}.
You may either put in into character group specification like this [+] which would literally mean one of following characters: array( '+') or escape it with \ so you'll get: \+.
Also adding {1} is implicit and you don't have to add it after one character. If you were doing this matching foo would look like: f{1}o{1}o{1}, ehm f{1}o{2} instead of foo :)
If you want to match both 0123456789 and +012345678 you should use {0,1} which has "shortcut" ?. Than your pattern would look like: /\+?/. I guess your desired regexp is:
/^\+?0?1?[0-9]([0-9]{7})?$/
The simplified form of your regex is
/\+?01[0-9]{8}/
However I recommend you use intval, is_int, ctype_digit to accomplish this.
if(intval($str)<=100000000){
// found it.
}
Based on regexp you put in the section you tried: "...preg_match('/^[+]{1}[6]{1}[0]{1}[1]{1}[0-9]{1}[0-9]{7}?$/'..."
If you would validate the string AND check if there is a '+' or not, you could use something like this:
if(preg_match('/(\+)?6011[0-9][0-9]{7}?$/', $countrecipient, $matches))
{
if ($matches[1] == '+') {
echo "Ok WITH PLUS";
} else {
echo "Ok without PLUS";
}
}
else
{
echo "Error";
}

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.

Categories