dlf1987 Posted November 5, 2011 Share Posted November 5, 2011 im trying to preg_replace the word product in a string, but if i use something like .. $k = 'product'; $str = 'product and product.test' preg_replace("/\b$k\b/", 'replace_with_me', $str); it will replace product and product.test instead of just product. Quote Link to comment https://forums.phpfreaks.com/topic/250491-preg_replace-exact-match/ Share on other sites More sharing options...
dlf1987 Posted November 5, 2011 Author Share Posted November 5, 2011 i got it to work by replacing the 2nd \b with a \s but then it removes the following whitespace... product and product.test to replace_with_meand product.test Quote Link to comment https://forums.phpfreaks.com/topic/250491-preg_replace-exact-match/#findComment-1285179 Share on other sites More sharing options...
trq Posted November 5, 2011 Share Posted November 5, 2011 Be sure to put the whitespace back then: preg_replace("/\b$k\b/", ' replace_with_me ', $str); Quote Link to comment https://forums.phpfreaks.com/topic/250491-preg_replace-exact-match/#findComment-1285184 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.