Jump to content

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/114934-solved-find-character-and-strip/
Share on other sites

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)

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.