hirephpexpert Posted October 10, 2011 Share Posted October 10, 2011 Hi, I need some help in selecting only a part of a string. Below is the string: $str = "state_West_Bracknell_4057"; I would like to meet only West Bracknell from the above. $state1 = explode("_", $str); echo $state1[1]; But this will only return West. I know I can concat and get both BUT sometimes the state's name is only: $str = "state_Bracknell_4057"; So if I concat it will give Bracknell_4057 So, is there a way to remove everything before the first "_" and the last "_"? Thanks 0 Quote Link to comment Share on other sites More sharing options...
Gitex Posted October 10, 2011 Share Posted October 10, 2011 use substr() in php 0 Quote Link to comment Share on other sites More sharing options...
hirephpexpert Posted October 11, 2011 Author Share Posted October 11, 2011 Thanks Gitex for the advise but its tried it but couldn't get proper result any other suggestion plz... 0 Quote Link to comment Share on other sites More sharing options...
grtoftis Posted October 24, 2011 Share Posted October 24, 2011 use substr() in php thanks for your advice.cool 0 Quote Link to comment Share on other sites More sharing options...
EhsanCh Posted November 12, 2011 Share Posted November 12, 2011 $str = substr($str,strpos($str,"_")+1,strrpos($str,"_")-strpos($str,"_")-1 ); 0 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.