John Davidson

regex - preg_match php with brackets and determined string is not working

0 comments
Message:


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':"";
?>


My Answer:
If you are having trouble with using preg_match in PHP with brackets and a specific string, it could be due to incorrect syntax or escaping characters.

Here is an example of how you can use preg_match with brackets and a determined string:

php
$string = "Hello [World]!";
$pattern = "/\[World\]/";

if (preg_match($pattern, $string)) {
echo "Match found!";
} else {
echo "No match found.";
}


In this example, we are searching for the string "[World]" within the variable $string. The brackets are escaped with backslashes to ensure they are treated as literal characters in the regular expression pattern.

If you are still having trouble, please provide more details or specific examples of the issue you are facing so that I can assist you further.

Rate this post

4 of 5 based on 4556 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us