Author Topic: working with string patterns...  (Read 19528 times)

0 Members and 1 Guest are viewing this topic.

Offline budduke

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • http://www.budduke.com
working with string patterns...
« on: August 19, 2012, 01:20:35 AM »
I am terrible when it comes to string patterns and kind not find the info I need anywhere.
I want to perform a rtrim ($string,"search pattern")  on a string like this one..
$string="This is my data (want to keep this data) (want to delete this data and brackets)"
Anybody have any ideas what my "search pattern" should be to get rid of the second set of () and the data inside it?

Thanks!
Buddy Duke
www.budduke.com

Rembrandt

  • Guest
Re: working with string patterns...
« Reply #1 on: August 19, 2012, 08:29:59 AM »
Hi!

Do they mean this?

          $string
="This is my data (want to keep this data) (want to delete this data and brackets)";

          
preg_match_all('#\([^\)]*\)#'$string$matches);
          
print_R($matches[0][0]."<br>"); //(want to keep this data)
          
print_R($matches[0][1]."<br>"); //(want to delete this data and brackets)
          
          
$string preg_replace('#\('.$matches[0][1].'\)#' ,"",$string);
          
print_R($string); //This is my data (want to keep this data) 


or always the last matches:

          $string
="This is my data (want to keep this data) (want to delete this data and brackets)";

          
preg_match_all('#\([^\)]*\)#'$string$matches);
          
$last_match array_pop($matches[0]);
          
$string preg_replace('#\('.$last_match.'\)#' ,"",$string);
          
print_R($string);//This is my data (want to keep this data) 


mfg Andi
« Last Edit: August 19, 2012, 08:56:47 PM by Rembrandt »

Offline budduke

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • http://www.budduke.com
Re: working with string patterns...
« Reply #2 on: August 19, 2012, 07:14:29 PM »
Thanks for the help Rembrandt but I guess I should have been more clear...
The $string variable sometimes will have 2 () in it and sometimes there is only 1().
Mainly, I am wanting to remove the far right set of () and the data inside of it no matter have many () are in the string. On some of them there are 3 sets.
Note, the string will always have a set of () at the end of the string if that helps.

I am glad someone knows more about the patterns because it just confuses me the more I look at it.

When I ran your code on my data, it actually erased the whole string when it only contained one set of ()....

any ideas?
Buddy Duke
www.budduke.com

Rembrandt

  • Guest
Re: working with string patterns...
« Reply #3 on: August 19, 2012, 07:27:52 PM »
ok, give me some "string" examples, and what you want to remove from their "strings"

Offline budduke

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • http://www.budduke.com
Re: working with string patterns...
« Reply #4 on: August 19, 2012, 08:08:38 PM »
Lets see...
"this is test#1 (2012) (delete this data)" needs to become "this is test#1 (2012)"
"this is some data test#2 (data to delete)" needs to become "this is some data test#2"
"this is another test #3 (2012) (september) (get rid of me data)" needs to become "this is another test #3 (2012) (september)"

Hope that makes it a little clearer. The data inside the bracket changes everytime but the brackets are always in the string at the end.
Looking at the rtrim() function looks to me as my best way to go but I can not configure a pattern that would match start bracket, anything in between, end bracket and the end of the string. The rtrim looks like it starts at the end of the string and looks backward when trying to find a match.
Buddy Duke
www.budduke.com

Rembrandt

  • Guest
Re: working with string patterns...
« Reply #5 on: August 19, 2012, 09:10:19 PM »
my second example in my first post works now, i forgot:
[0]

so here is version as "function":

function special_rtrim($string){
  
preg_match_all('#\([^\)]*\)#'$string$matches);
  
$last_match array_pop($matches[0]);
  
$string preg_replace('#\('.$last_match.'\)#' ,"",$string);
return 
$string;
}

//$string="This is my data (want to keep this data) (want to delete this data and brackets)";
$string="this is another test #3 (2012) (september) (get rid of me data)";  //needs to become "this is another test #3 (2012) (september)"
//$string="this is test#1 (2012) (delete this data)"; //needs to become "this is test#1 (2012)"
//$string="this is some data test#2 (data to delete)"; //needs to become "this is some data test#2" 

$string special_rtrim($string);
 
echo 
$string;


mfg Andi

Offline budduke

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • http://www.budduke.com
Re: working with string patterns...
« Reply #6 on: August 20, 2012, 12:41:27 AM »
That did it!  :D

Someday I will have to sit down and understand those patterns better. I think that is holding me back in allot of ways...

Thank you Rembrandt for your help!
Buddy Duke
www.budduke.com

Offline schmekl

  • Pre-Newbie
  • Posts: 1
    • View Profile
    • Übersetzung Italienisch Deutsch
Re: working with string patterns...
« Reply #7 on: November 03, 2012, 01:01:16 PM »
Thank you andy.

________________________
Software University
Übersetzung Englisch Deutsch
Hi ich bin Übersetzer für Italienisch und Deutsch. Gern berate ich Sie kostenlos

Offline mizeweb

  • Pre-Newbie
  • Posts: 1
    • View Profile
    • سایت ساز حرفه ای در شیراز
Re: working with string patterns...
« Reply #8 on: September 13, 2016, 06:28:21 AM »
my second example in my first post works now, i forgot:
[0]

so here is version as "function":

function special_rtrim($string){
  
preg_match_all('#\([^\)]*\)#'$string$matches);
  
$last_match array_pop($matches[0]);
  
$string preg_replace('#\('.$last_match.'\)#' ,"",$string);
return 
$string;
}

//$string="This is my data (want to keep this data) (want to delete this data and brackets)";
$string="this is another test #3 (2012) (september) (get rid of me data)";  //needs to become "this is another test #3 (2012) (september)"
//$string="this is test#1 (2012) (delete this data)"; //needs to become "this is test#1 (2012)"
//$string="this is some data test#2 (data to delete)"; //needs to become "this is some data test#2" 

$string special_rtrim($string);
 
echo 
$string;


mfg Andi

thats so good thanks
سایت ساز در شیرازxxx: :mizeweb. ir,طراحی سایت در شیراز