Re: Extracting Strings



kenoli wrote:
This is probably much simpler than I'm making it, and seems like
something that is done all the time, but I can't find a combination of
regular expression and php function that does it.

I want to extract the data in the following string:

"Person Name" <email@xxxxxxx>

that is between the "" and the <> characters.

In this case I want to extract:

Person Name and email@xxxxxxx

so I can assign the results to a variable or array. I want to extract
each string separately.

If someone can help me construct the script/regular expression to do
this, I would appreciate it.

Thanks,

--Kenoli

Hi Kenoli,
I'm no expert at regular expressions, but I found the following workable solution.

1 <?php
2 $string = '"Name" <email@xxxxxxxxxx>';
3
4 preg_match("/<([a-z0-9.@]+)>/i", $string, $matches);

5
6 var_dump($matches);
7 ?>

String refers to the string you would like to extract.

preg_match is the function you will use, to do the regex.

/<([a-z0-9.@]+)>/i is the regelur expression to match against
The characters in the square brackets are the characters to match against, you can add more to include other valid email characters ascii 7 bit chars iirc.

$matches is the matches, in this case you want match number 1 if it exists because it is the match inside the parenthesis.

Anyone please feel free to correct me if required.

Hope this helps, Cheers
Leigh Finch
www.phpmaniac.net
.



Relevant Pages

  • Re: Extracting Strings
    ... regular expression and php function that does it. ... I want to extract the data in the following string: ... The characters in the square brackets are the characters to match ...
    (alt.php)
  • Re: Extracting Strings
    ... regular expression and php function that does it. ... I want to extract the data in the following string: ... The characters in the square brackets are the characters to match ...
    (alt.php)
  • Re: Extracting a numbers from a text string
    ... thorough understanding of the application's potential. ... The Regular Expression is the section within the double quote marks. ... Look for a space (but don't extract it) ... a string of digits ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Extracting Strings
    ... regular expression and php function that does it. ... String refers to the string you would like to extract. ... The characters in the square brackets are the characters to match ...
    (alt.php)
  • Re: Regex help
    ... I'm trying to write a regular expression that extracts the host name ... the string I extract must have: ... the fullstops most be in the middle of it. ...
    (microsoft.public.dotnet.languages.csharp)