Rashmi Sonke
Rashmi Sonke

Reputation: 53

How to replace all available special character and numbers in php using regular expression

I am using AES algorithm and its encrypting the string with all avilable special chanracter and number. Please help me to write a ereg_replace function which will remove all special character and number from the encrypted string.

Example of the string is:

HyS7Nj+c3b3+1kaT6gLpK9kDQS3lIDtYUNQHtz/bLAw=

i have used following:

    $enc1 = preg_replace('/[0-9]/', '', $enc);
$enc2=preg_replace('/[\/\&%#\$]/', '', $enc1);
$en=preg_replace('/[\"\'\|]/', '', $enc2);

Every time this string it gets change. so please help me. i want to replace this with any random character.

help me to write one single preg_replace which will just give me alphabets in resulted string.

Upvotes: 1

Views: 2496

Answers (2)

Meenakshi
Meenakshi

Reputation: 175

You can try this:

To remove just special characters use this.

$enc = "HyS7Nj+c3b3+1kaT6gLpK9kDQS3lIDtYUNQHtz/bLAw=";
echo preg_replace('/\W/', '', $enc);

To remove both numbers and special characters use this.

$enc = "HyS7Nj+c3b3+1kaT6g$#@LpK9kDQS3lIDtYU%^NQHtz/bLAw=";
echo preg_replace('/\W|\d/', '', $enc);

Upvotes: 2

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

You can simply use the following regex

[^a-zA-Z]

Using preg_replace like as

$enc = "HyS7Nj+c3b3+1kaT6gLpK9kDQS3lIDtYUNQHtz/bLAw=";
echo preg_replace('/[^a-zA-Z]/', '', $enc);

Explanation : The above regex will capture all those characters which are not alphabets

Demo

Upvotes: 0

Related Questions