Jump to content

Recommended Posts

I have this string : Nick Leigh(1)

 

And I would like to get it so I have an array so that

 

Array (

[0] => Nick

[1] => Leigh

[2] => 1

)

 

I am very new to regular expressions but I have been looking at the preg_match function.  Is that the one I should be using here to strings that are of this format?

 

Thanks Nick

Link to comment
https://forums.phpfreaks.com/topic/140712-solved-breaking-a-string-down/
Share on other sites

The (1) is the tricky part. Not sure of regex to do this but you can use explode to split a string at the spaces. If there was a space before the ( it would work just fine.

 

Not sure if that is the answer you were looking for but it is sort of an answer.

preg_split splits a string by expression, discarding the parts that it matches. This expression has two delimiters (//), a character class ([]), a quantifier (+), a shorthand (\s), and two literals (()).

 

The delimiters contain the expression; the character class matches any one character inside, but the plus quantifier instructs it to match 1 or more; and what we're asking it to match is either "(", ")", or any kind of whitespace (\s).

Ah damn effigy, forgot about preg_split. Touché.

 

I'd go for effigy's, as it is more elegant.  In retrospect, there's a couple of things in my regex has a couple of unnecessary things in it.  First one technically works, but I would actually have changed it to:

preg_match("~(\w+)\s(\w+)\(([^\)]+)\)~",$string,$matches);

 

(\w+) matches the first word 'Nick'.  \w matches an alphanumeric character (lower and uppercase letters, numbers and underscores). + is a quantifier.  It tells it to match one or more of those characters. It will stop at the space because space is not an alphanumeric character.  The parenthesis means to capture the match and put it into $matches.

 

\s is shorthand for space character (and tabs and spans).  Used to look for a space between first name and last name.  We don't want to actually capture it; just match it, so the next capture knows where to start. Could have instead physically put a spacebar tap in there, but I think using \s is more visible.  Just remember that in general, \s matches not only space, but tabs and other 'whitespace' things. 

 

(\w+) matches the second word 'Leigh'. Same as the first one.  It starts after the \s and stops when it hits the ( because ( is not an alphanumeric character.

 

\(([^\)]+)\) The text to match is (1).  Since parenthesis are special characters for regex, we have to escape it in order for the engine to know to look for a parenthesis instead of treat it as the special character. so \( matches the first ( in (1).  The next ( is the opening capture parenthesis for what we want to capture inside the (..); the '1'.  [^\)] means to match anything that is not a closing parenthesis ')' (remember, have to escape it).  + matches one or more of that.  ) is the closing parenthesis for the capture, and \) is the closing parenthesis to literally match.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.