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

Regular expression to remove an iframe

I'm trying to use str_replace to replace all iframes with my own content. I believe I can use regular expression but not exactly sure how to remove all content within the iframe tag. Since I'm not really sure how to achieve this, below is what I…
Analog
  • 201
  • 1
  • 2
  • 13
6
votes
4 answers

How to remove spaces from a text input box

I have the following jQuery shell which works: $('.jq_noSpaces').on('change', function(){ alert('you changed the value in the box'); }); My form attributes are id="username" name="username" How do I use the following jQuery replace function…
H. Ferrence
  • 7,906
  • 31
  • 98
  • 161
6
votes
1 answer

PHP str_replace with function

Is it possible use str_replace() and use function in replace? $value = "gal($data)"; $replace = str_replace($dat, $value, $string); gal($data) is a function and I need replace one code for this function and show, but the script only give me…
Robert
  • 315
  • 2
  • 5
  • 9
6
votes
1 answer

Cannot work out a php str_replace() to remove comma

In a PHP project, I have: $string = "1,555"; str_replace(',', '', $string); echo $string; //is still 1,555 str_replace does not remove the comma. If I var_dump. I get string(5) "1,555" Any ideas? I just simply need to remove the commas so I can…
KiloJKilo
  • 465
  • 1
  • 6
  • 16
6
votes
5 answers

Replace " ’ " with " ' " in PHP

I'm grabbing a string from the database that could be something like String’s Title however I need to replace the ’ with a ' so that I can pass the string to an external API. I've used just about every variation of escaped strings in str_replace()…
Mike R
  • 951
  • 1
  • 8
  • 13
5
votes
5 answers

Replacing certain characters in javascript

Coming from a PHP background, I am a little spoiled with the str_replace function which you can pass an array of haystacks & needles. I have yet not seen such a function in Javascript, but I have managed to get the job done, altough ugly, with the…
Industrial
  • 41,400
  • 69
  • 194
  • 289
5
votes
3 answers

Is str_replace faster with array?

My question is if using array on str_replace is faster than doing it multiple times. My question goes for only two replaces. With array $phrase = "You should eat fruits, vegetables, and fiber every day."; $healthy = array("fruits",…
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
5
votes
4 answers

How to get rid of backslash("\")character from a string

Is there a way, where i can avoid the '\' character from a string ? //bit array BitArray b1 = new BitArray(Encoding.ASCII.GetBytes("10101010")); BitArray b2 = new BitArray(Encoding.ASCII.GetBytes("10101010")); //Say i perform XOR…
this-Me
  • 2,139
  • 6
  • 43
  • 70
5
votes
3 answers

How can I replace white space in filename of uploaded file

I'm making a SWF uploader and have my HTML form done. It works totally fine until I upload a SWF file with spaces in the name. How can I replace whitespace with underscores? I have tried... str_replace(" ","_", $file); ...and... preg_replace("…
Suf
  • 51
  • 1
  • 1
  • 2
5
votes
1 answer

R - why does str_detect return a different result than grepl when using word boundary on 'words' ending with dash

The help page for str_detect states "Equivalent to grepl(pattern, x)", however: str_detect("ALL-", str_c("\\b", "ALL-", "\\b")) [1] FALSE While grepl(str_c("\\b", "ALL-", "\\b"), "ALL-") [1] TRUE I imagine one of these is not working as intended?…
5
votes
1 answer

Which function is faster? substr() or str_replace()?

I have a script where I can use either substr() or str_replace(). With substr() I just have to cut off the last character and with str_replace I need to replace ! with nothing. Which one would be faster? I guess substr()?
Michiel Pater
  • 22,377
  • 5
  • 43
  • 57
5
votes
1 answer

Replace multiple words in R easily; str_replace_all gives error that two objects are not equal lengths

I'm trying to use str_replace_all to replace many different values (i.e. "Mod", "M2", "M3", "Interviewer") with one the consistent string (i.e. "Moderator:"). I'm doing this with multiple different categories, and I want avoid having to write each…
J.Sabree
  • 2,280
  • 19
  • 48
5
votes
7 answers

How to add .jpg to end of string if it doesn't exist

I have the "original list" and I'm trying to convert it to the "desired list" using php. When the .jpg is present at the end of the string, I want to do nothing. If .jpg isn't the end of the string, I want to add .jpg to the end. The "original list"…
Mr. B
  • 2,677
  • 6
  • 32
  • 42
5
votes
2 answers

Remove all hyphens, special characters etc. from string in PHP

Is there a general used regex that removes ALL hyphens, special characters etc., so I will only get letters. For example a regex containing: ,./<>?;':"|[]{}-=_+1234567890!@#$%^&*()|\ ~` and all of the hyphens and special characters. (don't know if…
Maarten Wolfsen
  • 1,625
  • 3
  • 19
  • 35
5
votes
2 answers

scraper php returning a blank page

I am new to php and I have made a scraper.php page where you can retrieve weather information from "http://www.weather-forecast.com" for any given city. I was following the instructor and I cannot see why my code is returning a blank page when its…
user3084840
  • 111
  • 1
  • 6