etrader Posted January 24, 2011 Share Posted January 24, 2011 I have a string with the structure of "word(11), word2(43), word3(99)". I want to delete the number between parenthesis to get "word, word2, word3"; but there's something wrong with this: $words = preg_replace("/(0-9)/", "", $words); Quote Link to comment https://forums.phpfreaks.com/topic/225508-deleting-numbers-between-parenthesis/ Share on other sites More sharing options...
Psycho Posted January 24, 2011 Share Posted January 24, 2011 One reason is because parens have special significance within regular expressions and that code is not interpreting those parens as parens. You will need to escape them. $words = preg_replace("/\([0-9]+\)/", "", $words); Quote Link to comment https://forums.phpfreaks.com/topic/225508-deleting-numbers-between-parenthesis/#findComment-1164468 Share on other sites More sharing options...
Roman Fedorov Posted January 24, 2011 Share Posted January 24, 2011 Hi, the pattern you wrote will search exactly the string "0-9", if you wanted to specify digits from 0 to 9 you should write "[0-9]" Anyway in your case try with $words = preg_replace("/\\(\\d.\\)/", "", $words); Quote Link to comment https://forums.phpfreaks.com/topic/225508-deleting-numbers-between-parenthesis/#findComment-1164470 Share on other sites More sharing options...
etrader Posted January 24, 2011 Author Share Posted January 24, 2011 Thanks for the effective solutions and useful guides Quote Link to comment https://forums.phpfreaks.com/topic/225508-deleting-numbers-between-parenthesis/#findComment-1164475 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.