Welcome to PHPMule.com
A blog site which I am posting to during my learning of the popular programming language PHP. Feel free to leave comments!
-
PHP - Replacing Strings With Other Strings
This is a very useful function to include in web forms or anywhere where user’s can leave input. What this function does is replace strings with strings for example, replacing swear words with something else such as ‘****’. This can be easily done in PHP by using the following format:
str_replace(findwhat, replacewith, whichstring)
If we used this format we could produce the following:
$modifiedcomments = str_replace(”water”, ‘fire’, $comments);
echo $modifiedcomments;This will replace water with fire in the variable $comments. So when the script get’s processed a new variable called $modifiedcomments will be created and if it finds the word or string ‘water’ it will be replaced with ‘fire’.
(Post created on Wednesday, April 15th 09 at 21:55)
-
PHP - Finding Strings Within Strings
There are four functions you can use to find strings:
- strstr()
- strchr()
- strrchr()
- stristr()
The first function: strstr() can be used to find a string inside another or longer string for example:
if (strstr($string, ‘computer’))
echo “The word computer is found”;
This function is basically testing out whether or not the string ‘computer’ is in the variable $string. If the string is in variable $string it would output “The word computer is found” to the browser.
The second function we could use is strchr(). This function is the nearly similiar to the first function but this one finds a specific character such as ‘@’ or any other character.
The third function: strrchr() finds the last occurence of a character or string and then returns all the characters from that point onward. The last function is the stristr() function. This is the same as the strstr() function but it’s not case sensitive so you could find the string whether or not it’s in capitals or not (or both caps and not caps)
(Post created on Wednesday, April 15th 09 at 15:45)
-
Archives
Recent Articles
- PHP - Rounding Off Numbers
- PHP - Single vs Double Quotation Marks
- PHP - Loading Files Into Your PHP Script By Using require() and include()
- PHP - Replacing Strings With Other Strings
- PHP - Finding Strings Within Strings
- PHP - Using strlen() To Test String Length
- PHP - Using The explode() Function
- PHP - Changing The String Case
- PHP - The nl2br() Function
- PHP - Trimming Strings
Links
Categories
- PHP (22)
- Uncategorized (1)


