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
29
votes
2 answers

Regex str_replace

Would it be possible to incorporate a str_replace method with a regular expression, designed to catch url strings in a simple html < textfield > input type? I'm considering something simple like a tip for the user to set it up as follows: This is…
Mikkel Winther
  • 567
  • 1
  • 4
  • 10
27
votes
9 answers

I have a string with "\u00a0", and I need to replace it with "" str_replace fails

I need to clean a string that comes (copy/pasted) from various Microsoft Office suite applications (Excel, Access, and Word), each with its own set of encoding. I'm using json_encode for debugging purposes in order to being able to see every single…
0plus1
  • 4,475
  • 12
  • 47
  • 89
26
votes
4 answers

array_map with str_replace

Is it possible to use array_map in conjunction with str_replace without calling another function to do the str_replace? For example: array_map(str_replace(' ', '-', XXXXX), $myArr);
Lizard
  • 43,732
  • 39
  • 106
  • 167
23
votes
3 answers

PHP: "... variables can be passed by reference" in str_replace()?

I created a function to print a prepared-statement-sql-string with the variables in it, based on what I found in this other StackOverflow question. Here is my code: foreach($params as $idx => $param) { if ($idx == 0) continue; $sql =…
Dexter
  • 3,072
  • 5
  • 31
  • 32
22
votes
6 answers

how to replace "(double quotes) in a string with \" in java

I have string variable strVar with value as ' "value1" ' and i want to replace all the double quotes in the value with ' \" '. So after replacement value would look like ' \"value1\" ' How to do this in java? Kindly help me.
net user
  • 573
  • 2
  • 8
  • 21
21
votes
6 answers

String.replaceAll() is not working for some strings

I am editing some email that got from tesseract ocr. Here is my code: if (email != null) { email = email.replaceAll(" ", ""); email = email.replaceAll("caneer", "career"); email = email.replaceAll("canaer", "career"); …
Neeraj
  • 1,612
  • 7
  • 29
  • 47
20
votes
2 answers

How to replace/escape U+2028 or U+2029 characters in PHP to stop my JSONP API breaking

Ok I am running a public JSONP API which the data is served from my PHP server. I just read this article: JSON: The JavaScript subset that isn't (by Magnus Holm; May 2011) (please read for clarification) Basically if my JSON strings contains a…
zuallauz
  • 4,328
  • 11
  • 43
  • 54
19
votes
6 answers

C++ replace multiple strings in a string in a single pass

Given the following string, "Hi ~+ and ^*. Is ^* still flying around ~+?" I want to replace all occurrences of "~+" and "^*" with "Bobby" and "Danny", so the string becomes: "Hi Bobby and Danny. Is Danny still flying around Bobby?" I would prefer…
John Goodson
  • 191
  • 1
  • 1
  • 3
19
votes
8 answers

How to remove accents and tilde in a C++ std::string

I have a problem with a string in C++ which has several words in Spanish. This means that I have a lot of words with accents and tildes. I want to replace them for their not accented counterparts. Example: I want to replace this word: "había" for…
Alejo
  • 787
  • 2
  • 9
  • 15
18
votes
11 answers

Removing Dollar and comma from string

How can we remove dollar sign ($) and all comma(,) from same string? Would it be better to avoid regex? String liveprice = "$123,456.78";
Varun Vishnoi
  • 980
  • 1
  • 9
  • 32
17
votes
9 answers

How to find and replace all occurrences of a substring in a string?

I need to search a string and edit the formatting of it. So far I can replace the first occurrence of the string, but I am unable to do so with the next occurrences of this string. This is what I have working, sort of: if(chartDataString.find("*A")…
Sarah
  • 2,713
  • 13
  • 31
  • 45
16
votes
5 answers

How to remove brackets from string in php?

I have the following string and would like to use str_replace or preg_replace to remove the brackets but am unsure how. I have been able to remove the opening brackets using str_replace but can't remove the closing brackets. This is the…
hairynuggets
  • 3,191
  • 22
  • 55
  • 90
16
votes
3 answers

is there a way to use tr/// (or equivalent) in java?

I would like to know if there is an equivalent to tr/// (as used in Perl) in Java. For example, if I wanted to replace all "s"s with "p"s in "mississippi" and vice versa, I could, in Perl, write #shebang and pragmas snipped... my $str =…
Limao Luo
  • 161
  • 1
  • 4
15
votes
7 answers

In C++, what's the fastest way to replace all occurrences of a substring within a string with another string?

I'm looking for the most efficient (in terms of "fastest") way to replace all occurrences of a substring within a string with another string. All I've came up with so far is: std::string StringReplaceAll(const std::string &cstSearch, const…
Oliver K.
  • 153
  • 1
  • 5
15
votes
6 answers

Remove term only if it is on its own line

I have this: $text = ' hello world    hello '; How do I remove   only if it is on its own line? So with above example, the second   should be removed. Results should be: $text = ' hello world …
Henrik Petterson
  • 6,862
  • 20
  • 71
  • 155