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

PHP, what is the better choice for removing a known string?

I am looking to search for and replace a known string from within another string. Should I use str_replace() or preg_replace()? The string to be replaced would be something similar to [+qStr+], [+bqID+], or [+aID+] and it would be being searched for…
Brook Julias
  • 2,085
  • 9
  • 29
  • 44
5
votes
2 answers

How to delete empty lines in a file using Emacs?

In Emacs, how to remove all empty lines (including tabs and spaces) in file? Can M-x replace-regexp do the trick? I can find the empty lines with regexp: ^[st]*$, but don't know how to replace it by deleting.
herbertD
  • 10,657
  • 13
  • 50
  • 77
5
votes
2 answers

Replace string with a new line in Batch

set NLM=^ set NL=^^^%NLM%%NLM%^%NLM%%NLM% SET memoli=%token:QMZ=%NL%%% echo %memoli%>>%tmp%\list2.txt I cant change the string "QMZ" with a new line. How to do that?
Rıdvan Çetin
  • 183
  • 5
  • 16
5
votes
2 answers

PHP: Escaping RegEx-reserved characters - anyone know what's wrong with this?

I'm trying to escape regex-reserved characters with a backslash (don't ask -- suffice it to say I'm NOT trying to parse HTML :) ) And I'm getting something odd. $regex_chars = array('[' , '\\' , '^', '$' , '.' , '|' , '?' , '*' , '+' , '(' ,…
Greg
  • 7,782
  • 7
  • 43
  • 69
5
votes
3 answers

Replace dash character in Java String

I tried to replace "-" character in a Java String but is doesn't work : str.replace("\u2014", ""); Could you help me ?
wawanopoulos
  • 9,614
  • 31
  • 111
  • 166
5
votes
2 answers

can't replace \n in php with content from database

I try to avoid asking stupid questions, but this for some reason is just not working. I'm retrieving some text from a database, with newlines included. When using echo straight away, this is the output: === BOLD TEXT ===\nHere is some text. This is…
MaxQ
  • 595
  • 1
  • 8
  • 25
5
votes
1 answer

Simple way of replacing a parts of NSMutableString/NSString

I am new to iPhone development (using iOS 5) and I am trying to replace : with spaces in an NSMutableString. I have been struggling with this since an hour and it forces me to use replaceOccurrencesOfString:@":" withString:@" " options:nil range:;…
Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169
5
votes
1 answer

String Replace in Gimp Script Fu

I have a Gimp plugin that renames files and I needed a replace function. Unfortunately TinyScheme that Gimp uses does not have a replace function for strings. I searched a lot but couldn't find one that worked as a true string-replace. Answer to…
Isaac
  • 121
  • 8
5
votes
3 answers

Replace multiple placeholders with PHP?

I have a function that sends out site emails (using phpmailer), what I want to do is basically for php to replace all the placheholders in the email.tpl file with content that I feed it. The problem for me is I don't want to be repeating code hence…
PHPLOVER
  • 7,047
  • 18
  • 37
  • 54
4
votes
1 answer

php sprintf replace multiple placeholders with one value

Using sprintf() how can I replace multiple placeholders in a string with one value? I know normally you will pass 1 variable for each placeholder, but I understand if you use the format %1$s instead of %s you can pass a single value, but I can't…
leejmurphy
  • 994
  • 3
  • 17
  • 28
4
votes
1 answer

array to string conversion notice when using str_replace

When I use str_replace with an array as second argument like in the much upvoted and accepted answer to this question, I get an array to string conversion notice. Why? Example: $str = 'a b a ba a'; $numerals = range(1, 10); $str = str_replace('a',…
greg0ire
  • 22,714
  • 16
  • 72
  • 101
4
votes
5 answers

str_replace with strpos?

The str_replace function with a strpos checking can avoid extra work? METHOD 1 ... if (strpos($text, $tofind) !== FALSE) $text = str_replace($tofind, $newreplace, $text); ... METHOD 2 ... $text = str_replace($tofind, $newreplace,…
Manz
  • 948
  • 12
  • 26
4
votes
4 answers

How to find a match within a string based on character from other column?

I have these data: df1 <- data.frame(matrix(, nrow=2, ncol=2)) colnames(df1) <- c("ca", "ea") df1$ca <- c("A=C,T=G", "T=C,G=G") df1$ea <- c("G", "T") And I want to make a new column called "match" which gives me the letter in column "ca" that is…
krtbris
  • 344
  • 1
  • 9
4
votes
2 answers

how to match bigger word first, from multiple words separated by OR operator in RegExp ? using java script

I need a regex to first match a numbers with 'px' after it, and then match remaining numbers having 'p' after it. I want to replace parts of string, that are number or a number having 'p' or 'px' after it by '*' character. a regexp i tried…
4
votes
1 answer

How to remove special characters from a string before specific character?

I have a df that has a column called EMAIL, which contains various email addresses. I want to remove all the special characters, specifically ., -, and _ that come before @ and append a new column NEW_EMAIL. For example, if df['EMAIL'] =…