steveclondon Posted March 1, 2007 Share Posted March 1, 2007 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 [email protected] (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 [email protected] xxxxx steven xx here xxx co xxx uk I hope you follow. Quote Link to comment https://forums.phpfreaks.com/topic/40657-removed-certain-words-from-text/ Share on other sites More sharing options...
effigy Posted March 1, 2007 Share Posted March 1, 2007 There are patterns for matching e-mails here. Also, see this post. Quote Link to comment https://forums.phpfreaks.com/topic/40657-removed-certain-words-from-text/#findComment-197018 Share on other sites More sharing options...
steveclondon Posted March 1, 2007 Author Share Posted March 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/40657-removed-certain-words-from-text/#findComment-197141 Share on other sites More sharing options...
effigy Posted March 1, 2007 Share Posted March 1, 2007 Sorry about that; I skimmed too quickly. <pre> <?php $tests = array( '[email protected]' => 'Please contact me at steven at here dot com immediately!', '[email protected]' => 'I am available on weekends @ bob co uk.', '[email protected]' => 'What if I want to give my friends address of [email protected]? It won\'t work...', '[email protected]' => 'Contact me! Just remove the x\'s in my email: [email protected]' ); 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> Quote Link to comment https://forums.phpfreaks.com/topic/40657-removed-certain-words-from-text/#findComment-197159 Share on other sites More sharing options...
steveclondon Posted March 7, 2007 Author Share Posted March 7, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/40657-removed-certain-words-from-text/#findComment-201508 Share on other sites More sharing options...
steveclondon Posted March 27, 2007 Author Share Posted March 27, 2007 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: [email protected] They would put steven cart at my site dot com. Quote Link to comment https://forums.phpfreaks.com/topic/40657-removed-certain-words-from-text/#findComment-215879 Share on other sites More sharing options...
effigy Posted March 27, 2007 Share Posted March 27, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/40657-removed-certain-words-from-text/#findComment-216156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.