0

I have this string with special characters only

 $chars="¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶¸¹º»¼½¾ÀÂÃÄÅÆÈÊËÌÎÏÐÒÔÕÖרÙÛÜÝÞßàâãäåæçèêëìî";

I have this another string with normal text with some of the special characters listed on the '$chars'occurrences.

   $string=" this is a normal text  with some special ° characters I Þ  want to remove, not replace ê £";

How do I find the ocurrences and remove the chars listed on $chars and remove them from $string?

I was thinking using str_replace() with arrays but it is too much, I would need to make an array from $chars

Here there is the white list

abcdefghijklmnñopqrstuvwxyzñáéíóúABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ0123456789[]=+-¿?¡!<>$%^&*'"()/#@*,.:;_|

Dan
  • 517
  • 1
  • 3
  • 13
  • Couldn't you just turn the logic upside down? Instead of black-listing, you should try to white-list the good chars. A simple regex for anything outside the white-listed chars would be trivial to do. – aefxx Aug 07 '18 at 21:31
  • too late I have gathered thousands of them...Is there a way to make that work? – Dan Aug 07 '18 at 21:36
  • This one would be my white list. `abcdefghijklmnñopqrstuvwxyzñáéíóúABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ0123456789[]=+-¿?¡!<>$%^&*'"()/#@*,./:;_|` – Dan Aug 07 '18 at 21:42
  • Sure, there is a way. Throw away those thousands and do it right! Black-listing is by it's very nature a limited beast, at best. – aefxx Aug 07 '18 at 21:42
  • @aefxx How can I make work this white list? – Dan Aug 07 '18 at 21:44
  • Check my answer – aefxx Aug 07 '18 at 21:54
  • wondering if you've tried: preg_replace('/[^{insert white list here}]/', '', $string); ALSO, you could use preg_replace('/[^{same list all lower case}]/i', '', $string); where the "i" modifier allows it to ignore upper/lower case. (oh, and you'd have to quote the square brackets in the white list) – UncaAlby Aug 07 '18 at 22:12
  • @UncaAlby could you give it a try please?, The white list is displayed above – Dan Aug 07 '18 at 22:14
  • Some of those characters wreak havoc with my PHP 5.6 interpreter. You may need to use their octal codes. I mean, jeez, I get division by zero errors! – UncaAlby Aug 07 '18 at 22:26

1 Answers1

0

Regarding the OP's comment:

$regex = '[^\w\s\[\]\=\+\-\¿\?\¡\!\<\>\$\%\^\&\*\'\"\(\)\/\#\@\*\,\.\/\:\;\_\|]';
mb_ereg_replace($regex, '', $string);
aefxx
  • 24,835
  • 6
  • 45
  • 55
  • 1
    It retrieves ��������ª������ – Dan Aug 07 '18 at 22:00
  • What was in `$string` that gave you this outcome? – aefxx Aug 07 '18 at 22:02
  • `$string="//abcdefghijklmnñopqrstuvwxyzñáéíóúABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ0123456789[]=+-¿?¡!<>$%^&*'()/#@*,.:;_| |||||||||| ] ¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶¸¹º»¼½ mmmmm onload onclick='' [ ? / < ~ # ` ! @ $ % ^ & * ( ) + = } | : ; ' , > { space !#$%&'()*+,-./:;<=>?@[\]^_`{|}~ sdsd ** *`` `` ´´ {} {}[] ````... ;;,,´'¡'!!!!¿?ña ñaña ÑA á é´´ è ´ 8i ó ú à à` à è`ì`ò ù & > < ksks < wksdsd '' \" \' "; ` – Dan Aug 07 '18 at 22:03
  • Updated the answer but keep in mind that `\w` in the regex might not suite your needs. You would need to replace it with the exact characters you wish to white-space if you need strict control. – aefxx Aug 07 '18 at 22:09
  • there still appears ` ¢£¤¥¦§¨©ª«¬®¯°±²³µ¶¸¹º»¼` on the retrieved string – Dan Aug 07 '18 at 22:13
  • Sry, my test say otherwise, please double check. – aefxx Aug 07 '18 at 22:18