PHP String Functions

1. str_repeat() - Repeat a string

string str_repeat(string $input, int $multiplier)

Returns input repeated multiplier times.

Parametes:

input: The string to be repeated.

multiplier: Number of time the input string should be repeated. multiplier has to be greater than or equal to 0.
Note: If the multiplier is set to 0, the function will return an empty string.

<?php 
  echo str_repeat('-', 5);  // Output -----
?>

2. str_replace() - Replace all occurrences of the search string with the replacement string

mixed str_replace($search, $replace, $subject, $count)

Returns input repeated multiplier times.

Parametes:

search: Specifies the value to find - (required)

replace: Specifies the value to replace - (required)

subject: Specifies the string to be searched - (required)

count: Count the number of replacements - (optional)

<?php 
  echo str_replace("John", "Peter", "Hello John");  // Output Hello Peter
?>

3. strtolower() - Convert a string lowercase

string strtolower($string)

Returns string with all alphabetic characters converted to lowercase. .

Parametes:

string: The input string.

<?php 
  echo strtolower("Lorem Ipsum Dolor");  // Output lorem ipsum dolor
?>

4. strtoupper() - Convert a string uppercase

string strtoupper($string)

Returns string with all alphabetic characters converted to uppercase. .

Parametes:

string: The input string.

<?php 
  echo strtoupper("Lorem Ipsum Dolor");  // Output LOREM IPSUM DOLOR
?>

5. ucwords() - Convert uppercase the first character of each word in a string

mixed ucwords($string, $delimiters)

Returns string with all alphabetic characters converted to uppercase. .

Parametes:

string: The input string. (required)

count: The optional delimiters contains the word separator characters. (optional)

<?php 
  echo ucwords("lorem ipsum dolor");  // Output Lorem Ipsum Dolor
?>

6. strlen() - Get string length

int strlen($string)

Returns the length of the given string.

Parametes:

string: The string being measured for length.

<?php 
  echo strlen("lorem ipsum dolor");  // Output 17
?>

7. strrev() - Reverse a string

string strrev($string)

Returns string, reversed.

Parametes:

string: The string to be reversed.

<?php 
  echo strrev("Hello Peter");  // Output reteP olleH
?>

8. strstr() - Find the first occurrence of a string

string strstr($string, $needle, $before_needle = false)

Returns the portion of string, or FALSE if needle is not found.

Parametes:

string: The input string.

needle: If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. .

before_needle: If TRUE, strstr() returns the part of the haystack before the first occurrence of the needle (excluding the needle).

<?php 
  echo strstr('support@example.com', '@'); // prints @example.com
?>
<?php 
  echo strstr('support@example.com', '@', true); // prints support
?>

9. str_shuffle() - Randomly shuffles a string

string str_shuffle(string $string)

Returns the portion of string specified by the start and length parameters.

Parametes:

string: The input string.

<?php 
  echo str_shuffle("Hello");  // This will echo something like: elHol
?>

10. substr() - Return part of a string

string strstr(string $string, int $start, int $length)

Returns the portion of string specified by the start and length parameters.

Parametes:

string: The input string. Must be one character or longer.

start: If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'lorem ipsum dolor', the character at position 0 is 'l', the character at position 2 is 'r', and so forth.

If start is negative, the returned string will start at the start'th character from the end of string.

If string is less than start characters long, FALSE will be returned.

length: If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).

If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes the position of this truncation or beyond, FALSE will be returned.

If length is given and is 0, FALSE or NULL, an empty string will be returned.

If length is omitted, the substring starting from start until the end of the string will be returned.

<?php 
  echo substr("lorem ipsum dolor", -1);  // Output r
  echo substr("lorem ipsum dolor", -2);  // Output or
  echo substr("lorem ipsum dolor", -3, 1);  // Output l
  echo substr("lorem ipsum dolor", 0, -1);  // Output lorem ipsum dolo
  echo substr("lorem ipsum dolor", 2, -1);  // Output rem ipsum dolo
?>