How to Remove Email Addresses and Links from a String in PHP

How to remove email addresses and links from a string in PHP?

You can use preg_replace to do it.

for emails:

$pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $string);

for urls:

$pattern = "/[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+/i";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $string);

Resources

PHP manual entry: http://php.net/manual/en/function.preg-replace.php

Credit where credit is due: email regex taken from preg_match manpage, and URL regex taken from: http://www.weberdev.com/get_example-4227.html

How to remove email addresses and phone number from a string in PHP?

This is just for a start up not a complete answer. You need to read more about regular expressions and its usage.

remove email addresses :

$pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $string);

Reference

For phone numbers :

$text = preg_replace('/\+?[0-9][0-9()-\s+]{4,20}[0-9]/', '[removed]', $text);

this looks for:

a plus symbol (optional), followed by a number, followed by between 4-20 numbers, brackets, dashes or spaces, followed by a number

and replaces with the string [removed].

Reference

removing @email.com from string in php

If you have PHP5.3 you could use strstr

$email = 'username@email.com';

$username = strstr($email, '@', true); //"username"

If not, just use the trusty substr

$username = substr($email, 0, strpos($email, '@'));

Extract email address from string - php

Try this

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

see demo here

Second method

<?php 
$text = 'Ruchika < ruchika@example.com >';
preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $text, $matches);
print_r($matches[0]);
?>

See demo here

Remove email address from a string

I propose to explode paragraph like this to check if each element contain @and remote it to put it in $arr_email. I don't use regex.

$my_paragraph =  "@test@gmail.com @test1@gmail.com  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker"
$arr_email = [];
$arr_message = explode(" ", $my_paragraph);

foreach($arr_message as $key=>value) {

if(strpos($value, "@") !== FALSE) {
array_push($arr_email, $value);
unset($arr_message[$key];
}

$my_paragraph = implode(" ", $arr_message);

Removing character from part of Email Address

You can use positive look-ahead, i.e.:

$result = preg_replace('/\.(?=.*@)/', '', $email);

Regex Demo

PHP Remove email addresses from comma separated string

You are adding to your result array if it DOES validate as an email, so change it to add if it DOES NOT ! validate:

$cadena = explode(",", $cadena);        

foreach($cadena as $res){
if(!filter_var($res, FILTER_VALIDATE_EMAIL)){
$result[] = $res;
}
}

Then just implode() it:

$result = implode(',', $result);

Remove link if contains specific string

You could use preg_replace (replacement using regular expressions) and surround your string with (?<=^|\s) ("is either at the beginning of the string or preceded by a space character") and \b (word boundaries):

$message = preg_replace(
'/(?<=^|\s)hello\b/',
'<a id="get-ahead-link" style="color: #ffa503;" href="https://somelink.com">hello</a>',
$message,
-1,
$count
);

Demo: https://3v4l.org/SknUT

Note: I left the additional parameters to retrieve $count.

Other (unrelated) note: such inline HTML styles should be avoided in most cases.



Related Topics



Leave a reply



Submit