mottwsc Posted October 20, 2008 Share Posted October 20, 2008 I'm allowing users to send an email as an invitation. I pre-fill it (using \n for new line, let them edit it and check if after that. I always get a negative response in the ereg when including \ in the string, and I can't figure out how to fix it. Any suggestions? $invitationClean = "Hello,\n\nYou are invited."; if( !ereg("^([A-Za-z0-9',. -!\\]{0,250})+$", $invitationClean) ) { echo "invitationClean - bad data<br>"; } else { echo "invitationClean - OK data<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/129179-problem-with-ereg-and-backslashes/ Share on other sites More sharing options...
nrg_alpha Posted October 20, 2008 Share Posted October 20, 2008 There are some issues with your code: - Since you are using double quotes for your $invitationClean variable, \n becomes a newline, so you can use the hex \x0A to match this... - I would advise not using ereg, as by default come PHP 6, this will not be supported. Use preg instead. - In your character class, you use a dash somewhere in the middle.. this automatically becomes a range.. when using a dash in such circumstances, you need to declare the dash as the very first or very last character in the character class. So here is my attempt: $invitationClean = "Hello, \n\n You are invited."; if( preg_match("#^(?:[ a-z0-9'.,!-]|\x0A)+$#i", $invitationClean) && strlen($invitationClean) < 251 ){ echo 'invitationClean - OK data <br />'; } else { echo 'invitationClean - bad data <br />'; } I urge you to view this link and this link to learn more about preg (as it will take too much time to properly type out full fledged explanations here). Hope this helps.. cheers, NRG Quote Link to comment https://forums.phpfreaks.com/topic/129179-problem-with-ereg-and-backslashes/#findComment-670236 Share on other sites More sharing options...
DarkWater Posted October 20, 2008 Share Posted October 20, 2008 Also, I'd suggest always using single quotes for regular expressions so that you don't have escaping dilemmas. >_< Quote Link to comment https://forums.phpfreaks.com/topic/129179-problem-with-ereg-and-backslashes/#findComment-670293 Share on other sites More sharing options...
nrg_alpha Posted October 20, 2008 Share Posted October 20, 2008 Also, I'd suggest always using single quotes for regular expressions so that you don't have escaping dilemmas. >_< Good point... I typically use only single quotes as a rule.. so a re-write could look like this: $invitationClean = 'Hello, \n\n You are invited.'; if( preg_match('#^[\\\ a-z0-9\'.,!-]+$#i', $invitationClean) && strlen($invitationClean) < 251 ){ echo 'invitationClean - OK data <br />'; } else { echo 'invitationClean - bad data <br />'; } So in this case, I still do require an escape (the ' in the pattern). No big deal though. Quote Link to comment https://forums.phpfreaks.com/topic/129179-problem-with-ereg-and-backslashes/#findComment-670309 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.