?
Solved

creating a function from a regex and returning two parts

Posted on 2013-01-17
10
Medium Priority
?
273 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
Technology Partners: 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
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 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…
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 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