Question

I have an string and I need an email ID parsed from that string. I have used PHP regular expression for retrieving and worked fine. But my problem is if the email prefix contains the regex.

<?php
$convert = "mailto:xxxin@yahoo.inATTENDEE";

preg_match_all('/mailto:(.*?)(.com|.org|.net|.in)/', $convert, $emails);

echo "<pre>";
print_r($emails);
echo "</pre>";
?>

Output:

Array (

[0] => Array
    (
        [0] => mailto:xxxin
    )

[1] => Array
    (
        [0] => xx
    )

[2] => Array
    (
        [0] => xin
    )

)

But I'm expecting [0] => mailto.xxxin@yahoo.in. Please help me to achieve this.

No correct solution

OTHER TIPS

$convert = "mailto:xxxin@yahoo.inATTENDEE";

preg_match_all('/(mailto.*(?:.com|.org|.net|.in){1}?)/', $convert, $emails);

$newArray = array();
foreach($emails as $em){
$newArray = array_merge($newArray, $em);
break;
}

echo "<pre>";
print_r($newArray);
echo "</pre>";

result

Array
(
    [0] => mailto:xxxin@yahoo.in
)

Just use str_replace() AND explode() as:

$convert    = "mailto:xxx@yahoo.com, mailto:xxxin@yahoo.in";
$finalstr   = str_replace(array("mailto:", " "),"",$convert);
$emailids   = explode(",", $finalstr);
var_dump($emailids);

The below should do it for you:

<?php
//$convert = "mailto:xxxin@yahoo.inATTENDEE";
    $convert = 'mailto:xxx@yahoo.com, mailto:xxxin@yahoo.in';

preg_match_all('/mailto:.*?(?:\.com|\.org|\.net|\.in){1}/', $convert, $emails);

echo "<pre>";
print_r($emails);
echo "</pre>";
?>

Updated with that pattern, working, and removed extraneous brackets, working: http://phpfiddle.org/main/code/3if-8qy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top