Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting Results from RegEx Query

Status
Not open for further replies.

Geronantimo

Technical User
Apr 3, 2006
79
SE
First, I hope this is the appropriate board for a RegEx query...

I have a small script that takes the input from a form textarea and tries to extract all of the e-mail addresses. It works very well and the output shows each e-mail address separated with a space.

Can anybody tell me how I can get the output to be displayed with each e-mail address on a new line?

The php looks like this:
Code:
$forminput .= "$_POST[emailtext]";

function extract_emails_from($string){
  preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
  return $matches[0];
}

$text = $forminput;

$emails = extract_emails_from($text);

print(implode("\n", $emails));
 
assuming your regex is correct, this code should work for you

Code:
function extract_emails_from($string){
  preg_match_all("/[red]([/red][\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+[red])[/red]/i", $string, $matches);
  return $matches[red][s][0][/s][/red];
}

if (!empty($_POST['emailtext']){
  $matches = extract_emails_from($_POST['emailtext']);
  foreach ($matches as $match){
    echo $match[1] . "<br/>\r\n";
  }
}
 
Hi jpadie,

Thanks for your reply.

In your post above, you have a red open and close bracket either end of the regex - are these to be removed?

And, the [0] - Is this also to be removed?

I replaced my function with the function you supplied and received an error:
"Parse error: syntax error, unexpected '{' in /extractor.php on line 20"

Line 20 is the line:
Code:
if (!empty($_POST['emailtext']){

If I add ")" before the curly bracket on this line, I don't get an error but the second character of the e-mail addresses are listed on separate lines and then all of the e-mail addresses are displayed in a single paragraph beneath. Obviously, I haven't quite got it right yet.
 
Code:
function extract_emails_from($string){
  preg_match_all("/([\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+)/i", $string, $matches);
  return $matches;
}

if (!empty($_POST['emailtext'])){
  $matches = extract_emails_from($_POST['emailtext']);
  foreach ($matches as $match){
    echo $match[1] . "<br/>\r\n";
  }
}
 
Thanks again jpadie,

I have found the solution using a slightly different technique:
Code:
$forminput .= "$_POST[emailtext]";

function extract_emails_from($string){
  preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
  return $matches[0];
}

$text = $forminput;

$emails = extract_emails_from($text);

print [COLOR=red]"<pre>" .[/color] (implode("\n", $emails)) [COLOR=red]. "</pre>"[/color];

This displays each e-mail address on a separate line.
 
i would be very surprised if that provides you the answer if you have multiple emails in the search string.

but there we go, glad you sorted out what you were aiming for.

alternatives to your solution (in terms of formatting) would have been

Code:
echo nl2br(implode("\n",$emails));
//echo implode ("<br/>", $emails);
//echo "<pre>".print_r($emails, true). "</pre>";
//and plenty more variations on a theme.
 
Thanks for your help jpadie,

would you like to see a demonstration?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top