Chris Burgess
0
Q:

string replace in php


<?php

echo str_replace("world","Peter","Hello world!");

?> 
0
str_replace ($search, $replace, $subject);
23
$new_string = str_replace( $take_out, $put_in, $string);
3
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// Fornece: Hll Wrld f PHP
1
<?php
  // This is the string we will search through
  $stringToSearch = "Hello World";
  // This is the string we are trying to find
  $stringWeAreLookingFor = "World";
  // If we find the string we are looking for, we will replace
  // it with this string
  $replaceString = "PHP";
  // We use the str_replace method to replace a substring in php
  $newString = str_replace( $stringWeAreLookingFor, $replaceString, $stringToSearch );

  // The newString variable now contains the string "Hello PHP"
  // Please note this search IS case sensitive. Use str_ireplace()
  // case in-sensitive searches.

  // This function can also be used with an array of strings 
  $stringArray = [ "Test", "Test", "Goose" ];
  $stringWeAreLookingFor = "Test";
  $replaceString = "Duck";

  $newArray = str_replace( $stringWeAreLookingFor, $replaceString, $stringArray );
  // The newArray variable will now contain [ "Duck", "Duck", "Goose" ]
?>
2

New to Communities?

Join the community