Hi why is the preg_match with brackets and determined string is not working?
Pls let me know your solution to search for "uRole('Admin')".
<?php
$check ='if(uRole("Admin")||uRole("Design")){';
preg_match('/uRole("Admin")/i', $check)? echo 'match':"";
?>
Two reasons this isn't working...
You have to escape () in REGEX (otherwise it's assuming a group)
You don't use echo in a ternary operator; it has to come before
echo (STATEMENT_TO_EVALUATE = [TRUE | FALSE]) : TRUE_VALUE : FALSE_VALUE;
Working code
$check ='if(uRole("Admin")||uRole("Design")){';
echo preg_match('/uRole\("Admin"\)/i', $check, $matches) ? "match" : "";
That is because the ( and ) is a special character in regular expressions used to create groups.
Because it is a special character, you should use a backslash to escape it:
if (preg_match('/uRole\("Admin"\)/i', $check)) {
echo 'match';
}
By the way, in this situation a simple stripos is probably more appropriate:
if (stripos($check, 'uRole("Admin")') !== false) {
echo 'match';
}
Related
I have a strange question maybe you could help me with. I am trying to check whether the given string contains special characters. The code below is working however one character seems to get exempted on the condition which is the square brackets [ ]. Can you help me with this? thank you.
$string = 'starw]ars';
if (preg_match('/[\'^£$%&*()}{##~?><>,|=_+¬-]/', $string)) {
echo 'Output: Contains Special Characters';
}else{
echo 'Output: valid characters';
}
Please note: I can't use below condition since I need to accept others characters from other languages like in arabic, chinese, etc. so it means I need to specify all characters that is not allowed.
if (!preg_match('/[^A-Za-z0-9]/', $string))
Appreciate your help. Thanks.
You forgot to add square brackets [] in your expression. I have added this \[\] in your current expression.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string = 'starw]ars';
if (preg_match('/[\[\]\'^£$%&*()}{##~?><>,|=_+¬-]/', $string))
{
echo 'Output: Contains Special Characters';
} else
{
echo 'Output: valid characters';
}
Use strpos:
$string = 'starw]ars';
if (strpos($string, ']') !== false) {
echo 'true';
}
Please see the following answer for additional information:
How do I check if a string contains a specific word in PHP?
You should add escaped square brackets to your expression.
preg_match('/[\'^£$%&*()}{##~?><>,|=_+¬-\[\]]/', $string)
EDIT: Apologies to #Thamilan, I didn't see your comment.
EDIT 2: You could also use the preg_quote function.
preg_match(preg_quote('\'^£$%&*()}{##~?><>,|=_+¬-[]', '/'), $string);
The preg_quote function will escape your special characters for you.
Try this example:
<?php
$string = 'starw]]$%ars';
if (preg_match('/[\'\/~`\!##\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $string))
{
echo 'Output: Contains Special Characters';
} else
{
echo 'Output: valid characters';
}
?>
Output:
Output: Contains Special Characters
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 some words with | between each one and I have tried to use preg_match to detect if it's containing target word or not.
I have used this:
<?php
$c_words = 'od|lom|pod|dyk';
$my_word = 'od'; // only od not pod or other word
if (preg_match('/$my_word/', $c_words))
{
echo 'ok';
}
?>
But it doesn't work correctly.
Please help.
No need for regular expressions. The functions explode($delimiter, $str); and in_array($needle, $haystack); will do everything for you.
// splits words into an array
$array = explode('|', $c_words);
// check if "$my_word" exists in the array.
if(in_array($my_word, $array)) {
// YEP
} else {
// NOPE
}
Apart from that, your regular expression would match other words containing the same sequence too.
preg_match('/my/', 'myword|anotherword'); // true
preg_match('/another/', 'myword|anotherword'); // true
That's exactly why you shouldn't use regular expressions in this case.
You can't pass a variable into a string with single quotes, you need to use either
preg_match("/$my_word/", $c_words);
Or – and I find that cleaner :
preg_match('/' .$my_word. '/', $c_words);
But for something as simple as that I don't even know if I'd use a Regex, a simple if (strpos($c_words, $my_word) !== 0) should be enough.
You are using preg_match() the wrong way. Since you're using | as a delimiter you can try this:
if (preg_match('/'.$all_words.'/', $my_word, $c_words))
{
echo 'ok';
}
Read the documentation for preg_match().
Please I have I want to use regex to preg_match this kind of string :
$liste = 'bla0bla-__my_separator_-01blabla';
I've tried :
if(preg_match('/^[a-zA-Z0-9]_my_separator_[a-zA-Z0-9]$/', $liste))
echo 'ok';
else echo 'not ok';
But this returns always not ok.
Please masters any advise ?
PS : I think that the problem is the _ and the - that what I've tried does not support !
Thanks in advance.
Change your regex pattern to this:
'/^[a-zA-Z0-9_\-]+?_my_separator_[a-zA-Z0-9_\-]+?$/'
If you're only trying to determine if a static substring exists in a string you should use strpos():
if (strpos('_my_separator_', $liste) !== false) {
echo 'ok';
}
You're missing the repeater + and - and _ in your whitelists:
/^[a-zA-Z0-9\-_]+_my_separator_[a-zA-Z0-9\-_]+$/
Your orignal regex would match things like:
A_my_seperator_B
0_my_seperator_C
but not:
AB_my_seperator_C
I'm trying to find special characters with characters like <?, <?php, or ?> in a string. The code below works to find "php" in the string anywhere no matter if it's PHP, php, or phpaPHPa.
<?php
$searchfor = "php";
$string = "PHP is the web scripting language of choice.";
if (preg_match("/".$searchfor."/i", $string)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>
I need a similar code that finds special characters like <?, <?php, or ?> in the string. Any suggestions?
You can use same code. Just make sure to escape regex special characters when using them in matching. The question mark must be escaped so your $searchfor becomes <\?php
Using strpos (or stripos) will be faster than using RegEx'es...
if(false === strpos($text, '<?'))
echo 'Match was not found';
else
echo 'Match was found';