micah1701 Posted May 27, 2010 Share Posted May 27, 2010 I've used regex a bit in the past with great success but I'm a little stumped on how to do something. I basically need to do a preg_replace withing another regex. I Need to replace the spaces between words with under_score characters, but only for spaces within quotes, not for all the spaces in the string. In other words, I need this: Here is a long "test string that" also contains quotes. It actually has "multiple quotes" to look end up looking like this: Here is a long "test_string_that" also contains quotes. It actually has "multiple_quotes" could someone point me in the best direction? thanks! Quote Link to comment Share on other sites More sharing options...
Alex Posted May 27, 2010 Share Posted May 27, 2010 You can use preg_replace_callback. $str = 'Here is a long "test string that" also contains quotes. It actually has "multiple quotes"'; $newstr = preg_replace_callback( '~"(.+?)"~', create_function( '$match', 'return str_replace(" ", "_", $match[0]);' ), $str ); echo $newstr; Quote Link to comment Share on other sites More sharing options...
micah1701 Posted May 27, 2010 Author Share Posted May 27, 2010 Wow, Thanks Alex. Thats perfect. I've never used the callback function with a preg_replace. Very nifty! Thanks for showing me something new 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.