WarDrome Blog

PHP Guide: handling strings

PHP Help: Handling Strings

Hi, here we are again after a period of inactivity with a new guide. Today we will discuss string manipulation in PHP.

PHP provides us some standard features to cut a string, counting the characters, use regular expressions, replace and so on.

strlen ()

Strlen (as you can well imagine) allows us to count the length of a string, ie the number of characters in a string.


 $string = "My string";
echo strlen($string); // will produce the result "8" 

trim ($string, [string $chars])

Trim will remove the spaces before and after a given string.

 $string = " My string ";
echo trim($string); // will print "My string" (without whitespaces) 

Specifying the second parameter of the function trim() you can specify the character to be deleted. For example:

 $string = "\tMy string\t";
echo trim ($ string, "\ t") / / will print "My string" (without \t) 

ltrim($string[string $chars])

Ltrim function is similar to trim() (and also supports the second parameter to specify the character to be removed), except that it only removes whitespaces at the beginning of the string (and not the end).

 $string = " My string ";
echo ltrim($string); // will print "My string " (note the whitespace at the end that is not stripped) 

rtrim($string [string $chars])

Similar to the two mentioned above, except that it deletes the whitespace at the end of the string.

substr(string string, int $start, [int $end])

Substr returns a portion of the string according to a beginning (defined by the second parameter) and a late (third parameter) predetermined, usually used in conjunction with the strpos(). Let’s see an example:

$string = "My search string";
echo substr ($string, 0, 8); // will print "My string" 

Alternatively, if you do not know the final position of the string, you can use the strlen() to determine the length of the string and subtract the number of characters you want to delete.

$string = "My search string";
echo substr($string, 0, strlen ($string)-7) // will print "My search" 

str_replace(mixed $search, mixed $replace, mixed $subject [, int &$count])

str_replace allows us to search within a string and replace with a selected portion of our interest.

$string = "My usual string";
echo str_replace ("usual", "simple", $ string); // will produce results in "My simple string"

strpos(string $haystack, mixed $needle [, int $offset = 0])

Strpos (as mentioned above) will help us to find the location of the string provided in the second parameter ($needle) in the first ($haystack).
Please note that The count starts at 0

$string = "My string";
echo strpos ($string, "my") / / will produce the result: 1 

strstr(string $haystack, mixed $needle [, bool $before_needle = false])

strstr allows us to perform a search using the criteria provided in the second parameter ($needle) in the first parameter ($haystack)

 $string = "My string";
echo strstr($string, "st") // will produce the result "ring" 

strtolower (string $string)

Strtolower allows us to convert all characters in the first parameter provided in lowercase. For example:

 $string = "MY STRING";
echo strtolower($string); // will produce the result "my string" 

strtoupper(string $string)

Similar to the function strtolower() with the difference that converts all characters to uppercase

ucfirst (string $string)

Ucfirst function converts the first character of the string to uppercase

$string = "my string";
echo ucfirst ($string); // will produce "My string" 

ucwords(string $string)

Ucwords function, similar to ucfirst, capitalize every first letter of each word. For example:

$string = "my string";
echo ucwords($string); // will produce "My String" 

explode(string $separator, string $string [, int $ count]

The explode function allows us to transform the supplied string in the second parameter in the array with the separator provided in the first. For example:

$string = "La,my,string";
print_r (explode (",",$string) // This will produce an array of 3 strings, namely: La, My and String 

Note that the third parameter of the function function is to provide the maximum number of elements that the array must contain.

We have finished our little lesson, in the next tutorial we’ll discuss about how to handle arrays, sort, remove items, add items and so on..see you !

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Optionally add an image (JPEG only)

Cookies help us deliver our services. By using our services, you agree to our use of cookies. More Info | Close