|
-
Feb 25th, 2010, 06:02 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Retrieving part of a string
Hi,
Basically I want to extract "foo" from the string "(foo)bar", where the only constants are the parentheses.
The method I'm using now:
PHP Code:
$tmp = explode('(', $input); $tmp2 = explode(')', $tmp[1]); $output = $tmp2[0];
In my opinion very inefficient, but I can't think of anything else at the moment.
Any hints?
Thanks.
Delete it. They just clutter threads anyway.
-
Feb 25th, 2010, 06:40 PM
#2
Re: Retrieving part of a string
Welcome to the wonderful world of regular expressions.
Code:
$input="(foo)bar";
preg_match("/[^(]*\(([^)]*)\).*/",$input,$matches);
print_r($matches); //"foo" is in $matches[1]
Regex is a big topic that I'd recommend Googling for an introduction and some examples to learn from. Its purpose is string processing based on flexible patterns.
-
Feb 25th, 2010, 06:55 PM
#3
Re: Retrieving part of a string
try:
PHP Code:
if (preg_match('/^\(([^\)])*\)/', $input, $matches)) $output = $matches[1];
(untested...)
Last edited by penagate; Feb 25th, 2010 at 07:23 PM.
-
Feb 25th, 2010, 06:55 PM
#4
Re: Retrieving part of a string
-
Feb 25th, 2010, 07:04 PM
#5
Thread Starter
Frenzied Member
Re: Retrieving part of a string
Yeah, I figured it would be regex 
But my knowledge of it is really fragmented, still need to take some time to study that.
If it isn't too much trouble, would you mind explaining the expression?
What I seem to understand so far is that with [^(]* it matches anything that isn't brackets, right?
Thanks anyway
Delete it. They just clutter threads anyway.
-
Feb 25th, 2010, 07:26 PM
#6
Re: Retrieving part of a string
Square brackets make up a character class:
[a] will match one instance of the character 'a'
[ab] will match either 'a' or 'b'
[a]* will match 'a' any number of times, including zero
[ab]* will match '', 'a', 'b', 'ab', 'ba', 'aba', and so on
The ^ symbol is used for negation:
[^a] will match any character except for 'a'
[^a]* will match any string which doesn't contain 'a'.
-
Feb 25th, 2010, 07:27 PM
#7
Re: Retrieving part of a string
yes. the expression ([^)])* basically says to match zero or more characters (*) that are not (^) a closing bracket (")"). the brackets surrounding this expression mean that you want to store whatever is matched there. the end expression, .* says to match any character zero or more times (which would match bar in your example), but does not store it.
oops! I guess penagate got here first ;)
-
Feb 25th, 2010, 07:43 PM
#8
Re: Retrieving part of a string
Ha!
-
Feb 25th, 2010, 11:32 PM
#9
Re: Retrieving part of a string
I could see you were both on the thread when I was going to reply... Figured I'd just sit back and watch. :]
-
Feb 26th, 2010, 06:35 AM
#10
Thread Starter
Frenzied Member
Re: Retrieving part of a string
I see. And what's the \( and \) for?
Thanks for all the replies
Delete it. They just clutter threads anyway.
-
Feb 26th, 2010, 09:09 AM
#11
Re: Retrieving part of a string
backslashes in PHP are pretty much always for escaping characters. parenthesis in regular expressions are special characters, just like square brackets, the asterisk, period, and addition signs are. these must be escaped if you want to match for them in a regular expression. you're escaping them in this case because you want to match a string enclosed within them, hence \(([^)])*\).
-
Feb 26th, 2010, 09:37 AM
#12
Thread Starter
Frenzied Member
Re: [RESOLVED] Retrieving part of a string
Thanks, that's clear now
Delete it. They just clutter threads anyway.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|