-1

I have a problem I am trying to solve. I am unable to use preg_replace or str_replace as we cant predetermine the characters as the data comes from a form input.

I am trying to remove these characters ½ ´ ¤ £ € ¨ from this $name "Test ½ ´ ¤ £ € ¨";

I am the same applies for the front end it cant be predetermined characters

I have tried the below and they do not work

$name = "Test ½ ´ ¤ £ € ¨";

mb_convert_encoding(strval($name);

utf8_decode(strval($name);

My ideal out put is Test

Olivier
  • 13,283
  • 1
  • 8
  • 24
  • 4
    _"I am unable to use preg_replace or str_replace as we cant predetermine the characters as the data comes from a form input."_ - I don't see why the _source_ of any data should be stopping you, you need to explain that better. – CBroe Jul 18 '23 at 10:08
  • This is because there will be no end to changes and there may be more characters being added on the form that we cant predict – Brian Nowlan Jul 18 '23 at 10:10
  • But *why* do you want to remove such characters? – Your Common Sense Jul 18 '23 at 10:11
  • 2
    In that case, your question title is _very_ misleading, because you absolutely made it sound as if you _had_ a fixed list of characters there. Then you need to turn your explanation around - which characters would you want to _keep_ then? All "letters" only? Well then replace everything that _isn't_ a letter with an empty string - regular expressions have character classes, and they can be _negated_. – CBroe Jul 18 '23 at 10:11
  • if the source of characters to be removed is variable, and is stored somewhere (like a configuration file or database) you can use `preg_replace` or `str_replace` but with a piece of code fetching the list before running. – JoSSte Jul 18 '23 at 10:13
  • 1
    If what you are asking for _is_ what I am guessing, then here is one example (which was easily found by typing "php remove all non-letter characters from string" into Google) https://stackoverflow.com/questions/659025/how-to-remove-non-alphanumeric-characters (perhaps not _exactly_ what you need, but it should illustrate the principle.) – CBroe Jul 18 '23 at 10:14

1 Answers1

0

You can always iterate over the characters in the string. But be careful: php isn't Unicode-native.

$remove = array( '½', '´', '¤', '£', '€' );
$result = '';
$name = "Test ½ ´ ¤ £ € ¨";

$count = mb_strlen($name, 'UTF-8');
for( $i = 0; $i < $count; $i++ ){
    $char = mb_substr( $name, $i, 1, 'UTF-8' );
    if( ! in_array( $char, $remove ) {
      $result .= $char;
    }
}

This runs through the multibyte characters in the string presented by your user and lets you do anything you want with them. The example builds a result string omitting the characters you mentioned. (Not debugged, sorry.)

(With respect, I think your reason for avoiding the string-replace functions might be flawed. The good thing about using regex character classes is this: the regex authors have sorted out many oddball edge cases, of which Unicode contains uncomfortably many.)

O. Jones
  • 103,626
  • 17
  • 118
  • 172