Sukanta Paul
Sukanta Paul

Reputation: 792

How to remove all punctuation in a string just get the words separated by spaces in PHP

I want to remove any type of special characters in a string like this:

This is, ,,, *&% a ::; demo +  String.  +
Need to**@!/// format:::::
 !!! this.`

Output Required:

This is a demo String Need to format this

How to do this using REGEX?

Upvotes: 5

Views: 6123

Answers (4)

Sarfraz
Sarfraz

Reputation: 382696

Use:

preg_replace('#[^a-zA-Z0-9 ]#', '', $yourString);

If characters are not alphabet, numbers or space, it is replaced with empty string.

Example:

$yourString = 'This is, ,,, *&% a ::; demo +  String.  + Need to**@!/// format::::: !!! this.`';
$newStr = preg_replace('#[^a-zA-Z0-9 ]#', '', $yourString);
echo $newStr;

Result:

This is a demo String Need to format this

So you can allow more characters if you want by putting them in:

[^a-zA-Z0-9 ]

Note: Also if you don't want to allow multiple spaces between words (though they wont be shown when output in browser), you need to use this instead:

preg_replace('#[^a-zA-Z0-9]+#', ' ', $yourString);

Upvotes: 4

Paul
Paul

Reputation: 141829

$string = preg_replace('/[^a-z]+/i', ' ', $string);

You may also want to allow ' in your character class to have conjunctions like don't not be turned into don t:

$string = preg_replace('/[^a-z\']+/i', ' ', $string);

You might also want to trim it afterwards to remove leading and trailing whitespace:

$string = trim(preg_replace('/[^a-z\']+/i', ' ', $string));

Upvotes: 2

Sampson
Sampson

Reputation: 268344

Check for any repeated instance of a non-number, non-letter character and repeat with a space:

# string(41) "This is a demo String Need to format this"
$str = trim( preg_replace( "/[^0-9a-z]+/i", " ", $str ) );

Demo: http://codepad.org/hXu6skTc

/       # Denotes start of pattern
[       # Denotes start of character class
 ^      # Not, or negative
 0-9    # Numbers 0 through 9 (Or, "Not a number" because of ^
 a-z    # Letters a through z (Or, "Not a letter or number" because of ^0-9
]       # Denotes end of character class
+       # Matches 1 or more instances of the character class match
/       # Denotes end of pattern
i       # Case-insensitive, a-z also means A-Z

Upvotes: 15

flowfree
flowfree

Reputation: 16462

echo preg_replace('/[^a-z]+/i', ' ', $str); 
// This is a demo String Need to format this 

Upvotes: 2

Related Questions