Table of contents Home page
echo substr_replace('Hello John', 'Jane', 6), "\n"; // Change the substring 'John' into 'Jane' echo substr_replace('Hello world', '!', 11), "\n"; // Insert an exclamation mark at the end echo substr_replace('Hello world!', '', 11), "\n"; // Remove the exclamation mark at the end $s = array('John, how are you?', 'Jane, how are you?'); $greetings = substr_replace($s, ' doing?', 17); // Change '?' into ' doing?' in each string foreach ($greetings as $greeting) { echo $greeting, "\n"; } $s = array('John, how are you?', 'Jane, how are you?'); $r = array('now?', 'today'); // Change '?' into ' now?' in the first and into ' today?' in the second one $greetings = substr_replace($s, $r, array(17, 17)); foreach ($r as $greeting) { echo $greeting, "\n"; }
echo substr_replace('Hello world', 'Goodbye', 0, 5), "\n"; // Change the substring 'Hello' into 'Goodbye' $s = array('How are you, John?', 'How are you, Sheila?'); $greetings = substr_replace($s, 'Where', 0, 3); // Change 'How' into 'Where' in each string foreach ($greetings as $greeting) { echo $greeting, "\n"; } $s = array('Where are you, John?', 'How are you, Sheila?'); // Change 'Where' into 'How' in the first string and 'How' into 'Where' in the second one $greetings = substr_replace($s, array('How', 'Where'), array(0, 0), array(5, 3)); foreach ($greetings as $greeting) { echo $greeting, "\n"; }