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);
ResourcesPHP 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 hereSecond 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/SknUTNote: I left the additional parameters to retrieve $count
.
Other (unrelated) note: such inline HTML styles should be avoided in most cases.
Related Topics
Check If SQL Row Exists with PHP
Remove "(Optional)" Text from Checkout Fields in Woocommerce 3.4+
How to Select Year and Month from the Created_At Attributes of Database Table in Laravel 5.1
Jquery Ajax Form Data Serialize Using PHP
Check If Youtube and Vimeo-Clips Are Valid
How to Remove All Dtddwrappers and Labels on Zend Form Elements
PHP MySQL Query Where X = $Variable
Zend Framework 2 Routing Subdomains to Module
Force Download via Ajax and PHP
Use MySQL_Fetch_Array() with Foreach() Instead of While()
How to Add 5 Minutes to Current Datetime on PHP < 5.3
PHP Readfile() Causing Corrupt File Downloads
Php:Capturing the Command Output
Posting to a Facebook Page as the Page (Not a Person)
Laravel: How to Use Multiple Pivot Table Relationships
How to Find "Related Items" in PHP
Check If Two PHP Datetime Objects Are Set to the Same Date ( Ignoring Time )