jeff5656 Posted August 1, 2011 Share Posted August 1, 2011 I have a variable $x that is "P23456" How do I strip out the P and make it "23456"? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/243515-strip-first-letter/ Share on other sites More sharing options...
Maq Posted August 1, 2011 Share Posted August 1, 2011 Is the data consistently in that format? Quote Link to comment https://forums.phpfreaks.com/topic/243515-strip-first-letter/#findComment-1250403 Share on other sites More sharing options...
manix Posted August 1, 2011 Share Posted August 1, 2011 substr() would do just fine? Quote Link to comment https://forums.phpfreaks.com/topic/243515-strip-first-letter/#findComment-1250410 Share on other sites More sharing options...
jeff5656 Posted August 1, 2011 Author Share Posted August 1, 2011 Well it always starts with either a number or the letter P. If it's a P I want to strip it. Sometimes it does not have a P, so if I use substr(), it would remove the first number which is what I dont want. Quote Link to comment https://forums.phpfreaks.com/topic/243515-strip-first-letter/#findComment-1250411 Share on other sites More sharing options...
manix Posted August 1, 2011 Share Posted August 1, 2011 well check it, it's simple Quote Link to comment https://forums.phpfreaks.com/topic/243515-strip-first-letter/#findComment-1250412 Share on other sites More sharing options...
jeff5656 Posted August 1, 2011 Author Share Posted August 1, 2011 "well check it, it's simple" Maybe for YOU it is, but not for me (or I wouldn't be asking in this forum right??). Of course it is simple to the person who knows the answer. This "truism" does not help ME out very much though does it? Quote Link to comment https://forums.phpfreaks.com/topic/243515-strip-first-letter/#findComment-1250421 Share on other sites More sharing options...
manix Posted August 1, 2011 Share Posted August 1, 2011 I'm sorry, if(substr($string, 0, 1)=="P"){$string = substring($string, 1, strlen($string));} This should do it Quote Link to comment https://forums.phpfreaks.com/topic/243515-strip-first-letter/#findComment-1250422 Share on other sites More sharing options...
jeff5656 Posted August 1, 2011 Author Share Posted August 1, 2011 Ok I figured it out. But instead of being cryptic about it, I will post the solution here so that others can see it in the future: while ($row=mysql_fetch_assoc($result)){ $string = $row['cpt']; if ($string[0] == 'P' ){ $new_cpt = substr($row['cpt'], 1); } else {$new_cpt =$row['cpt'];} echo $row['cpt']." and new is ".$new_cpt."<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/243515-strip-first-letter/#findComment-1250423 Share on other sites More sharing options...
jeff5656 Posted August 1, 2011 Author Share Posted August 1, 2011 Ok your solution is a bit more elegant I'll admit... Quote Link to comment https://forums.phpfreaks.com/topic/243515-strip-first-letter/#findComment-1250425 Share on other sites More sharing options...
manix Posted August 1, 2011 Share Posted August 1, 2011 not really, doesn't matter tho, I hope I helped. Quote Link to comment https://forums.phpfreaks.com/topic/243515-strip-first-letter/#findComment-1250426 Share on other sites More sharing options...
Alex Posted August 1, 2011 Share Posted August 1, 2011 I'm sorry, if(substr($string, 0, 1)=="P"){$string = substring($string, 1, strlen($string));} This should do it Just a small comment: In the way you're using substr() there (you also put substring() instead of substr()), the third parameter is superfluous. In fact, you'd be trying to take one extract character that doesn't exist from the string. $string = substr($string, 1); Would suffice. Quote Link to comment https://forums.phpfreaks.com/topic/243515-strip-first-letter/#findComment-1250432 Share on other sites More sharing options...
manix Posted August 1, 2011 Share Posted August 1, 2011 I made a typo, apologies <?php if(substr($string, 0, 1)=="P"){$string = substr($string, 1, strlen($string));} ?> Quote Link to comment https://forums.phpfreaks.com/topic/243515-strip-first-letter/#findComment-1250454 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.