Re: Extracting Strings



kenoli wrote:
On May 31, 5:30 pm, Leigh Finch <feiyan...@xxxxxxxxx> wrote:
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" <em...@xxxxxxx>
that is between the "" and the <> characters.
In this case I want to extract:
Person Name and em...@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" <em...@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 Finchwww.phpmaniac.net

Leigh, thanks. That works great for the email address. Do you have
any idea how I get the text between the " "? I tried replacing the
carats with the quote marks, even preceding them with escape
characters and can't get it to work.

Thanks,

--Kenoli

Hi Kenoli,
The following should give you both variables you are after in the same bit of code.

Array element 1 is the name
Array Element 2 is the email address

1 <?php
2 $string = '"Test Name" <email@xxxxxxxxxx>';


3
4 preg_match("/^\"([a-z0-9.@\ -.]+)\" ?<([a-z0-9.@-_]+)>/i", $string, $matches);
5
6 var_dump($matches);
7 ?>

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



Relevant Pages

  • Re: isolate a number in cell with text and numbers
    ... That formula doesn't specifically extract numbers per se, ... have a string of numbers as *the last word*. ... The result of the inner TRIM function is then passed to the SUBSTITUTE ... string with other characters. ...
    (microsoft.public.excel.misc)
  • Re: RegExp irregularity in JScript
    ... we believe the VBScript Regular Expression class (version 1.0 through ... It does not however, limit the string minimum 4, maximum 8 characters. ... Obviously the first test should test the length of the string, minimum 4, ...
    (microsoft.public.scripting.jscript)
  • Re: Usename regex
    ... Think of a string, ... Regular expression benchmark ... MS MAX AVG MIN DEV INPUT ... If the textbox in question is limited to say 16 characters you'd ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: removing value within a string
    ... parenthesis "(" in the string and extract everything to the right of that. ... This formula is simply replacing the 3 characters from the right with the ...
    (microsoft.public.excel.worksheet.functions)
  • Re: removing value within a string
    ... parenthesis "(" in the string and extract everything to the right of that. ... This formula is simply replacing the 3 characters from the right with the ...
    (microsoft.public.excel.worksheet.functions)