gazever Posted May 29, 2007 Share Posted May 29, 2007 Hi Can anyone help, I have just spent about an hour googling str replace using wildcards to no joy. Can someone suggest how i would go about replacing: "(3,6) (9)" within a string to: "(3, 6)" however I need to use wildcards for the numbers as these could be "(2,4) (6)" for instance this searching goes thorugh an array containing crossword clues, so not every string will have these specifics, many will just have the number, i.e "(9)" Hope this makes sense! Many Thanks Gazever Quote Link to comment https://forums.phpfreaks.com/topic/53400-str_replace-using-wildcards/ Share on other sites More sharing options...
trq Posted May 29, 2007 Share Posted May 29, 2007 Look at preg_replace, and if you need help wth a pattern, we have a regex board, but you'll need to be allot clearer in your description. Quote Link to comment https://forums.phpfreaks.com/topic/53400-str_replace-using-wildcards/#findComment-263840 Share on other sites More sharing options...
svivian Posted May 29, 2007 Share Posted May 29, 2007 Regex search: '/\(([0-9]+),([0-9]+)\) \(([0-9]+)\)/' Regex replace: '(\1, \2) (\3)' I think that should work. If you are changing files or anything, make sure you backup or are able to undo beforehand. Quote Link to comment https://forums.phpfreaks.com/topic/53400-str_replace-using-wildcards/#findComment-263848 Share on other sites More sharing options...
gazever Posted May 29, 2007 Author Share Posted May 29, 2007 @thorpe, yup worked a treat, thanks had to replace the brackets first as these interferred with the preg code i think, so changed them before then reverted them back. <?php ////// replace --->( and --->) to not mess up the pregreplace foreach ($newpuzzle as &$value) { $value = str_replace("(", "LEFTBRACKET", $value); $value = str_replace(")", "RIGHTBRACKET", $value); } ////// replace "(?,?) (?)" with "(?, ?)" foreach ($newpuzzle as &$value) { $old = '/(LEFTBRACKET)(.*)(,)(.*)(RIGHTBRACKET LEFTBRACKET)(.*)(RIGHTBRACKET)/'; $new = 'LEFTBRACKET${2}${3} ${4}RIGHTBRACKET'; $newvalue = preg_replace($old, $new, $value); $value = $newvalue; } ?> @svivian, Thanks but already sorted the solution from Thorpes suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/53400-str_replace-using-wildcards/#findComment-263860 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.