Solved

creating a function from a regex and returning two parts

Posted on 2013-01-17
10
255 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
when I test
$matches[0] is the name 'ray'
and
$matches[1] is the email
0
 

Author Comment

by:rgb192
Comment Utility
if I do

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

I dont know how to call and echo
array($matches[0], $matches[1])
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Are you referring to?

$result = emailregex();

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

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
$result = emailregex();
echo $result[0];
echo $result[1];
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Well, I'm certainly glad that question was clear  : \
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
@kaufmed: Me too.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

743 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

14 Experts available now in Live!

Get 1:1 Help Now