preg_replace - url and line break

Asked

Viewed 395 times

-1

I have this function below:

function filterEmail($text)
{
    return preg_replace('/(.*)[a-z0-9\.\_\-]+@[a-z0-9\.\_\-]+\.[a-z]+(.*)/i','', $text);
}

It removes lines who have email.

I have a textarea, and sometimes users post the email, so whenever it puts the email, this function removes.

Ex: Hello guys, all right?

I really liked the site.

Send me an e-mail [email protected]

With this function, the content is like this:

Ex: Hello guys, all right?
I really liked the site.


So far so good. What I need is the same function, but also remove URL. If possible using the preg_replace.

And finally, sometimes users put three line breaks (\n\n) - I need that whenever you put three line breaks (or more), it leaves only two. But only when there are three line breaks or more..

Ex:

Hello guys, all right?


I really liked the site.


Should stay:

Hello guys, all right?

I really liked the site.

  • Someone?????????

  • A hint, instead of using the deletion of the entire line with preg_replace, could delete only the email, as the user can type other things in the line along with the email I use : preg_replace('/([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/','', $text);

1 answer

1

Try the following:

<?php
function filterEmail($texto) {

$texto = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $texto);
$texto = preg_replace('/(.*)[a-z0-9\.\_\-]+@[a-z0-9\.\_\-]+\.[a-z]+(.*)/i','', $texto);
$texto = preg_replace('/[\n\r]{3,}/',"\n\n",$texto);
return $texto;

}

$string = "Olá pessoal, tudo bem?

Gostei muito do site.

Me envie um e-mail [email protected]
http://www.codigosnaweb.com";
echo filterEmail($string);
?>

Browser other questions tagged

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