sohel14_cse_ju
sohel14_cse_ju

Reputation: 2521

How to Remove special character from a string

I want to remove the following special character from my string.

:
'
""
`
``

how can i remove each of the above character from my string?

Upvotes: 0

Views: 237

Answers (3)

user456814
user456814

Reputation:

This is a more efficient solution than using preg_replace, which uses regular expressions:

$string = str_replace(array(':',"'",'"','`'), '', $sourceString);

You can read more about str_replace and preg_replace at the php docs:

Upvotes: 0

Ant
Ant

Reputation: 3887

Use str_replace:

$to_remove = array(':', "'", '"', '`'); // Add all the characters you want to remove here

$result = str_replace($to_remove, '', $your_string);

This will replace all the characters in the $to_remove array with an empty string, essentially removing them.

Upvotes: 4

Michael Robinson
Michael Robinson

Reputation: 29498

You could use preg_replace

$string = preg_replace('/[:'" `]/', '', $string);

Upvotes: 1

Related Questions