dsp77 Posted September 1, 2011 Share Posted September 1, 2011 i'm having trouble in combining the 2 preg_replace into 1. basically i have a string witch strange chars, first i remove all except alphanumeric chars and the remaining string has 1 or 2 spaces and i replace them with dash. strtolower(preg_replace('/\s+/', '-', preg_replace("/[^0-9a-zA-Z]/i", ' ',$a['item']))); Quote Link to comment Share on other sites More sharing options...
salathe Posted September 1, 2011 Share Posted September 1, 2011 Why do you want to combine two completely unrelated regular expressions, with two completely unrelated replacement values, into one? Quote Link to comment Share on other sites More sharing options...
dsp77 Posted September 1, 2011 Author Share Posted September 1, 2011 from what i understand is ok like this? when i code i'm trying to make the simplest and fastest code that's why asked if can be one preg_replace doing all the job. Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 1, 2011 Share Posted September 1, 2011 If you read the manual it will tell you that preg_replace will accept an array of strings as a pattern, and an array of strings as replacement. Quote Link to comment Share on other sites More sharing options...
salathe Posted September 1, 2011 Share Posted September 1, 2011 I was hoping that you would rethink the logic being used here, but maybe that was too much to ask. Think about what your regular expressions are doing. 1. Replace any single non-alphanumeric character with a space. 2. Replace any occurrence of one or more consecutive whitespace characters with a dash. There is never any chance of an alphanumeric character also being a whitespace character, it's just not possible. Anything non-alphanumeric will become a space, then any group of spaces will become one dash. The above two steps can be replaced with a single one doing the same job: 1. Replace one or more consecutive non-alphanumeric characters with a dash. strtolower(preg_replace('/[^0-9a-zA-Z]+/', '-', $a['item'])); Quote Link to comment Share on other sites More sharing options...
dsp77 Posted September 2, 2011 Author Share Posted September 2, 2011 thank you salathe this is the code i was trying to achieve 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.