Basic functions for working with string in PHP. Example of u... open

Basic functions for working with string in PHP. Example of use

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

Examples of strings:

$str = ' Hello, world! ';
$str2 = 'Hello, world!';
$str3 = "<script>alert('©Hello &gt;' + \" world\")</script>";
$str4 = "Hi!\nIn first...";

1. strlen — returns the length of the string:

strlen($str);//will return the number of bytes, not the number of characters in the string.

2. explode — split a string using a delimiter:

explode(',', $str2);//the first parameter is a delimiter and the second is a string
//returns an array of strings,

3. implode — concatenates the elements of an array into a string:

implode(',', $data);//creates an array with strings where each element of the array is separated by ","
//returns string

4. htmlspecialchars — Convert special characters to HTML entities:

htmlspecialchars($str3, ENT_QUOTES, double_encode: false);//read a documentation - https://www.php.net/manual/ru/function.htmlspecialchars.php

5. ltrim – remove spaces (or other characters) from the beginning of a string

ltrim($str);return string without charecters on beginning

6. rtrim – remove spaces (or other characters) from the end of a string

rtrim($str);return string without charecters on the end

7. trim — removes spaces (or other characters) from the beginning and end of a string

trim($str, 'H ');//delete all "H" characters from the string, and if 2 is not specified, remove spaces (or another characters) from the beginning and end of the string.

8. nl2br — inserts an HTML line break code before each newline

nl2br($str4);//Returns a string with a <br /> or <br> inserted before each newline (\r\n, \n\r, \n, and \r).

9. str_split — converts a string to an array

str_split($str));//return the array for each element and the array for the symbols for the string
//In 2-th parameter we can specify the length of each element of the array

10. strpos – Returns the position of the first occurrence of a substring

strpos('abc', 'a');//returns 0. If the search string is not found, returns false

11. stripos – returns the position of the first occurrence of a substring, case insensitive

stripos('abc', 'a');//used to find the first occurrence of a substring in a string, case insensitive

12. strtolower – converts a string to lowercase

strtolower($str);//returns the string with all lowercase characters

13. strtoupper – сonverts a string to uppercase

strtoupper($str);//returns the string with all uppercase characters

14. substr– returns a substring

substr($str, -3, 1);//substr(string $string, int $offset, ?int $length = null)://returns a substring of string starting at offset character count and length characters long.
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation