Jump to content

Removed certain words from text


steveclondon

Recommended Posts

HI,

 

I have companies that register their email address with me. I then want  to check any text they have an move this address but I want to do it in the following way.

 

 

 

example

 

customers email address steven@here.com (this will of course  change using php)

 

I want to

 

If there is the following sequence i want to remove just the email address. So i want to check for 'steven' followed by 'here' followed by 'com' if the text is in that sequence with anything else in between I want it to just remove the steven here and com. This is so I can take out all the things people will try to get around a normal email removal regex which I have as well. Example :(x reprsents other text to be left in place) xxxxxxxxxx steven xx here xxx com xxxxx xxxxxx or for steven@here.co.uk xxxxx steven xx here xxx co xxx uk

 

 

 

I hope you follow.

Link to comment
Share on other sites

thanks for that. I aready have a normal email address regex to take out the email address. I have also looked at the other post there (I also started it) I have tried the code that was helpfully suggested there with all domain types however I found it was taking away all normal text. So i thought now the best way to do this would be to use the email address that I have stored for the user and then look for it through the text and remove it if all three parts are there in order but they can have text between them.

 

I can't use just a string replace because if for example the start of the email is a persons name I wouldn't want to remove that unless it is followed by the second part and then the final part of the email address.

 

Im not to hot on regex im affraid.

Link to comment
Share on other sites

Sorry about that; I skimmed too quickly.

 

<pre>
<?php

$tests = array(
	'steven@here.com' => 'Please contact me at steven at here dot com immediately!',
	'bob@co.uk' => 'I am available on weekends @ bob co uk.',
	'john@doe.net' => 'What if I want to give my friends address of john.someone_else@doe.net? It won\'t work...',
	'someone@sneaky.org' => 'Contact me! Just remove the x\'s in my email: xsomexonex@xsneakyx.orgx'
);

function filter_out_email ($matches) {
	### Toss the full match.
	array_shift($matches);
	return join ('', $matches);
}

foreach ($tests as $email => $test_data) {
	### Separate the e-mail into pieces.
	$pieces = preg_split('/[@.]/', $email);
	### Make the pieces safe for regexing.
	foreach ($pieces as &$piece) {
		$piece = preg_quote($piece, '/');
	}
	### Create the expression.
	$regex_for_pieces = join('(.*?)', $pieces);
	$regex = '/\b' . $regex_for_pieces . '\b/i';
	### Test.
	echo "<b>Testing $regex against:</b><br>";
	echo $test_data;
	echo '<hr size="1">';
	echo $test_data = preg_replace_callback($regex, 'filter_out_email', $test_data);
	echo '<br><hr size="5"><br>';
}

?>
</pre>

Link to comment
Share on other sites

Cheers mate looks very good. I know that this won't get around everything by any means but I will keep track of what people try and alter things as they go along to combat anything new people try. I am not going to get around anyone giving someone elses email address however I can always make a block list up at some point.

Link to comment
Share on other sites

  • 3 weeks later...

Back again. All of the above code worked brilliantly. I also have a code to remove phone numbers. Now people are trying to get clever and are writing the phone numbers such as five four three three etc. I want to count the number of written number variations in a row and if there are more than say 5 take them all out.

 

Also on the email take out code is working a treat, however I have some who are splitting there email up as an example if you had:

 

stevencart@mysite.com

 

They would put steven cart at my site dot com.

 

 

Link to comment
Share on other sites

Again, not the most graceful or efficient, but an example of how it can be (un)done:

 

<pre>
<?php

$tests = array(
	'one two three four five six seven',
	'three  nine	 two two four   five eight	',
	'nine x two . three / four (five) one',
	'What if I write one to two sentences that mention six numbers? All six will get replaced with a one-two punch from the regex.',
	'Dial one eighthundred a b c d e f g',
	'uno dos tres cuatro cinco seis',
	'eins zwei drei vier fünf sechs'
);

$numbers = array(
	'zero', 'one', 'two', 'three', 'four',
	'five', 'six', 'seven', 'eight', 'nine'
);

echo $pattern = '/(??:' . implode('|', $numbers) . ').*?){6,}/';
echo '<br><br>';

foreach ($tests as $test) {
	echo "<b>$test</b><br>Result = ";
	echo preg_replace($pattern, '', $test);
	echo '<hr>';
}

?>
</pre>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.