• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 811
  • Last Modified:

How to I extract text between the "<>" tags?

Suppose I have this a variable with this value:

$eFrom = ""Joe Buck" <user@domain.com>";

What's the quickest way function that would allow me to extract the text between the <> tags?

In the above example, I'd like the end result to be:

$result = "user@domain.com"

I was thinking regular expressions, but struggle with writing them so I though someone here could assist.  Or maybe there is a better solution.
0
jrram
Asked:
jrram
  • 2
  • 2
1 Solution
 
b0lsc0ttIT ManagerCommented:
jrram,

You could get it with ...

if (preg_match('/<([^>]*)>/', $eFrom, $regs)) {
      $result = $regs[1];
} else {
      $result = "";
}

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
jrramAuthor Commented:
Thanks, I'll try it out.

This is a longer solution I just created:

function getIT($e)
{
      $cp_from = strpos($e, "<", 0) + 1;
      $cp_to = strpos($e, ">", $cp_from);

        $cp_len = $cp_to - $cp_from;

      $e = substr($e, $cp_from, $cp_len);

      return $e;
}
0
 
jrramAuthor Commented:
Thanks b0lsc0tt,

Any sites you recommend that gives a good explanation of regular expressions.  I've Googled for it haven't come up with anything that explains it well.

0
 
b0lsc0ttIT ManagerCommented:
Your welcome!  I'm glad I could help.

One of the best helps for learning or making expressions is RegexBuddy (http://www.regexbuddy.com/).  That site also has a tutorial or manual that can be a great tool for learning regular expressions.  It comes with the program but I believe is available separately.  If you will be making expressions or are interested in them then the program is worth every penny.

The best source for learning or instructions is a book called Master Regular Expression by Jeff Freidl (http://www.oreilly.com/catalog/regex3/).  It is commonly thought of as the "bible" of regular expressions.

Some of my other favorite regex sites are:

http://blog.stevenlevithan.com/ - Not just regex but some great blogs on them
http://regexpal.com/ - A super Javascript based regex tester
http://www.evolt.org/article/Regular_Expression_Basics/20/22700/index.html

Finally, the Regex zone on this site has some great expression experts and a lot of questions that can really help those that need it.

Thanks for the grade, the points and the fun question.  I hope this info helps too.

bol
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now