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. Link to comment https://forums.phpfreaks.com/topic/39831-solved-preg_replace-problem-with-spaces/ 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. Link to comment https://forums.phpfreaks.com/topic/39831-solved-preg_replace-problem-with-spaces/#findComment-192435 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. Link to comment https://forums.phpfreaks.com/topic/39831-solved-preg_replace-problem-with-spaces/#findComment-192469 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 ]+. Link to comment https://forums.phpfreaks.com/topic/39831-solved-preg_replace-problem-with-spaces/#findComment-192472 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] Link to comment https://forums.phpfreaks.com/topic/39831-solved-preg_replace-problem-with-spaces/#findComment-192481 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.