Ruchika Ruchika - 2 years ago 88
PHP Question

Extract email address from string - php

I want to extract email address from a string, for example:

<?php // code
$string = 'Ruchika <>';

From the above string I only want to get email address

Kindly, recommend how to achieve this.

Answer Source

Try this

    $string = 'Ruchika < >';
    $pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';
    preg_match_all($pattern, $string, $matches);

see demo here

Second method

    $text = 'Ruchika < >';
    preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $text, $matches);

See demo here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download