programming.name Posted November 17, 2012 Share Posted November 17, 2012 Hi, Please see the code below: $string = 'I want to be a PHP geek!'; if (preg_match('/'. urldecode($_GET['no']) . '/i', $string)) //do something issue 1: When I search with parenthesis '(' I got the following error: Warning: preg_match(): Compilation failed: missing ) at offset 1 and with ')': Warning: preg_match(): Compilation failed: unmatched parentheses at offset 0 isseu 2: I want to search letter by letter; please consider the following snippet: $string = 'Do you want to be a PHP geek?'; preg_match('/php want/i', $string) ? $var = 'Cool': $var = 'Noooooo'; I want it to have me the $var "Cool". How can I get it done please? thanks for helping me. Quote Link to comment Share on other sites More sharing options...
MMDE Posted November 17, 2012 Share Posted November 17, 2012 (edited) Do you check if $_GET is set before you use it? Though, I only get Undefined index: no, when I don't set it. <?php $string = 'some text test'; if (preg_match('/'. urldecode($_GET['no']) . '/i', $string)) echo 'TRUE: '.$_GET['no']; else echo 'FALSE '.$_GET['no'] ?> ^ Worked as expected for me. results: TRUE: test TRUE: text FALSE string TRUE: some FALSE some text string TRUE: some text test etc BUT watch out for special characters like (, ) and many others: Warning: preg_match() [function.preg-match]: Compilation failed: unmatched parentheses at offset 4 FALSE some) Warning: preg_match() [function.preg-match]: No ending delimiter '/' FALSE some\ Warning: preg_match() [function.preg-match]: Unknown modifier '/' FALSE some/ I would strongly suggest not using user input in the regex pattern, unless you are very sure you've tested it to be safe already. Edited November 17, 2012 by MMDE Quote Link to comment Share on other sites More sharing options...
programming.name Posted November 17, 2012 Author Share Posted November 17, 2012 Thanks for the reply. I mean I need a multiple strings searched with no sequential or single string. if the string given is as below: “In veterinary school we studied the brain of the hippopotamus. At that time most students stayed on the main campus, while I stayed on the hippocampus.” and I enter "campus school(bolded)" at the same time I want it to be true. I think preg_match searches one sting or adjacent strings only. Thanks Quote Link to comment Share on other sites More sharing options...
Christian F. Posted November 17, 2012 Share Posted November 17, 2012 So, if I understand you correct, the resulting RegExp you're trying to match to the string is the following? '/campus school(bolded)/i' If so, then you've clearly misunderstood what Regular Expressions are, and how to actually use them. The above RegExp will only match a string containing the text "campus schoolbolded", and save the "bolded" part in a sub group of its own. While Regular Expressions are a tool to parse text written in any regular language, it is not a natural language reader. It is simply a tool which can be used to define the rules of the patterns you want to match, the context and grammatical implications is something you have to take into account yourself. All the RegExp engine can do, is try to match the text against the pattern you've specified.they do what they do. Should I have misunderstood your intent, please explain a bit further on what it is, exactly, you're trying to do. Quote Link to comment Share on other sites More sharing options...
MMDE Posted November 17, 2012 Share Posted November 17, 2012 (edited) To further extend upon what Christian F. said... You can easily write a script that makes a regex which will make preg_match return true if at least one of the words in the input are in the string. word1|word2|word3 etc You can also make preg_match return true if one of the words input are found as a whole word in the script, and not the part of a word: ((\W|^)word1(\W|$))|((\W|^)word2(\W|$))|((\W|^)word3(\W|$)) The hard part comes when you want it to contain all the inputted words in one regex pattern... Then you get a very long permutated string, which I totally don't recommend (might be there is some way around it I don't know about). In this case I think a better option would be to loop through all the words and make sure you find them all in the string. function match_input($input, $string){ $one_match = false; $words = explode(' ', $input); $wc = count($words); for($i=0; $i<$wc; i++){ if(preg_match('/\A\s\Z/', $words[$i])){ continue; } if(!preg_match('/(\W|^)'.$words[$i].'(\W|$)/', $string)){ return false; }else{ $one_match = false; } } if($one_match){ return true; } return false; } Sorry, did a couple of typos, but I think I've fixed 'em all now! I also just added another small change to one of my codes, to check if the $words[$i] only contained white-space characters, and to ignore those, and also to return false if no words was actually found in the string. Edited November 17, 2012 by MMDE Quote Link to comment Share on other sites More sharing options...
DavidAM Posted November 17, 2012 Share Posted November 17, 2012 If you need to use user input in a regular expression, you should run it through preg_quote first. That will escape any special characters so the user expression is searched as a literal string. $string = 'I want to be a PHP geek!'; if (preg_match('/'. preg_quote($_GET['no'], '/') . '/i', $string)) //do something This should get rid of the compilation error when the user's input contains unbalanced parenthesis Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted November 17, 2012 Share Posted November 17, 2012 As a side note to the original code posted, from the PHP manual: The superglobals $_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.