4images Forum & Community
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 15, 2018, 11:17:54 PM

Login with username, password and session length
Search:     Advanced search
Check the new Tutorial subforum with helpfull guides and tutorials for modifications and tweaks.
Togle to toolbar
Translate this page with =>
Translate this page >
* Home Help Search Login Register
 
+  4images Forum & Community
|-+  General / Allgemeines
| |-+  Programming
| | |-+  working with string patterns...
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Print
This topic has not yet been rated!
Author Topic: working with string patterns...  (Read 13190 times)
budduke
Hero Member
*****
Offline Offline

Posts: 506

Thank You
-Given: 10
-Receive: 124


View Profile WWW
« 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!
Logged

Buddy Duke
www.budduke.com
Rembrandt
4images Moderator
4images Guru
*****
Offline Offline

Posts: 4187

Thank You
-Given: 116
-Receive: 900

Vienna


View Profile WWW
« Reply #1 on: August 19, 2012, 08:29:59 AM »

Hi!

Do they mean this?
1
2
3
4
5
6
7
8
          $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:
1
2
3
4
5
6
          $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 » Logged

Meine 4Images Modifikationen  Now over 100 Modification Online!    Meine 4images Demo Seite


Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
budduke
Hero Member
*****
Offline Offline

Posts: 506

Thank You
-Given: 10
-Receive: 124


View Profile WWW
« 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?
Logged

Buddy Duke
www.budduke.com
Rembrandt
4images Moderator
4images Guru
*****
Offline Offline

Posts: 4187

Thank You
-Given: 116
-Receive: 900

Vienna


View Profile WWW
« 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"
Logged

Meine 4Images Modifikationen  Now over 100 Modification Online!    Meine 4images Demo Seite


Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
budduke
Hero Member
*****
Offline Offline

Posts: 506

Thank You
-Given: 10
-Receive: 124


View Profile WWW
« 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.
Logged

Buddy Duke
www.budduke.com
Rembrandt
4images Moderator
4images Guru
*****
Offline Offline

Posts: 4187

Thank You
-Given: 116
-Receive: 900

Vienna


View Profile WWW
« Reply #5 on: August 19, 2012, 09:10:19 PM »

my second example in my first post works now, i forgot:

so here is version as "function":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
Logged

Meine 4Images Modifikationen  Now over 100 Modification Online!    Meine 4images Demo Seite


Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
budduke
Hero Member
*****
Offline Offline

Posts: 506

Thank You
-Given: 10
-Receive: 124


View Profile WWW
« Reply #6 on: August 20, 2012, 12:41:27 AM »

That did it!  Very Happy

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!
Logged

Buddy Duke
www.budduke.com
schmekl
Pre-Newbie

Offline Offline

Posts: 1

Thank You
-Given: 0
-Receive: 0


View Profile WWW
« Reply #7 on: November 03, 2012, 01:01:16 PM »

Thank you andy.

________________________
Software University
▄bersetzung Englisch Deutsch
Logged

Hi ich bin ▄bersetzer fŘr Italienisch und Deutsch. Gern berate ich Sie kostenlos
mizeweb
Pre-Newbie

Offline Offline

Posts: 1

Thank You
-Given: 3
-Receive: 0


View Profile WWW
« Reply #8 on: September 13, 2016, 06:28:21 AM »

my second example in my first post works now, i forgot:

so here is version as "function":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
Logged

سایت ساز در شیرازxxx: :mizeweb. ir,طراحی سایت در شیراز
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.091 seconds with 19 queries.