TD Posted February 26, 2008 Share Posted February 26, 2008 Hello! I need to split words from numbers. $code = "Johny234"; $split = explode(" ", $code); $codetxt = $split[0]; $codenr = $split[1]; Hope that someone can help me... many thanks! Link to comment https://forums.phpfreaks.com/topic/93099-explode-function-split-word-from-number/ Share on other sites More sharing options...
paul2463 Posted February 26, 2008 Share Posted February 26, 2008 you will have to iterate over the string until you come across a number, then you will use substr() to break the string up Link to comment https://forums.phpfreaks.com/topic/93099-explode-function-split-word-from-number/#findComment-476957 Share on other sites More sharing options...
obsidian Posted February 26, 2008 Share Posted February 26, 2008 If it is always a word then number combination, you could easily just do this: <?php $code = 'Johnny456'; preg_match('|^([a-z]+)([\d]+)$|i', $code, $match); $codetxt = $match[1]; $codenum = $match[2]; ?> Link to comment https://forums.phpfreaks.com/topic/93099-explode-function-split-word-from-number/#findComment-476959 Share on other sites More sharing options...
TD Posted February 26, 2008 Author Share Posted February 26, 2008 Yes i will have it always that word first and then the number comes. Many thanks it works very well! Cheers! Link to comment https://forums.phpfreaks.com/topic/93099-explode-function-split-word-from-number/#findComment-476964 Share on other sites More sharing options...
TD Posted February 26, 2008 Author Share Posted February 26, 2008 There may be a space between the word and number, what should i do, it wont work!??! Link to comment https://forums.phpfreaks.com/topic/93099-explode-function-split-word-from-number/#findComment-476971 Share on other sites More sharing options...
Orio Posted February 26, 2008 Share Posted February 26, 2008 Small modification... <?php $code = 'Johnny456'; preg_match('|^([a-z]+) ?([\d]+)$|i', $code, $match); $codetxt = $match[1]; $codenum = $match[2]; ?> Orio. Link to comment https://forums.phpfreaks.com/topic/93099-explode-function-split-word-from-number/#findComment-476973 Share on other sites More sharing options...
TD Posted February 26, 2008 Author Share Posted February 26, 2008 BIG BIG BIG Thanks Great community... Link to comment https://forums.phpfreaks.com/topic/93099-explode-function-split-word-from-number/#findComment-476975 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.