153

I remember doing this before, but can't find the code. I use str_replace to replace one character like this:

str_replace(':', ' ', $string); 

but I want to replace all the following characters \/:*?"<>|, without doing a str_replace for each.

0

9 Answers 9

253

Like this:

str_replace(array(':', '\\', '/', '*'), ' ', $string);

Or, in modern PHP (anything from 5.4 onwards), the slighty less wordy:

str_replace([':', '\\', '/', '*'], ' ', $string);
Sign up to request clarification or add additional context in comments.

Comments

134

str_replace() can take an array, so you could do:

$new_str = str_replace(str_split('\\/:*?"<>|'), ' ', $string);

Alternatively you could use preg_replace():

$new_str = preg_replace('~[\\\\/:*?"<>|]~', ' ', $string);

4 Comments

What's the advantage of str_replace() over preg_replace() (or vice-versa) in the OP's case?
@ludditedev No real advantage either way, it's just a matter of preference
@NullUserException let's say I want to remove all these characters: +->< . Based on your answer, I tried: preg_replace('~[\\+-><]~', '', $s); . But it is removing the digits also (0-9). What is wrong ? Demo at: 3v4l.org/inS5W
@MadhurBhaiya Change the regex to '~[\\+><-]~' (note where the - is). The dash (-) is a regex metacharacter, so it needs to escaped or strategically placed in the character class (everything inside []). See this answer: stackoverflow.com/a/7604888/396458
102

For example, if you want to replace search1 with replace1 and search2 with replace2 then following code will work:

print str_replace(
    array("search1","search2"),
    array("replace1", "replace2"),
    "search1 search2"
);

// Output: replace1 replace2

Comments

53
str_replace(
    array("search","items"),
    array("replace", "items"),
    $string
);

Comments

6

If you're only replacing single characters, you should use strtr()

2 Comments

Single characters only? How come?
3

You could use preg_replace(). The following example can be run using command line php:

<?php
$s1 = "the string \\/:*?\"<>|";
$s2 = preg_replace("^[\\\\/:\*\?\"<>\|]^", " ", $s1) ;
echo "\n\$s2: \"" . $s2 . "\"\n";
?>

Output:

$s2: "the string          "

1 Comment

Too much escaping inside the character class. (see accepted answer)
0

preg_replace() is the best for multiple items. Please check:

$invoice_no = preg_replace('~[\\+:><-]~', "",rand(0, 10) . now());

For single replacement you can use str_replace() :

$invoice_no = str_replace("-", "",rand(0, 1000) . now());

Comments

0

strtr() is suitable for your replacement task because each single-byte character is to be replaced with another single-byte character.

  • The syntax is concise because the find and replacement parameters are corresponding strings.

  • str_replace() and preg_replace() will fully traverse the input string once for each array value. This function only traverses the string once.

  • This function avoids the need for a regex pattern or an array to make multiple replacements.

Each character in the find string must be represented by a character in the replacement string or the replacement will not occur.

Code: (Demo)

$string = '<foo:bar/fizz\\buzz|fuzz|"*?">';

var_dump($string);

var_dump(
    strtr($string, '\/:*?"<>|',  '         ')
);

Output:

string(29) "<foo:bar/fizz\buzz|fuzz|"*?">"
string(29) " foo bar fizz buzz fuzz      "

Comments

0

If you're looking to replace special chars for custom links, let's say you need to replace: ( ' : ' with ' %3A ' ) and replace ( ' / ' with ' %2F ' ).

The following example:

str_replace( [':', '/'], ['%3A', '%2F'], $page );

Output URL:

https%3A%2F%2F%2Fwww.example.com%2Fpage%2Fpage_name%2F

Which is:

https://www.example.com/page/page_name/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.