Jump to content

[SOLVED] Find character and strip


EP

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.