Solved

creating a function from a regex and returning two parts

Posted on 2013-01-17
10
264 Views
Last Modified: 2013-01-18
this question is about creating a function from question
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27998518.html


$email = " Ray <ray.paseur@gmail.com>"

// A REGULAR EXPRESSION TO FIND THE FROM-EMAIL ADDRESS
$regex
= '#'         //REGEX DELIMITER
. '.*?'       // ANYTHING OR NOTHING
. '\<'        // ESCAPED WICKET
. '(.*?)'     // GROUP OF CHARACTERS WITH EMAIL ADDRESS
. '\>'        // ESCAPED WICKET
. '#'         // REGEX DELIMITER
;


// ISOLATE THE FROM EMAIL ADDRESS
preg_match($regex, $email, $matches);
$from = $matches[1];

Open in new window





function emailregex(){
// A REGULAR EXPRESSION TO FIND THE FROM-EMAIL ADDRESS
$regex
= '#'         //REGEX DELIMITER
. '.*?'       // ANYTHING OR NOTHING
. '\<'        // ESCAPED WICKET
. '(.*?)'     // GROUP OF CHARACTERS WITH EMAIL ADDRESS
. '\>'        // ESCAPED WICKET
. '#'         // REGEX DELIMITER
;


// ISOLATE THE FROM EMAIL ADDRESS
preg_match($regex, " Ray <ray.paseur@gmail.com>" , $matches);
return $matches;
}

Open in new window



i need to use $matches[0] and $matches[1]
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38788163
What do you mean by, "i need to use $matches[0] and $matches[1]?" $matches[0] should be the whole match, and $matches[1] should be just the first capture group (the stuff between the brackets in this case). Are you wanting to do something like:

return array($matches[0], $matches[1]);

Open in new window

0
 

Author Comment

by:rgb192
ID: 38788185
when I test
$matches[0] is the name 'ray'
and
$matches[1] is the email
0
 

Author Comment

by:rgb192
ID: 38788298
if I do

return array($matches[0], $matches[1]);

I dont know how to call and echo
array($matches[0], $matches[1])
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38788400
Are you referring to?

$result = emailregex();

echo $result[0];
echo $result[1];

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38788460
$result = emailregex();
echo $result[0];
echo $result[1];
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38790019
Try this, and follow the instructions in the output carefully.  The wickets<> are being treated as HTML.  View source is your friend.

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// FROM THE POST AT EE
function emailregex($str)
{
    // A REGULAR EXPRESSION TO FIND THE FROM-EMAIL ADDRESS
    $regex
    = '#'         // REGEX DELIMITER
    . '.*?'       // ANYTHING OR NOTHING
    . '\<'        // ESCAPED WICKET
    . '(.*?)'     // GROUP OF CHARACTERS WITH EMAIL ADDRESS
    . '\>'        // ESCAPED WICKET
    . '#'         // REGEX DELIMITER
    ;


    // ISOLATE THE FROM EMAIL ADDRESS
    preg_match($regex, $str, $matches);
    return $matches;
}

// TEST THE FUNCTION
$dat = " Ray <ray.paseur@gmail.com>";
$ret = EmailRegex($dat);

// SHOW THE OUTPUT
echo '<h1>USE THE BROWSER FEATURE "View Source" TO LOOK AT THIS</h1>' . PHP_EOL;
var_dump($ret);

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 38790211
Ray's code take the parameter. So it works for me.

I cant understand how to do an empty function() with this

thanks
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38790243
Not sure I understand empty function() either, but thanks for the points.  You might consider something like this...

function emailregex($str=FALSE)
{
    if (!$str) return array();
    /* etc */...

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38792497
Well, I'm certainly glad that question was clear  : \
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38792830
@kaufmed: Me too.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question