Jeffro Posted April 26, 2011 Share Posted April 26, 2011 Actually.. I'm assuming I use substr, but feel free to correct me if there's a better way. I have an ever changing list that I need to knock the from numbers off of. The numbers can be different lenghts, such as.. 1. item number 1 12. item number 12 101. item number 101 How do I remove the number, period and blank space for each item, since the length of the number may always differ? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/234784-how-do-i-use-substr-to-remove-numbers-only/ Share on other sites More sharing options...
micah1701 Posted April 26, 2011 Share Posted April 26, 2011 you don't want to use substr, you want to use a regular expression. look into preg_replace something like: <?php $value = "12345. some some string"; $match = "/^[0-9]. /"; $replace = ""; $value = preg_replace($match,$replace,$value); ?> //this probably won't work 'cause i suck at regex, but someone can come along and fix it Quote Link to comment https://forums.phpfreaks.com/topic/234784-how-do-i-use-substr-to-remove-numbers-only/#findComment-1206597 Share on other sites More sharing options...
drisate Posted April 26, 2011 Share Posted April 26, 2011 You can explode the dot and keep only the number part <?php // 1. item number 1 $explode = explode ('.', $number); echo $explode[0]; // 1 echo $explode[1]; // item number 1 ?> Quote Link to comment https://forums.phpfreaks.com/topic/234784-how-do-i-use-substr-to-remove-numbers-only/#findComment-1206598 Share on other sites More sharing options...
wildteen88 Posted April 26, 2011 Share Posted April 26, 2011 Or Use regex $text = "1. item number 1 12. item number 12 101. item number 101"; $text = preg_replace('~\d+\.\s~', '', $text); echo $text; Quote Link to comment https://forums.phpfreaks.com/topic/234784-how-do-i-use-substr-to-remove-numbers-only/#findComment-1206600 Share on other sites More sharing options...
micah1701 Posted April 26, 2011 Share Posted April 26, 2011 drisate's explode() option is how I probably would have done it since regex is annoying. the only downside is if your text string may have more than one "." in it. inwhich case your array will have more than just 2 levels. also, wildteen88 is much better at regex then me Quote Link to comment https://forums.phpfreaks.com/topic/234784-how-do-i-use-substr-to-remove-numbers-only/#findComment-1206601 Share on other sites More sharing options...
Jeffro Posted April 26, 2011 Author Share Posted April 26, 2011 I used wildteens regex and it worked beautifully! I was just about to ask if the implode would cause that problem. Good to know! Thank you all! Wildteen.. you saved me again! Gracious!! Quote Link to comment https://forums.phpfreaks.com/topic/234784-how-do-i-use-substr-to-remove-numbers-only/#findComment-1206604 Share on other sites More sharing options...
kenrbnsn Posted April 26, 2011 Share Posted April 26, 2011 If you use the explode method, you can use the little used third parameter to explode to limit the number of parts returned: <?php $strs = array('1. item number 1', '12. item number 12', '101. item number 101'); foreach ($strs as $str) { list (,$str) = explode('.',$str,2); echo ltrim($str) . "<br>\n"; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/234784-how-do-i-use-substr-to-remove-numbers-only/#findComment-1206605 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.