EP Posted July 15, 2008 Share Posted July 15, 2008 Hey guys. I have a string. I want to find the last ( character in the string, strip out the character and anything after it. If there is a ) character before the last ( then that means there is another set of brackets which I want to leave alone. I just want to find the last bracket, delete it, and everything after it. I also need it to only search for the last ( because some times there is not a closing ) bracket... some of the strings from my source are cut off. How would I go about this? I'm new to PHP so I'm a little lost. Quote Link to comment https://forums.phpfreaks.com/topic/114934-solved-find-character-and-strip/ Share on other sites More sharing options...
MadTechie Posted July 15, 2008 Share Posted July 15, 2008 try this <?php $mystring = 'hello (world) how are (you today)'; $pos = strrpos($mystring, '(')-1; $mystring = substr($mystring, 0, $pos); echo $mystring; ?> check each function out on php.net to make sure you understand what they do.. (this code is untested) Quote Link to comment https://forums.phpfreaks.com/topic/114934-solved-find-character-and-strip/#findComment-591116 Share on other sites More sharing options...
EP Posted July 15, 2008 Author Share Posted July 15, 2008 It works but the strings that don't contain any ( characters are getting the last character of the string cut off. Example: test (123) --> test test (123) (456) --> test (123) test123 --> test12 Quote Link to comment https://forums.phpfreaks.com/topic/114934-solved-find-character-and-strip/#findComment-591127 Share on other sites More sharing options...
MadTechie Posted July 15, 2008 Share Posted July 15, 2008 try changing $pos = strrpos($mystring, '(')-1; to $pos = strrpos($mystring, '('); Quote Link to comment https://forums.phpfreaks.com/topic/114934-solved-find-character-and-strip/#findComment-591132 Share on other sites More sharing options...
EP Posted July 15, 2008 Author Share Posted July 15, 2008 Without the -1, the strings that don't contain a ( aren't even displayed. I have this running in a looping "while" statement. I'm trying to figure out how to put an if else statement around it.. If the string contains ( then it runs your code. Else, leave the string alone. Quote Link to comment https://forums.phpfreaks.com/topic/114934-solved-find-character-and-strip/#findComment-591134 Share on other sites More sharing options...
MadTechie Posted July 15, 2008 Share Posted July 15, 2008 <?php $mystring = 'hello (world) how are (you today)'; $pos = strrpos($mystring, '(')-1; if($pos > 0) { $mystring = substr($mystring, 0, $pos); } echo $mystring; ?> Quote Link to comment https://forums.phpfreaks.com/topic/114934-solved-find-character-and-strip/#findComment-591136 Share on other sites More sharing options...
EP Posted July 16, 2008 Author Share Posted July 16, 2008 It works. Thanks alot man. Quote Link to comment https://forums.phpfreaks.com/topic/114934-solved-find-character-and-strip/#findComment-591141 Share on other sites More sharing options...
MadTechie Posted July 16, 2008 Share Posted July 16, 2008 welcome, can you click topic solved (bottom left).. saves other reading the whole post hoping to help Quote Link to comment https://forums.phpfreaks.com/topic/114934-solved-find-character-and-strip/#findComment-591147 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.