Remove all symbols php

Remove Specific, Special Characters From String In PHP

To remove specific/special characters from a string in PHP; This tutorial demonstrates to you, how you can remove special or specific characters from string in PHP.

When you work with PHP or any other PHP framework like laravel, Codeigniter, Cake PHP, etc. Many times you want to remove special or specific character from a string.

Remove Special or Specific Characters From a String In PHP

This tutorial explains to you, how you can easily remove special or specific characters from string in PHP. We will show you two methods to remove the special or specific character from string with or without using a regular expression.

Читайте также:  Css target div with id and class

Str_replace PHP Function

The str_replace() function finds characters in a string and replaces some characters with some other characters in a string.

Syntax

str_replace(find,replace,string,count)

You can use PHP function str_replace() to remove special characters or specific characters from string in PHP.

Method 1: Remove Specific Character from String

We have one string if you want to remove specific characters from a string. So use the below function:

function RemoveSpecificCharacter($string)

The above example removes “World” to Tutsmake.com. When you pass any string in this function, this function removes find “World” in a given string and replace it to “Tutsmake.com”. And return a new string.

Method 2: Remove Special Character From String

If you want to remove special character (#,@,%,&,*, etc) from string in PHP. You can use the below function for that:

function remove_special_character($string) < $t = $string; $specChars = array( ' ' =>'-', '!' => '', '"' => '', '#' => '', '$' => '', '%' => '', '&' => '', '\'' => '', '(' => '', ')' => '', '*' => '', '+' => '', ',' => '', '₹' => '', '.' => '', '/-' => '', ':' => '', ';' => '', ' '', '=' => '', '>' => '', '?' => '', '@' => '', '[' => '', '\\' => '', ']' => '', '^' => '', '_' => '', '`' => '', ' '', '|' => '', '>' => '', '~' => '', '-----' => '-', '----' => '-', '---' => '-', '/' => '', '--' => '-', '/_' => '-', ); foreach ($specChars as $k => $v) < $t = str_replace($k, $v, $t); >return $t; >

The above function will remove all the special characters from the given string in PHP. If you want to add more any special character for remove in the given string, you can do it easily.

Preg_replace PHP Function

The PHP preg_replace() function is used to perform a regular expression for search and replace the content.

Syntax Of Preg_replace Function:

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

Example 1: Remove Special Character From String Using Regular Expression

You can use the preg_replace function to remove the special character from the string using regular expression in PHP.

function RemoveSpecialCharacter($string) < $result = preg_replace('/[^a-zA-Z0-9_ -]/s','',$string); return $result; >

Conclusion

In this tutorial, you have learned, how you can easily remove special characters or specific characters in the string using the PHP function str_replace() and preg_replace().

Источник

How to remove special characters from a String in PHP

Having a string of text, and a task to remove all or some of special characters from it in PHP, this article explains the different methods of achieving that with demonstrations of multiple examples.

To begin with, what do we mean by special characters?

A special character can be defined as any character that is not considered alphanumeric (an alphabet or a number), ie. if it does not fall within the range 0 — 9 or A — Z. Punctuation marks, symbols, and accent marks are considered special characters.

In PHP, you can remove some or all of the special characters within the strings using:

Method 1: Using str_replace() function

The str_replace() is an inbuilt function in PHP that is used to replace some characters with some other characters in a string.

Syntax

Parameters

  • The find is a mandatory parameter that specifies the value (character) we want to find and replace.
  • The replace is a mandatory parameter that specifies the value we should replace the character with. In this case, we will use an empty string («»).
  • The replace is a mandatory parameter that specifies the string in which we want to remove the various special characters.
  • The count is an optional parameter that is a variable that counts the number of replacements. In this case, we won’t really need it.

The function returns a string with the specified special characters removed.

Example

Hey there! How are you doing

You can see that «?«, which we have specified in our string has been removed.

To remove more than one specific character from a string, you will need to specify them all in an array as the first parameter of the function.

Example

Hey there! How are you doing?

As you can see, we have removed all the specified characters («@», «#», «(«, «)», «*», «/») from the string.

This method is best used to remove specific characters but is not suitable for removing all special characters.

The str_ireplace() function is equivalent to str_replace() in all aspects with their only difference being case sensitivity. str_replace() is case-sensititve while str_ireplace() is case-insensitive.

Method 2: Using preg_replace() function

The preg_replace() function replaces all matches of a pattern or list of patterns within a string with substrings.

Syntax

preg_replace(pattern, replacement, input, limit, count)

Parameters

  • The pattern is a mandatory parameter that consists of a regular expression or an array of regular expressions.
  • The replacement is a mandatory parameter that consists of a replacement string or an array of replacement strings.
  • The input is a mandatory parameter that specifies the string or array of strings in which replacements are to be done.
  • The limit is a mandatory parameter that sets a limit on how many replacements should be done in each string.
  • The count is an optional parameter, a variable that contains the number of replacements that have happened after the function has been executed.

The pattern comprises the word or phrase to be replaced enclosed within an opening and closing forward slash /pattern/ .

Below is the regular expression (pattern) for alphanumeric characters.

[a-zA-Z0-9] which can also be written as [[:alnum:]]

It matches all alphanumeric values, ie, all values that fall within the range of 0 — 9 and A — Z (upper and lowercase).

If we want to remove all the alphanumeric values from a string, we pass [a-zA-Z0-9] as the pattern in the preg_replace() function and replace it with an empty string «» .

Now to remove the special characters from the string, we have to use a regular expression that matches any non-alphanumeric character as shown below.

[^a-zA-Z0-9] which can also be written as [^[:alnum:]]

It’s just a negation by adding «^» immediately after the angle bracket. It matches everything that is not part of the pattern. In this case, it matches everything that does not fall within the range of 0 — 9 and A — Z (upper and lowercase).

Example

Now as you can see, we have removed all the special characters from the string.

However, as you already have noticed, it has as well removed the spaces and punctuation marks such as full stop, question mark, exclamation marks, etc.

If you want to retain other characters in the string, you need to specify them within the regular expression.

For example, the regular expression below matches lowercase alphabets a-z , uppercase alphabets A-Z , numbers 0-9 , whitespaces \s , exclamation mark ! , question mark ? , full stop . , comma , , single quote \’ and double quote » .

To remove all the characters that do not fall under the above criteria, you have to add ^ after the opening angle bracket so the regular expression to be [^a-zA-Z0-9\s. \’\»].

Example

Hey there! How are you doing?

You can now comfortably remove specific or all special characters from a string in PHP using the str_replace() and preg_replace() functions.

It’s my hope that you found this article useful.

Источник

PHP: Remove special characters from a string.

This is a guide on how to remove special characters from a string using PHP. In this post, I will provide a number of different examples on how to strip out non-alphanumeric characters.

This can be a useful approach to take if you are dealing with user-inputted data for usernames and post codes, etc.

Remove ALL non-alphanumeric characters.

If you want to remove everything that isn’t a letter or a number, then you can use the following regular expression:

//String containing non-alphanumeric characters $str = "this*is-a'test ()"; var_dump($str); //Remove any character that isn't A-Z, a-z or 0-9. $str = preg_replace("/[^A-Za-z0-9]/", '', $str); var_dump($str);

The PHP string above contains an asterisk, a hyphen, an apostrophe, a blank space and two curly brackets. However, if we pass it into preg_replace and remove all non-alphanumeric characters, we are left with the following:

preg_replace

As you can see, all of the special characters have been removed. The white space included.

Remove everything except letters, numbers and white spaces.

If you want to keep white spaces, then you can use the following regex:

//String containing special characters. $str = "this is- a' (test*&()"; //Remove any character that isn't A-Z, a-z, 0-9 or a whitespace. $str = preg_replace("/[^A-Za-z0-9 ]/", '', $str); var_dump($str);

The preg_replace example above transforms our string into the following:

This is because we instructed the preg_replace function to remove all non-alphanumeric character except spaces.

Regex to remove non-alphanumeric characters – except hyphens.

What if have a username field and you want to strip out everything except letters, numbers and hyphens?

//String containing special characters. $username = "my-username()*&>>>>"; //Remove any character that isn't A-Z, a-z, 0-9 or a hyphen. $username = preg_replace("/[^A-Za-z0-9-]/", '', $username); var_dump($username);

The PHP snippet takes “my-username()*&>>>>” and converts it into the following string:

clean username php

In the screenshot above, you can see that the hyphen character has been left untouched. This is unlike the asterisk and the ampersand characters, which have all been removed.

Allowing underscores.

In this example, we will remove all non-alphanumeric characters except the underscore character:

//String containing special characters. $username = "&( my_username ><>

The preg_match example above results in the following string:

Removing everything except letters, numbers and full stops / dots.

If you want allow full stops, you can use the following preg_match example:

//Example string. $str = "website.com/#_"; //Remove any character that isn't A-Z, a-z, 0-9 or a dot. $str = preg_replace("/[^A-Za-z0-9.]/", '', $str); var_dump($str);

If you run the snippet above you will see that preg_match strips the forward slash, the hashtag and the underscore while ignoring the dot character.

To allow spaces AND dots, you can use the following regex:

//Example string. $str = "*>hello. this' is*^ a test.(*&"; //Remove any character that isn't A-Z, a-z, 0-9, a space or a full stop. $str = preg_replace("/[^A-Za-z0-9. ]/", '', $str); var_dump($str);

This strips out all of the “bad” characters and leaves us with the following:

And that’s it. Hopefully, you found at least one of these examples useful!

Источник

How to remove special characters from string in PHP

Characters other than alphabet [A-Za-z] and digits 3 are known as special characters like symbols & ^ % @.

Removing special characters from PHP string is very easy by using php library functions like

Using preg_replace() to remove special characters from string in PHP

PHP provides preg_replace() functions to remove special characters from string.

Lets see how to remove special characters from string in PHP using Examples.

Example: remove special characters from string in PHP.

Here preg_replace() is used for regular expression search and replace the string.

Syntax of preg_replace is

preg_replace($pattern, $replacement, $string)

$pattern: regular expression to find in string

$replacement: $pattern will be replaced by this

$string: The string on which we will find pattern and replace it.

Example: Remove special characters from string in PHP.

Here $pattern is regular expression that specifies.

Consider A-Z and a-Z and 0-9 characters and a space (” “) in $str string.

Other then this replace any character with ”.

Example: Remove special characters except ! and . from string in PHP.

In above example two special characters are part of string ! and . we dont want to remove these two special characters for that we have included these two characters in pattern.

Using str_replace to remove special characters from PHP String

str_replace($search, $replace, $string);
str_replace($search, $replace, $string, $count)

$search: The character or string you want to search on $string

$replace: $search will be replaced with $replace

$string: on given string we will apply search and replace

$count: Keep record of how many replacement performed.

Example: PHP program to remove special characters from string except dot

Источник

Оцените статью