Buyocat Posted September 6, 2007 Share Posted September 6, 2007 I want to find the first character which is NOT alphanumeric (also quotes are allowed), but I'm not sure how to express that in regex. Can anyone lend a hand? The closest I can get is: /\W/ which almost works except it doen't catch the quote (double or single) correctly. Thanks! Quote Link to comment Share on other sites More sharing options...
effigy Posted September 6, 2007 Share Posted September 6, 2007 That should work, but it will bypass _. [^A-Za-z0-9] is better (unless you're also dealing with "special" characters, like ß). Does this not show a double quote for you? <pre> <?php $string = 'abc0976123kmhsfkshdf"!(*'; preg_match('/\W/', $string, $matches); print_r($matches); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Buyocat Posted September 6, 2007 Author Share Posted September 6, 2007 I actually get an error running your script for some reason: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /Users/Kanti/Sites/test.php on line 3 At any rate I ran my own test and find that the double quote and single quote do not pass through the current regex. Again, I want to find the first character which is not alphanumeric or a quote (single or double). As for symbols such as hyphons and underscores, I'm willing to have them work or not. Thanks for your response. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 6, 2007 Share Posted September 6, 2007 That's awkward. Was that the only code in the entire file? Quote Link to comment Share on other sites More sharing options...
Buyocat Posted September 6, 2007 Author Share Posted September 6, 2007 Ya, I'm not sure why it was failing. I only spent a few minutes trying to fix it, but failed. It's odd since my own code is public static function findFirst($haystack, $pattern) { preg_match($pattern, $haystack, $matches, PREG_OFFSET_CAPTURE); if (empty($matches)) return -1; return $matches[0][1]; } So, its very similar. Maybe the lack of the flag was the problem? Quote Link to comment Share on other sites More sharing options...
effigy Posted September 6, 2007 Share Posted September 6, 2007 What's your PHP version? Quote Link to comment Share on other sites More sharing options...
Buyocat Posted September 6, 2007 Author Share Posted September 6, 2007 5.2.1 Quote Link to comment Share on other sites More sharing options...
effigy Posted September 7, 2007 Share Posted September 7, 2007 Same here. 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.