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
7
votes
6 answers

Replace multiple characters in a string variable (VBA)

How can I replace more than one thing in a string variable? Here my example function in VBA: Private Function ExampleFunc(ByVal unitNr$) As String If InStr(unitNr, "OE") > 0 Then unitNr = Replace(unitNr, "OE", "") unitNr =…
yuro
  • 2,189
  • 6
  • 40
  • 76
7
votes
12 answers

How do I convert a string to spinal case in JavasScript?

I am stuck on this coding challenge Spinal Tap Case from freeCodeCamp. Essentially I don't know how to get the last check to execute. This is the last check: spinalCase("AllThe-small Things") should return "all-the-small-things" And this is my…
Antonio Pavicevac-Ortiz
  • 7,239
  • 17
  • 68
  • 141
7
votes
3 answers

php str_ireplace without losing case

is it possible to run str_ireplace without it destroying the original casing? For instance: $txt = "Hello How Are You"; $a = "are"; $h = "hello"; $txt = str_ireplace($a, "".$a."", $txt); $txt =…
FoxyFish
  • 874
  • 1
  • 12
  • 21
7
votes
1 answer

String Replace - Add Slash Before Single Quote

I'm trying to escape a single quote within my PHP by adding a slash before it. Unfortunately, I've been unable to get it working with str_replace and I'm wondering if I'm doing something wrong. What I have is the following ... $string = 'I love…
Brian Schroeter
  • 1,583
  • 5
  • 22
  • 41
7
votes
4 answers

"\\\\".replaceAll("\\\\", "\\") throws java.lang.StringIndexOutOfBoundsException

The following code fragment in Java: "\\\\".replaceAll("\\\\", "\\"); throws the exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 1 (NO_SOURCE_FILE:0) The javadoc on replaceAll does include a caveat on the use of…
Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331
7
votes
2 answers

jQuery, Get filename without the extension and then replace string

I have this code to get the file extension: filename1 = "a.crudely_formed.document_name.jpg" var fileExtension = filename1.substring(filename1.lastIndexOf('.')); Now I need to only get the filename, without the extension. As you can see…
Albin N
  • 1,401
  • 2
  • 16
  • 27
6
votes
1 answer

Replace text with hyperlinks where there isn't already one

A better example would be this: $string = "That is a very nice ford mustang, if only every other ford was quite as nice as this honda"; I want to replace the car names with a link for manufacturer and model if they match, or just manufacturer, but…
Chris
  • 5,516
  • 1
  • 26
  • 30
6
votes
6 answers

Remove trailing brackets in a string

I'm trying to trim trailing square brackets, inner quotes and slashes in a list of R strings, preferably using dplyr. Sample data: df <- c("['Mamie Smith']", "[\"Screamin' Jay Hawkins\"]") Expected result: "Mamie Smith", "Screamin' Jay…
6
votes
7 answers

php str_replace replacing itself

I need to replace every occurrence of one of the letters a,o,i,e,u with [aoieu]? I tried to do the following: str_replace(array('a', 'o', 'i', 'e', 'u'), '[aoieu]?', $input); But when giving it input of black instead of giving me the expected…
Daniel
  • 30,896
  • 18
  • 85
  • 139
6
votes
5 answers

Get Array of All Possible l33t Combinations in Javascript

I have a string that I'd like to get all possible replace-ment combinations on using the following substitutions: var equiv = { "a": "4", "b": "8", "e": "3", "i": "1", "l": "1", "o": "0", "t": "7" } I would like to define a…
xd1936
  • 1,038
  • 2
  • 9
  • 27
6
votes
4 answers

How to replace tab with   in PHP?

In my database I have the following text: for x in values: print x I want to print this code on my HTML page. It is printed by PHP to the HTML file as it is. But when HTML is displayed by a browser I, of course, do not see text in this form. I…
Roman
  • 124,451
  • 167
  • 349
  • 456
6
votes
1 answer

oracle utl_lms integer substitution with variables

I've encountered an oddity using oracle's UTL_LMS.MESSAGE_FORMAT. When targeting %d replacements with variable substitutions, UTL_LMS is quietly failing to replace and disrupting exception-messaging in my call-stack. UTL_LMS.FORMAT_MESSAGE calls…
alexgibbs
  • 2,430
  • 2
  • 16
  • 18
6
votes
2 answers

Replace a char into NSString

I want simply replace all occourrencies of "+" with a blank " " char... I tried some sample listed here, also used NSSMutableString, but the program crash... what's the best way to replace a char from another?? thanks
ghiboz
  • 7,863
  • 21
  • 85
  • 131
6
votes
4 answers

Substring substitution in bash

my problem of today is to replace in a string like this --> 6427//6422 6429//6423 6428//6421 every // with a ,. I tried with different commands: finalString=${startingString//[//]/,} doesn't work fileTemp=$(echo -e "$line\n" | tr "//" "," does a…
Mattia Baldari
  • 567
  • 1
  • 5
  • 15
6
votes
5 answers

PHP template class with variables?

I want to make developing on my new projects easier, and I wanted a bare bones very simple template engine solution. I looked around on the net and everything is either too bloated, or makes me cringe. My HTML files will be like so: …
Joe
  • 281
  • 1
  • 5
  • 8