Questions tagged [str-replace]

This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

The function str_replace() replaces all occurrences of the search string with the replacement string. It returns a string or an array with all occurrences of search in subject replaced with the given replace value.

Format on PHP

str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

Example

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

References

2650 questions
618
votes
17 answers

How to replace all occurrences of a character in string?

What is the effective way to replace all occurrences of a character with another character in std::string?
big-z
  • 6,812
  • 5
  • 20
  • 19
144
votes
10 answers

Str_replace for multiple items

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.
sameold
  • 18,400
  • 21
  • 63
  • 87
144
votes
25 answers

How can I perform a str_replace in JavaScript, replacing text in JavaScript?

I want to use str_replace or its similar alternative to replace some text in JavaScript. var text = "this is some sample text that i want to replace"; var new_text = replace_in_javascript("want", "dont want", text); document.write(new_text); should…
Vish
  • 4,508
  • 10
  • 42
  • 74
94
votes
13 answers

How can I use a dictionary to do multiple search-and-replace operations?

I have to replace text like "north", "south", etc. with "N", "S" etc. in address fields. I thought of making a dictionary to hold the replacements. Suppose we have: replacements = {'NORTH':'N','SOUTH':'S','EAST':'E','WEST':'W'} address = "123 north…
user1947457
  • 997
  • 1
  • 7
  • 5
93
votes
4 answers

When to use strtr vs str_replace?

I'm having a hard time understanding when strtr would be preferable to str_replace or vice versa. It seems that it's possible to achieve the exact same results using either function, although the order in which substrings are replaced is reversed.…
andrewtweber
  • 24,520
  • 22
  • 88
  • 110
89
votes
4 answers

How to Replace dot (.) in a string in Java

I have a String called persons.name I want to replace the DOT . with /*/ i.e my output will be persons/*/name I tried this code: String a="\\*\\"; str=xpath.replaceAll("\\.", a); I am getting StringIndexOutOfBoundsException. How do I replace the…
soumitra chatterjee
  • 2,268
  • 9
  • 26
  • 48
84
votes
4 answers

PHP str_replace replace spaces with underscores

Is there a reason that I'm not seeing, why this doesn't work? $string = $someLongUserGeneratedString; $replaced = str_replace(' ', '_', $string); echo $replaced; The output still includes spaces... Any ideas would be awesome
Gisheri
  • 1,645
  • 2
  • 18
  • 27
66
votes
1 answer

How to replace multiple items from a text string in PHP?

I want to be able to replace spaces with - but also want to remove commas and question marks. How can I do this in one function? So far, I have it replacing spaces: str_replace(" ","-",$title)
James Brandon
  • 1,350
  • 3
  • 16
  • 43
60
votes
5 answers

str_replace in Twig

I want to do a simple str_replace in my twig template. I'm new to twig and probably I need to add new filter or sth like that or to use existing. How can I do this? Where can I find list of filters available?
umpirsky
  • 9,902
  • 13
  • 71
  • 96
54
votes
5 answers

PHP explode the string, but treat words in quotes as a single word

How can I explode the following string: Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor into array("Lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit", "dolor") So that the text in quotation is treated as a single…
timofey
  • 543
  • 1
  • 4
  • 4
50
votes
5 answers

str_replace with array

I'm having some troubles with the PHP function str_replace when using arrays. I have this message: $message = strtolower("L rzzo rwldd ty esp mtdsza'd szdepw ty esp opgtw'd dple"); And I am trying to use str_replace like this: $new_message =…
LautaroAngelico
  • 623
  • 2
  • 6
  • 7
41
votes
4 answers

PHP preg_replace/preg_match vs PHP str_replace

Can anyone give me a quick summary of the differences please? To my mind, are they both doing the same thing?
benhowdle89
  • 36,900
  • 69
  • 202
  • 331
38
votes
4 answers

PHP string replace match whole word

I would like to replace just complete words using php Example : If I have $text = "Hello hellol hello, Helloz"; and I use $newtext = str_replace("Hello",'NEW',$text); The new text should look like NEW hello1 hello, Helloz PHP returns NEW…
NVG
  • 3,248
  • 10
  • 40
  • 60
29
votes
8 answers

Simple: How to replace "all between" with php?

$string = "i dont know what is here" $string = str_replace("???", "", $string); echo $string; // So what code am i looking for?
faq
  • 2,965
  • 5
  • 27
  • 35
29
votes
9 answers

C# string replace

I want to replace "," with a ; in my string. For example: Change "Text","Text","Text", to this: "Text;Text;Text", I've been trying the line.replace( ... , ... ), but can't get anything working properly.
Bobcat88
  • 738
  • 2
  • 7
  • 22
1
2 3
99 100