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
15
votes
8 answers

Replace string in an array with PHP

How can I replace a sub string with some other string for all items of an array in PHP? I don't want to use a loop to do it. Is there a predefined function in PHP that does exactly that? How can I do that on keys of array?
hd.
  • 17,596
  • 46
  • 115
  • 165
15
votes
5 answers

Python string.replace() not replacing characters

Some background information: We have an ancient web-based document database system where I work, almost entirely consisting of MS Office documents with the "normal" extensions (.doc, .xls, .ppt). They are all named based on some sort of arbitrary ID…
aelindeman
  • 659
  • 2
  • 8
  • 14
15
votes
6 answers

swap two words in a string php

Suppose there's a string "foo boo foo boo" I want to replace all fooes with boo and booes with foo. Expected output is "boo foo boo foo". What I get is "foo foo foo foo". How to get expected output rather than current one? $a = "foo boo foo…
codefreak
  • 6,950
  • 3
  • 42
  • 51
14
votes
1 answer

Best way to remove '\xad' in Python?

I'm trying to build a corpus from the .txt file found at this link. I believe the instances of \xad are supposedly 'soft-hyphens', but do not appear to be read correctly under UTF-8 encoding. I've tried encoding the .txt file as iso8859-15, using…
14
votes
4 answers

How to remove 'em' dash from a string?

I've looked at other solutions here and here, but it's not working for me. Code $s1clean = 'ALIEN - FILM - MOVIE – PSP – Sony - Boxed & Complete'; echo $s1clean; echo "

"; // Remove dash $s1clean = str_replace('-', '', $s1clean); // Remove…
user3314053
  • 239
  • 1
  • 3
  • 11
14
votes
3 answers

Replace multiple dashes with one dash

I have a string which looks like this: something-------another--thing //^^^^^^^ ^^ I want to replace the multiple dashes with a single one. So the expected output would be: something-another-thing //^ ^ I tried to use…
MM PP
  • 4,050
  • 9
  • 35
  • 61
14
votes
7 answers

Can't get str_replace() to strip out spaces in a PHP string

Hi, I am getting a PHP string which I need to strip the spaces out of. I have used the following code but when I echo $classname it just displays the string still with the spaces in it.
Paul Elliot
  • 475
  • 2
  • 6
  • 21
12
votes
2 answers

Pandas Dataframe replace part of string with value from another column

I having replace issue while I try to replace a string with value from another column. I want to replace 'Length' with df['Length']. df["Length"]= df["Length"].replace('Length', df['Length'], regex = True) Below is my data Input: **Formula** …
Js _ lfzr
  • 141
  • 1
  • 8
12
votes
1 answer

Using str_replace multiple times on the same string

I'm looping through a title from a table so it's essentially something along these lines. foreach($c as $row){ echo string_shorten($row['title']); } What I'm doing is trying is a switch statement that would switch between what I want it to…
stepquick
  • 398
  • 1
  • 3
  • 14
11
votes
7 answers

str_replace A1-A9 to A01-A09 and so on

Hi I have a following strings in my data and would like to replace A1-A9 to A01-A09 and B1-B9 to B01-B09 but keep the numbers >=10. rep_data=data.frame(Str= c("A1B10", "A2B3", "A11B1", "A5B10")) Str 1 A1B10 2 A2B3 3 A11B1 4 A5B10 There is a…
Alexander
  • 4,527
  • 5
  • 51
  • 98
11
votes
3 answers

Replacing \r\n (newline characters) after running json_encode

So when I run json_encode, it grabs the \r\n from MySQL aswell. I have tried rewriting strings in the database to no avail. I have tried changing the encoding in MySQL from the default latin1_swedish_ci to ascii_bin and utf8_bin. I have done tons of…
Robbie Trencheny
  • 549
  • 1
  • 4
  • 19
10
votes
3 answers

php how to make a if str_replace?

$str is some value in a foreach. $str = str_replace('_name_','_title_',$str); how to make a if str_replace? I want do the thing if have a str_replace then echo $str, else not, jump the current foreach then to the next. Thanks.
fish man
  • 2,666
  • 21
  • 54
  • 94
10
votes
3 answers

Regular Expression to remove a letter and dot combination

I am trying to use regular expressions in R to remove text - either an 'X' or an 'X.' - from the front of a number. I am new to regular expressions and having a hard time getting this to work. I have tried every combination of X and . with or…
mrpargeter
  • 339
  • 3
  • 12
10
votes
4 answers

How to replace last comma in string with "and" using php?

I'm trying to replace the last occurence of a comma in a text with "and" using strrchr() and str_replace(). Example: $likes = 'Apple, Samsung, Microsoft'; $likes = str_replace(strrchr($likes, ','), ' and ', $likes); But this replaces the entire…
AlexioVay
  • 4,338
  • 2
  • 31
  • 49
10
votes
5 answers

Replace all occurrences of a substring in a string in C

I'm trying to make a function in C to replace all occurrences of a substring in a string. I made my function, but it only works on the first occurrence of the substring in the bigger string. Here is the code so far: void strreplace(char string*,…
Erik Blenert
  • 103
  • 1
  • 1
  • 6