substr_replace
-
We can replace a substring of characters within a string using a similar method to that demonstrated in Ruby’s syntax for substr. This time however we’re assigning a new value to positions we specify.
Assign the first five characters of a string:
PHP
print substr_replace("hello world", "hey", 0, 5); // => hey world
Ruby
puts "hello world"[0, 5] = "hey" # => hey world
Assign all characters after a specified position. In Ruby we can use Ranges to specify a range of character positions:
PHP
print substr_replace("hello world", "p", 3); // => help
Ruby
puts "hello world"[3..-1] = "p" # => help
Assign the last five characters in the string:
PHP
print substr_replace("hello world", "p", 3); // => help
Ruby
puts "hello world"[3..-1] = "p" # => help
see also
Looking for Rails or PHP web application development, integration, and training?
Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.

