1

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

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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);

6 Comments

preg_match('/^(?:[^(]+|\([^\\s)]+)/', $var, $match); fetches "Tom Greenleaf ", "(screenplay" and "(novella"
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???
Great! Now its working perfectly!! Thanks!! Just one more thing, can you also delete everything after a colon (:) ??? In the same regex?? Thanks!
Just add the colon to the inverted character classes [^…] and it won’t get matched.
Last question!! I promise!!!! If the string begins with a space... how I delete this beginning space?? Thanks!!!!
|
0

try this one

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.