Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

creating a function from a regex and returning two parts

Posted on 2013-01-17
10
Medium Priority
?
271 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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 111

Expert Comment

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

610 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