problems with regular expression and grep
Hi,
I would appreciate help with the following. if $userinput is the variable representing user input. If the search term is $newuserinput = "[^0123456789.:]".$userinput."[^0123456789.:]"; and within a php file I call exec ($mydirectory."grep -E "newuserinput $mystring", $searchoutput, $searchstatus); I have found that this grep command works most of the time except when there is a space character in the variable $userinput. What can I do to this expression to make it work when there are spaces in the $userinput variable? thanks! |
I am curious why you don't just use the regular expression engine built into php... it would be much more efficient than a call to exec. If you still want to exec, you'll need to quote the regexp.
|
Hi Matir,
Thanks for your help. If I still want to use exec how would I quote the regexp I am already using the following in my php file. $newuserinput = "[^0123456789.:]".$userinput."[^0123456789.:]"; exec ($mydirectory."grep -E $newuserinput $mystring", $searchoutput, $searchstatus); So would I change the line with the $newuserinput to: $newuserinput = "[^0123456789.:]"."$userinput"."[^0123456789.:]"; ie. the $userinput variable input quoted? thanks! |
Actually, I tried quoting the $userinput variable recently. It doesn't work.
Any ideas? $newuserinput = "[^0123456789.:]"."$userinput"."[^0123456789.:]"; thanks! |
Quote:
Code:
userinput=$(cat $userinput | tr " " "_") |
make your exec command like "grep '$userinput'" or similar. (Should quote it properly).
|
Thanks Matir,
I used your suggestion and the grep worked properly. also thanks ahh I used a modification of your idea for another part of my code since spaces weren't allowed somewhere else |
All times are GMT -5. The time now is 04:07 PM. |