dev1902 Posted August 12, 2013 Share Posted August 12, 2013 Hello guys, i'm trying to separate something like this: 15969_D55T_000 this: 3105_C55_00148 and this : 15969_D55T_00 using php. I have a few letter and number combination like these in a database that i already access. I was wondering how i would be able to just make it 15969_D55T, 3105_C55. I want help with a general php code that will help me to get ride of the extra "_xxx or _xx or _xxxx" after "xxxx_xxx" As i said before, All this information is on a database that I access. Thank you Very Much. Quote Link to comment Share on other sites More sharing options...
requinix Posted August 12, 2013 Share Posted August 12, 2013 (edited) You can break it apart by underscores and then put the first two pieces back together. [edit] Or strtok strtok($string, "_") . strtok("_") Edited August 12, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
dev1902 Posted August 13, 2013 Author Share Posted August 13, 2013 Thanks, I actually ended up using preg_replace as it was easier than the methods you suggested. Thank you though. Quote Link to comment Share on other sites More sharing options...
requinix Posted August 13, 2013 Share Posted August 13, 2013 (edited) Don't use regular expressions unless you absolutely must. They're much slower and more difficult to understand than simpler functions like explode(). Edited August 13, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
dev1902 Posted August 13, 2013 Author Share Posted August 13, 2013 I know, but with the script I already had, that was really the best choice. It runs well for what me. It was extremely easy to write : preg_replace("/_[0-9]{0,5}$/", "",$Id); Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted August 13, 2013 Share Posted August 13, 2013 Requinix's example is just as easy and is quicker. <?PHP $Id = '15969_D55T_000'; $Id = strtok($Id, "_") .'_'. strtok("_"); echo $Id; ?> Quote Link to comment Share on other sites More sharing options...
dev1902 Posted August 13, 2013 Author Share Posted August 13, 2013 They're over 25k and some of them only have 1 _ and some have more/less numbers . Example : 15607_249 or 357_C679_50984 Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted August 13, 2013 Share Posted August 13, 2013 (edited) The code I posted works for those examples too. Just need this line: $Id = strtok($Id, "_") .'_'. strtok("_"); I only wrote the rest as an example, so you can see it's output. Edited August 13, 2013 by PaulRyan Quote Link to comment Share on other sites More sharing options...
Solution dev1902 Posted August 13, 2013 Author Solution Share Posted August 13, 2013 Thanks a lot guys. My client seems to be fine preg_replace. I will look into it more though. Thanks again. 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.