jini01 Posted July 9, 2007 Share Posted July 9, 2007 $returnValue=preg_replace("/[^0-9 \d]/i", "", $text); I am trying for the user to be only able to input numbers. I dont understand the \d or /i please someone explain that to me. thanks Link to comment https://forums.phpfreaks.com/topic/59084-can-someone-explain-the-following-line-of-code-to-me/ Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 \d means any number between 0 and 9. The /i is a flag to turn case sensitivity off so "Cat" is treated the same as "cat" Link to comment https://forums.phpfreaks.com/topic/59084-can-someone-explain-the-following-line-of-code-to-me/#findComment-293356 Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 $returnValue=preg_replace("/[^0-9 \d]/i", "", $text); I can't see anything being replaced there as you're not grabbing a group. Link to comment https://forums.phpfreaks.com/topic/59084-can-someone-explain-the-following-line-of-code-to-me/#findComment-293357 Share on other sites More sharing options...
jini01 Posted July 9, 2007 Author Share Posted July 9, 2007 What do you mean by group ? Sorry im not that proficient with preg replace Link to comment https://forums.phpfreaks.com/topic/59084-can-someone-explain-the-following-line-of-code-to-me/#findComment-293364 Share on other sites More sharing options...
MadTechie Posted July 9, 2007 Share Posted July 9, 2007 here what the code should look like $returnValue = preg_replace('/[^\d]/', '', $text); replace whats NOT (^) digits(\d) with nothing ('') Link to comment https://forums.phpfreaks.com/topic/59084-can-someone-explain-the-following-line-of-code-to-me/#findComment-293378 Share on other sites More sharing options...
jini01 Posted July 9, 2007 Author Share Posted July 9, 2007 youre bad ass thanks man Link to comment https://forums.phpfreaks.com/topic/59084-can-someone-explain-the-following-line-of-code-to-me/#findComment-293389 Share on other sites More sharing options...
Wildbug Posted July 9, 2007 Share Posted July 9, 2007 $returnValue=preg_replace("/[^0-9 \d]/i", "", $text); Note that there is also a space (' ') in the original code. If that's correct, and you wish to maintain that functionality, you should also include it in MadTechie's example: $returnValue = preg_replace('/[^\d ]/', '', $text); Then it will only keep digits and spaces. Link to comment https://forums.phpfreaks.com/topic/59084-can-someone-explain-the-following-line-of-code-to-me/#findComment-293560 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.