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
10
votes
3 answers

Make text between asterisks bold

I wanted to make a PHP function that would make text bold between double asterisks, and italic between one asterisk, (quite like the editor on stackoverflow). Same rules apply, if there's a space between the * and the word, it shouldn't render. Who…
Isaiah
  • 1,852
  • 4
  • 23
  • 48
9
votes
5 answers

Remove all backslashes from PHP urldecoded string

I am trying to remove all backslashes from a url-decoded string, but it is outputting \ rather than outputting the url-decoded string with \ removed. Please can you tell me my problem.
max_
  • 24,076
  • 39
  • 122
  • 211
9
votes
2 answers

Increment a number on each replace in javascript

I want to increment a number each time string.replace() replace a sub-string. for example in: var string = "This is this"; var number = 0; string.replace("is", "as"); When string.replace first is of This number becomes 1, then for second is of…
EMM
  • 171
  • 2
  • 12
9
votes
6 answers

Escaping escape Characters

I'm trying to mimic the json_encode bitmask flags implemented in PHP 5.3.0, here is the string I have: $s = addslashes('O\'Rei"lly'); // O\'Rei\"lly Doing json_encode($s, JSON_HEX_APOS | JSON_HEX_QUOT) outputs the…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
9
votes
5 answers

PHP "str_replace" doesn't work properly in some case?

The below is the php code i am using $file_handle = fopen("products.csv", "r"); $fname = "products.csv"; $fhandle = fopen($fname,"r"); $content = fread($fhandle,filesize($fname)); $server = "**\******,1433"; $connectionInfo = array(…
Max
  • 229
  • 2
  • 6
  • 18
8
votes
3 answers

String replaceAll not replacing i++;

String preCode = "helloi++;world"; String newCode = preCode.replaceAll("i++;", ""); // Desired output :: newCode = "helloworld"; But this is not replacing i++ with blank.
Android Guy
  • 573
  • 1
  • 8
  • 18
8
votes
8 answers

Replace only the first character of a string with str_replace?

I'm trying to replace only the first character of a string, but it isn't working, it replaces all the "0" on the string, here's my code: $mes2 = str_replace('0', '', $mes[0]); I want to replace the first character only if its a 0, example: 07…
Vinny
  • 141
  • 1
  • 1
  • 6
8
votes
8 answers

PHP replace string with values from array

I have a string such as: Hello <%First Name%> <%Last Name%> welcome and I have a array [0] => Array ( [First Name] => John [Last Name] => Smith ) What I need to do is take the string and replace the words in <% with the…
Yeak
  • 2,470
  • 9
  • 45
  • 71
7
votes
2 answers

Javascript Replacing All Instances of New Line Character ASCII (13) with "\r\n"?

How can I replace all instances of a newline character ASCII code (13) in a string with "\r\n"? Any help would be appreciated.
christian
  • 2,279
  • 4
  • 31
  • 42
7
votes
4 answers

Performance of str_replace in PHP

Here I have 2 methods using str_replace to replace strings in a given phrase. // Method 1 $phrase = "You should eat fruits, vegetables, and fiber every day."; $healthy = array("fruits", "vegetables", "fiber"); $yummy = array("pizza", "beer", "ice…
Raptor
  • 53,206
  • 45
  • 230
  • 366
7
votes
4 answers

Cannot replace £ with £ from string

I have a HTML string containing £ signs, for some reason i'm not able to replace them. I'm assuming this is an encoding issue although i can't work out how. The site is using ISO-8859-1 for its encoding $str = '
robjmills
  • 18,438
  • 15
  • 77
  • 121
7
votes
1 answer

Razor String Replace

I was looking for a way to do some kind of string replace. Found this snippet on here, but it gives me the error that MvcHtmlString is not defined: @MvcHtmlString.Create(Html.Encode(comic.name).Replace(" ", "-")); Whole Section Of Code: @{ var…
rackemup420
  • 1,600
  • 2
  • 15
  • 37
7
votes
4 answers

PySpark remove special characters in all column names for all special characters

I am trying to remove all special characters from all the columns. I am using the following commands: import pyspark.sql.functions as F df_spark = spark_df.select([F.col(col).alias(col.replace(' ', '_')) for col in df.columns]) df_spark1 =…
user13766314
7
votes
4 answers

str_replace_all replacing named vector elements iteratively not all at once

Let's say I have a long character string: pneumonoultramicroscopicsilicovolcanoconiosis. I'd like to use stringr::str_replace_all to replace certain letters with others. According to the documentation, str_replace_all can take a named vector and…
biomiha
  • 1,358
  • 2
  • 12
  • 25
7
votes
2 answers

Str replace method happening inplace

I have a Python string which I want to \include in a latex file. However, my string has an underscore character (_), which needs to be converted into \_ (this is latex representation for _) before included. If I do: self.name =…
Eduardo
  • 631
  • 1
  • 12
  • 25