How do i use this str_replace with this




and so on

so i have setup

$email = str_replace(" ", ", ", $mail_list);

but it will only talk details if i do this

It does not seam to find and replace return

Dani AI

Generated

Good point from : the characters between addresses are line breaks, not spaces. Different sources may use LF ("\n"), CRLF ("\r\n"), CR ("\r") or even HTML "<br>" tags, so a replacement should handle all of those at once.

A simple normalization (replace all newline types and HTML breaks, collapse repeated commas/spaces, trim ends) works well:

$clean = str_replace(array("\r\n", "\r", "\n", "<br>", "<br />"), ', ', $rawHtmlOrText);
$clean = preg_replace('/\s*,\s*/', ', ', $clean);   // normalize spacing around commas
$clean = trim($clean, " ,");

When the input contains mailto anchor tags it’s safer to extract addresses from the hrefs (or from the text) rather than relying on tag-stripping. Example using regex to pull mailto: values or fallback to a standard email regex:

$emails = array();
if (preg_match_all('/mailto:([^"\'>]+)/i', $html, $m)) {
    $emails = $m[1];
} else {
    preg_match_all('/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/', strip_tags($html), $m2);
    $emails = $m2[0];
}
$emails = array_map('trim', array_unique($emails));
$result = implode(', ', $emails);

If the list comes from a plain text file, using file() with FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES is simpler and less error-prone than manual fgets loops (as suggested by ). After loading lines, trim, validate with filter_var(..., FILTER_VALIDATE_EMAIL), remove duplicates, then join with commas.

Troubleshooting notes: watch for invisible characters (tabs, zero-width spaces), remove HTML before sending to mail headers, avoid trailing commas, and validate addresses to prevent malformed recipients.

Recommended Answers

All 2 Replies

How do i use this str_replace with this



.com
and so on

so i have setup

$email = str_replace(" ", ", ", $mail_list);

but it will only talk details if i do this

It does not seam to find and replace return

The 'return' character is not a space, it is '\n'.

-Fredric

Not sure how the email list are stored... If they are stored in txt file, then you can use fread to read each line of email address into a string:

$mail_list = '';
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
   while (!feof($handle)) {
       $buffer = fgets($handle, 1024);
       $mail_list .= $buffer.',';  // email addresses
   }
   fclose($handle);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.