tags:

views:

251

answers:

3

Hi, I have a variable containing a string. The string can come in several ways:

// some examples of possible string
$var = 'Tom Greenleaf (as John Dunn Hill)'
// or
$var = '(screenplay) (as The Wibberleys) &'
// or
$var = '(novella "Four Past Midnight: Secret Window, Secret Garden")'

I need to clean up the string to get only the first element. like this:

$var = 'Tom Greenleaf'
$var = 'screenplay'
$var = 'novella'

I know this is a bit complicated to do but there is a pattern we can use.

Looking at the first variable, we can first check if the string contains the text ' (as'. If it does then we need to delete ' (as' and everything that comes after that.

In the second variable the rule we made in the first one also applies. We just need also to delete the parenthesis .

In the third variable if ' (as' was not found we need to get the first word only and then delete the parenthesis.

Well, thas all. I just need someone to help me do it because I'm new to PHP and I don't know regular expressions.... or another way to do it.

Thanks!!!!

+1  A: 

Try this regular expression:

(?:^[^(]+|(?<=^\()[^\s)]+)

This will get you either anything up to the first parenthesis or the first word inside the first parenthesis. Together with preg_match:

preg_match('/(?:^[^(]+|(?<=^\\()[^\\s)]+)/', $var, $match);
Gumbo
preg_match('/^(?:[^(]+|\\([^\\s)]+)/', $var, $match); fetches "Tom Greenleaf ", "(screenplay" and "(novella"
VolkerK
Yes, how can I delete the first opening parenthesis in case there is one???
Jonathan
This returns "Tom Greenleaf ", "(screenplay " and "(novella " but with a space in the end of the word. i need to clean this space. Also: How can I get the result in a variable and not in an array???
Jonathan
Great! Now its working perfectly!! Thanks!! Just one more thing, can you also delete everything after a colon (:) ??? In the same regex?? Thanks!
Jonathan
Just add the colon to the inverted character classes `[^…]` and it won’t get matched.
Gumbo
Last question!! I promise!!!! If the string begins with a space... how I delete this beginning space?? Thanks!!!!
Jonathan
Send it through `ltrim` first to remove all whitespace at the begin of the string.
Gumbo
+1  A: 

actually, there's no need for complex regex

$var = 'Tom Greenleaf (as John Dunn Hill)';
$var = '(novella "Four Past Midnight: Secret Window, Secret Garden")';
$var = '(screenplay) (as The Wibberleys) &';
if (  strpos($var,"(as") !== FALSE ){
    # get position of where (as is
    $ind =  strpos($var,"(as");    
    $result = substr($var,0,$ind);        
}else {
    # if no (as, split on spaces and get first element
    $s = preg_split("/\s+/",$var);    
    $result = $s[0];
}      
print  preg_replace( "/\(|\)/","",$result);
ghostdog74
A: 

try this one

^\((?<MATCH>[^ )]+)|^(?<MATCH>[^ ]+)

Erhan Baris