The function str_replace() (as defined in PHP manual) – “replace all occurrences of the search string with the replacement string.
What this means if you simply need to replace a character in a variable or a word in a phrase you should use this function. (If you need to use regular expressions to find a pattern match then use preg_replace() instead).
$text = 'I like apples.';
$new_text = str_replace('like', 'hate', $text);
echo $new_text; // will print 'I hate apples.'
As simple as that.
It seems easy enough to use the <br /> tag in HTML to output your text on a new line. In PHP, many of you know that to move your cursor to a new line you can use the “backslash r” escape sequence.
And still many of us still make mistakes by using single quotes around this sequence. To make this work use DOUBLE quotes around “\r” which provides escape sequence and moves the cursor to a new line.
This blog will be dedicated to PHP functions, problems and solutions. As I build online web applications I get stuck using some of the functions or simply can’t figure out a work around some problems. Given that I like problem solving I spend hours (or in some cases days) trying to come up with a solution. My first approach to problem solving is to find a working solution and then optimize the same solution to reduce code and increase performance.
So, here I will be posting various problems that I come across while writing code or learning a new framework. Please follow my blog and you’ll notice I try to be concise and persistent with my work.
Some of my previous PHP work can be found here: http://pc-technic.blogspot.ca/
If you have any questions or comments please write them and I’ll be glad to answer them.