Solved

creating a function from a regex and returning two parts

Posted on 2013-01-17
10
256 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

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 108

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 108

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now