mzhou88 Posted February 23, 2007 Share Posted February 23, 2007 $string='#function#hell world test#'; $pattern='/#function#(\d+)#/i'; $replace='<a href="http://gmail.com">${1}</a>'; preg_replace($pattern, $replace, $string) For some reason, PHP cannot recognize my pattern when $string has a space in the variable. If I put hell_world_test instead of hell world test, it will work, but how do i do it so that it accepts spaces as well? Thank you for all your help. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 23, 2007 Share Posted February 23, 2007 Your pattern is looking for \d+, which indicates 1 or more digits; "hell world test" does not contain any digits. Quote Link to comment Share on other sites More sharing options...
mzhou88 Posted February 23, 2007 Author Share Posted February 23, 2007 Sorry, that is a mistake on my part for mis-writing the code. In my script I had \w+, not \d+. Thank you. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 23, 2007 Share Posted February 23, 2007 \w breaks down to [A-Za-z0-9_], which does not include spaces. Use [\w ]+. Quote Link to comment Share on other sites More sharing options...
mzhou88 Posted February 23, 2007 Author Share Posted February 23, 2007 Wow, thats a quick reply. And it works too. Thank you. =p] 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.