php_novice2007 Posted June 15, 2007 Share Posted June 15, 2007 Hi, i've got a string which begins with a ".", how can I get rid of it? e.g. $str = ".hello", I want $str = "hello" thanks! Quote Link to comment https://forums.phpfreaks.com/topic/55689-quick-question/ Share on other sites More sharing options...
christofurr Posted June 15, 2007 Share Posted June 15, 2007 <?php $str = ".hello"; $clean_str = ereg_replace(".", "", $str); echo $clean_str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/55689-quick-question/#findComment-275164 Share on other sites More sharing options...
php_novice2007 Posted June 15, 2007 Author Share Posted June 15, 2007 hmm I tried your code but nothing got printed :S I only want to remove the first ".", if there are subsequent "." I want to keep them.. Quote Link to comment https://forums.phpfreaks.com/topic/55689-quick-question/#findComment-275165 Share on other sites More sharing options...
PC Nerd Posted June 15, 2007 Share Posted June 15, 2007 ok: i think this might work......I dont know about php, but it would in python. I dont know about the string indexing etc in php or even if it supports it, however : $char = string[0]; if ($char == ".") { $new_string = string[1:-1]; } print $new_string ive put python code in blue.... hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/55689-quick-question/#findComment-275169 Share on other sites More sharing options...
php_novice2007 Posted June 15, 2007 Author Share Posted June 15, 2007 I know how to do the string[0] bit in PHP, its just $str[0]. $str[1:-1] unfortunately doesn't work.. Quote Link to comment https://forums.phpfreaks.com/topic/55689-quick-question/#findComment-275173 Share on other sites More sharing options...
christofurr Posted June 15, 2007 Share Posted June 15, 2007 I can't tell you how, but it can probably be done with preg_replace(). Quote Link to comment https://forums.phpfreaks.com/topic/55689-quick-question/#findComment-275174 Share on other sites More sharing options...
jitesh Posted June 15, 2007 Share Posted June 15, 2007 $str= str_replace(".", "", ".hello"); Quote Link to comment https://forums.phpfreaks.com/topic/55689-quick-question/#findComment-275181 Share on other sites More sharing options...
atitthaker Posted June 15, 2007 Share Posted June 15, 2007 If the "." in the beginning is going to be there every time, you can simply take substring of the given string. <? $str="Test"; if($str[0] == ".") { $str = substr($str,1); } ?> I think this shall solve the problem. Quote Link to comment https://forums.phpfreaks.com/topic/55689-quick-question/#findComment-275186 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.