colinsp Posted February 26, 2009 Share Posted February 26, 2009 I have a little problem. I have searched the php manual and a couple of books without success. I am trying to get my head round the ltrim command. Say I have a variable and it contains 12345-ABCDEF and I just want to assign the letters to another variable the code I have tried is $one ="12345-ABCDEF"; $two = ltrim($one, "-"); echo $two"\ n"; I still get 12345-ABCDEF instead of ABCDEF so it has to be me not understanding how this function works properly. Is this the proper way to do this or is there a better way? TIA for any suggestions. -- Colin Quote Link to comment https://forums.phpfreaks.com/topic/147022-solved-newbie-got-a-problem-with-ltrim/ Share on other sites More sharing options...
micah1701 Posted February 26, 2009 Share Posted February 26, 2009 i would use explode() instead and make it an array. <?php $string = "12345-ABCDEF"; $parts = explode("-",$string); echo $parts[0]; // 12345 echo $parts[1]; // ABCDEF ?> Quote Link to comment https://forums.phpfreaks.com/topic/147022-solved-newbie-got-a-problem-with-ltrim/#findComment-771845 Share on other sites More sharing options...
JonnoTheDev Posted February 26, 2009 Share Posted February 26, 2009 All ltrim does is remove whitespace from the left side of the string. What you need is to use substr() to get a substring or use preg_match() to extract the non numeric part or break up the string using the hyphen i.e. explode() Quote Link to comment https://forums.phpfreaks.com/topic/147022-solved-newbie-got-a-problem-with-ltrim/#findComment-771849 Share on other sites More sharing options...
colinsp Posted February 26, 2009 Author Share Posted February 26, 2009 Thanks very much to both of you for the information. I have used explode and it works fine. Nothing like using a hammer when you should have used a screwdriver. Quote Link to comment https://forums.phpfreaks.com/topic/147022-solved-newbie-got-a-problem-with-ltrim/#findComment-771862 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.