Reniel Ramos Salvador
Reniel Ramos Salvador

Reputation: 66

replace two character: apostrophe and space in PHP

How can I remove two character? Here's my code:

str_replace('\'| ', '',"remove ' and spaces");

I'm trying to use the | but it's not working

Upvotes: 1

Views: 898

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626893

Looking at \'|, it seems you want to remove either ' or a space. str_replace (that does not allow regex as its needle argument) accepts an array of search strings as the first argument:

search
The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.

So, use

$s = str_replace(array("'",' '), "", "remove ' and spaces");
// => removeandspaces

See demo

Upvotes: 2

Related Questions