Php remove all symbols from string

Searching through the internet and this website as well, I've found several topics on the matter. Thing is, there are countless solutions if the inserted strings must contain only characters of the Latin alphabet, but when the case requires text of other alphabets it gets a bit tricky.

Is there any way I can strip in PHP all symbols from a string, but leave the actual letters of all UTF-8 alphabets? I have tried already creating an array of all the characters of my keyboard and then by using str_replace or preg_replace remove them, but then I found out that different countries have also different keyboards sometimes which include different symbols. For example, my qwerty keyboard doesn't have the £ symbol, which a British keyboard might have.

I know this is a weird question, I am just wondering if there is an easy solution to it which I may have missed.

Any help would be very much appreciated!

EDIT: OK After some better and extended Google-ing I have found out that the following regular expression works fine for what I need and it keeps all letters of all types of alphabets while removes all symbols. I am sharing it here in case somebody else would need to do the same.

$string = preg_replace('/[^\p{L}\p{N}\s]/u', '', $string);

In this article, we will see how to remove special characters from strings in PHP, along with understanding their implementation through the examples. We have given a string and we need to remove special characters from string str in PHP, for this, we have the following methods in PHP:

Using str_replace() Method: The str_replace() methodis used to remove all the special characters from the given string str by replacing these characters with the white space (” “).

Syntax:

str_replace( $searchVal, $replaceVal, $subjectVal, $count )

Example: This example illustrates the use of the str_replace() function to remove the special characters from the string.

PHP

<?php

  function RemoveSpecialChar($str) {

      $res = str_replace( array( '\'', '"',

      ',' , ';', '<', '>' ), ' ', $str);

      return $res;

      }

  $str = "Example,to remove<the>Special'Char;";

  $str1 = RemoveSpecialChar($str);

  echo $str1;

?>

Output:

Example to remove the Special Char

Using str_ireplace() Method: The str_ireplace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “). The difference between str_replace and str_ireplace is that str_ireplace is case-insensitive.

Syntax:

str_ireplace( $searchVal, $replaceVal, $subjectVal, $count )

Example: This example illustrates the use of the str_ireplace() method to remove all the special characters from the string.

PHP

<?php

  function RemoveSpecialChar($str){

      $res = str_ireplace( array( '\'', '"',

      ',' , ';', '<', '>' ), ' ', $str);

      return $res;

      }

  $str = "Example,to remove<the>Special'Char;";

  $str1 = RemoveSpecialChar($str);

  echo $str1;

?>

Output:

Example to remove the Special Char

Using preg_replace() Method: The preg_replace() method is used to perform a regular expression for search and replace the content.

Syntax:

preg_replace( $pattern, $replacement, $subject, $limit, $count )

Example: This example illustrates the use of the preg_replace() method where the particular or all the matched pattern found in the input string will be replaced with the substring or white space.

PHP

<?php

  function RemoveSpecialChar($str){

      $res = preg_replace('/[^a-zA-Z0-9_ -]/s',' ',$str);

      return $res;

  }

  $str = "Example,to remove<the>Special'Char;";

  $str1 = RemoveSpecialChar($str);

  echo $str1;

?>

Output:

Example to remove the Special Char

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


How remove all special characters from a string in PHP?

This should do what you're looking for: function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. }

How do I remove special characters from a string?

Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).

How can I replace multiple characters in a string in PHP?

Approach 1: Using the str_replace() and str_split() functions in PHP. The str_replace() function is used to replace multiple characters in a string and it takes in three parameters. The first parameter is the array of characters to replace.

How do I remove a word from a string in PHP?

Answer: Use the PHP str_replace() function You can use the PHP str_replace() function to replace all the occurrences of a word within a string.

Tải thêm tài liệu liên quan đến bài viết Php remove all symbols from string