phpretard Posted March 25, 2009 Share Posted March 25, 2009 How can I eliminate all but the A from: $var = 'APR32509105205'; // What I have (predetermined) $var1=A //What I need Without string replace. Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/ Share on other sites More sharing options...
POG1 Posted March 25, 2009 Share Posted March 25, 2009 I'm not very good at regex but you could try something like.. preg_replace('[^a]','',$var); Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793689 Share on other sites More sharing options...
phpretard Posted March 25, 2009 Author Share Posted March 25, 2009 I should clarify...I just need the first charactor. $var could be anything Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793690 Share on other sites More sharing options...
lonewolf217 Posted March 25, 2009 Share Posted March 25, 2009 $var[0]; Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793695 Share on other sites More sharing options...
phpretard Posted March 25, 2009 Author Share Posted March 25, 2009 What do you do with $var[0] Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793701 Share on other sites More sharing options...
lonewolf217 Posted March 25, 2009 Share Posted March 25, 2009 $var[0] will contain the first character which is what you want, right? Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793703 Share on other sites More sharing options...
phpretard Posted March 25, 2009 Author Share Posted March 25, 2009 I just want the first charactor of any string. $var="ADFKLADHFH" // Strip all but A or $var="HKLJHKLDJKL"; // Strip all but H or $var="LSDFGKSJFH"; // Strip all but L AND SO ON Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793707 Share on other sites More sharing options...
lonewolf217 Posted March 25, 2009 Share Posted March 25, 2009 i know .. echo $var[0] and see what it contains Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793708 Share on other sites More sharing options...
FaT3oYCG Posted March 25, 2009 Share Posted March 25, 2009 you would be looking for [pre] string substr ( string $string , int $start [, int $length ] ) [/pre] Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793711 Share on other sites More sharing options...
phpretard Posted March 25, 2009 Author Share Posted March 25, 2009 I have no idea what to do with this ??? string substr ( string $string , int $start [, int $length ] ) I thought it was easy and I was just being ingnorant but could you explain that a little more? Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793724 Share on other sites More sharing options...
Maq Posted March 25, 2009 Share Posted March 25, 2009 echo substr($var, 0, 1); Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793731 Share on other sites More sharing options...
lonewolf217 Posted March 25, 2009 Share Posted March 25, 2009 you have two options here $var1 = $var[0]; //sets $var1 to be the first character of the string $var $var1 = substr($var,0,1); //same thing Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793734 Share on other sites More sharing options...
POG1 Posted March 25, 2009 Share Posted March 25, 2009 echo $var[0]; would be a faster alternative Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793735 Share on other sites More sharing options...
Yesideez Posted March 25, 2009 Share Posted March 25, 2009 Does anyone know if PHP uses null-terminated strings? I'm wondering if treating $var[0] as a string could pose future problems... Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793752 Share on other sites More sharing options...
MadTechie Posted March 25, 2009 Share Posted March 25, 2009 phpretard read the manual substr if it don't make sense keep reading.. if your still stuck (even after Example #1)! find some tutorials ie PHP 101: PHP For the Absolute Beginner if you don't want to spend the time then try the freelance section! No offence intended! EDIT: PHP doesn't use null-terminated strings, PHP stores the integer length of the string as well as the data itself, so it knows how long the string is, and doesn't get fooled by NULL bytes part-way along the string Quote Link to comment https://forums.phpfreaks.com/topic/151084-easy-easy/#findComment-793766 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.