Break line after semicolon ;

Asked

Viewed 2,256 times

2

I have a text file (word) composed of four pages with several email addresses on each side separated by comma and dot.

Is that way:

[email protected]; [email protected]; [email protected];
[email protected]; [email protected]; [email protected];

I’d like it to stay that way:

for [email protected];
[email protected];
for [email protected];
for [email protected];
[email protected];
[email protected];

I believe that can be done using php where, will be made a "read file" that for each ; (point and comma) found will have a line break...

I would like help to achieve this.

My code for now reads the file and prints the result on the screen....

<?php
// Abre o Arquvio no Modo r (para leitura)
$arquivo = fopen ('emails.txt', 'r');

// Lê o conteúdo do arquivo 
while(!feof($arquivo))
{
//Mostra uma linha do arquivo
$linha = fgets($arquivo, 1024);
echo $linha.'<br />';
}

// Fecha arquivo aberto
fclose($arquivo);
?>
  • 1

    What you’ve already done?

  • For now nothing yet, I’m trying to read the file and print the result...I’m trying to assemble the code

  • 1

    Assemble something, post it there and tell us what your specific question is so we can help you. It’s not nice to ask to do everything for you.

  • @bigown, follow my code ... need now to do the line break

  • It already worked with the str_replace...was easier than I imagined. Thank you

2 answers

7


It would be something like that?

str_replace(";",";<br>",texto);

Will replace all the ; for ;<br> of texto.

  • It worked out, buddy, thank you very much.

  • 1

    When printing the line: echo str_replace(";",";<br>",$linha). You have an example here: http://www.w3schools.com/php/func_string_str_replace.asp

1

I have this complete method that does all the work to separate the email data:


 function setEmail($stringMail)
    {
        $nstringMail = preg_replace('/\;/',',', $stringMail);
        $nstringMail = preg_replace('/(.*)\<|\>(.*)/','', $stringMail);
        $nstringMail = preg_replace('/\v+/',',', $nstringMail);
        $nstringMail = preg_replace('/\t+/',',',  $nstringMail);
        $nstringMail = preg_replace('/\n+/',',', $nstringMail);
        $nstringMail = preg_replace('/\,\,/',',', $nstringMail);
        $nstringMail = preg_replace('/\s+/','', $nstringMail);

            $dataEmail = explode(',', $nstringMail);
           if (!empty($dataEmail)) {
               foreach ($dataEmail as $stringValue) {
                  if (filter_var($stringValue, FILTER_VALIDATE_EMAIL)) {
                         $emails[] = $stringValue;
                  }
               }
           }
        array_unique($emails);
        return $emails;
    }

$saida = setEmail('[email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected];');

echo "<pre>";
print_r($saida);

You can check the code in action here: http://ideone.com/7AzObO

Browser other questions tagged

You are not signed in. Login or sign up in order to post.