| There are instances where you need to manipulate | | | | <?php |
| the string either it was inputted or taken from the | | | | $string = “This tutorial is very useful for |
| database. This tutorial will help you find out what | | | | newbies”; |
| string manipulation function you can use. | | | | $newstring = str_replace(”useful”, |
| What is a String? | | | | “difficult”, $string );echo $newstring; |
| In PHP, and every other flavor of Web programming, | | | | ?> |
| a string is a variable contained between quotes with | | | | The str_replace() function takes 3 parameters. The |
| a literal value. For example, $number = “2” is | | | | first parameter is the piece of text you want to |
| a string variable, whereas $number = 2 is not. The | | | | replace. The second is the text that will replace the |
| first value is seen as text only, the latter as a | | | | original. The third parameter tells PHP what value to |
| numeric value. Simply put, a string is a text variable. | | | | do the find and replace on. |
| 1. ucwords - this is used to convert a phrase or | | | | 4. strpos – find position of first occurrence of a |
| sentence into a title format. | | | | string |
| <?php | | | | Syntax: int strpos ( string $haystack |
| $string =”string manipulation in php”;echo | | | | , mixed $needle [, int $offset = 0 ] ) |
| ucwords($string); | | | | <?php |
| ?> | | | | $string = “Hello World”;echo “The word |
| 2. strtoupper – this is used to capitalize every | | | | World is at position number: |
| letter in a string. | | | | “.strpos($string,”World”); |
| <?php | | | | ?> |
| $string =”string manipulation in php”;echo | | | | 5. substr – Return part of a string |
| strtoupper($string); | | | | Syntax: string substr ( string $string , int $start |
| ?> | | | | [, int $length ] ) |
| 2. strtolower – this is used to bring all letters in a | | | | <?php |
| string to lowercase. | | | | $string = “Hello World”; |
| <?php | | | | $substring = substr($string,6);echo $substring; |
| $string =”String Manipulation in PHP”;echo | | | | ?> |
| strtolower($string); | | | | The example above will display the word |
| ?> | | | | “World”. |
| 3. str_replace – this function is used to replace | | | | I will post more of those functions in my next |
| a certain substring in a string | | | | tutorial. |