Strings bend to your will with PHP’s dynamic functions
This article is all about string functions. These are simple built-in functions that require little to grasp. They’re conveniently named to simplify what the operations will accomplish. After we finish string functions, we’ll move on to array functions, which are just as simple.
trim(),
ltrim()
,rtrim()
htmlspecialchars()
__call()
preg_match()
filter_var()
addslashes()
str_replace()
strlen()
strtolower()
strtoupper()
ucfirst()
strpos()
,stripos()
,strrpos()
,strripos()
- Array Functions like:
array_chunk()
,array_diff()
,array_key_exists()
,array_key_first()
,array_key_last()
,array_map()
,array_merge()
,array_push()
,array_sum()
,asort()
,arsort()
,count()
,in_array()
,ksort()
,krsort()
,sort()
,rsort()
,shuffle()
,sizeof()
,is_array()
,explode()
,implode()
- Magic Methods like:
__invoke()
,__toString()
Addslashes()
Starting off this list is the addslashes
function. It will look at a string and add backslashes to the following characters:
- single quote (
'
) - double quote (
"
) - backslash (
\
) - NUL (the NUL byte)
These are the characters that need to be escaped.
<?php
// AddSlashes
var_dump( addslashes("Hello there. What's up?") );
var_dump( addslashes('She said, "I am not interested."') );
var_dump( addslashes('c:\users\dino"') );
/app/98 Functions/index.php:24:string 'Hello there. What\'s up?' (length=24)
/app/98 Functions/index.php:25:string 'She said, \"I am not interested.\"' (length=34)
/app/98 Functions/index.php:26:string 'c:\\users\\dino\"' (length=17)
Not much more to the addslashes
function.
str_replace()
This is a frequently used function. It searches for a particular value in the string and replaces it with something else.
str_replace(
array|string $search,
array|string $replace,
string|array $subject,
int &$count = null
): string|array
The $search
and $replace
arguments can be strings or arrays. Let’s take a look at a couple of examples.
<?php
var_dump( str_replace("Hello", "Hey", "Hello, what's going on?") );
The code above searches for Hello
and replaces it with Hey
.
/app/98 Functions/index.php:29:string 'Hey, what's going on?' (length=21)
Let’s look at an example with one array.
<?php
$search = ["Hello", "Hey", "Hi"];
var_dump( str_replace($search, "Good Morning", "Hey There. How are you? I said Hello.") );
We’re searching for the Hello
, Hey
and Hi
. If any of those searches match, replace them with Good Morning
.
/app/98 Functions/index.php:32:string 'Good Morning There. How are you? I said Good Morning.' (length=53)
We can also do a one-to-one array replacement. What I mean by that is that you have two arrays. Index 0 in the search array will replace index 0 in the replace array.
<?php
$search = ["Hello", "Hey", "Hi"];
$replace = ["Good Morning", "Good Day", "Good Evening"];
var_dump( str_replace($search, $replace, "Hey There. How are you? I said Hello.") );
<?php
/app/98 Functions/index.php:36:string 'Good Day There. How are you? I said Good Morning.' (length=49)
strlen()
The str_len
function just counts the length of the string. Remember that this isn’t an array, so if you have one character, the length will be 1. I’m not sure why that gets frequently mixed up.
<?php
var_dump( strlen("Dino") ); // 4
strtolower()
The strtolower
function converts a string to lowercase characters.
<?php
// strtolower
var_dump( strtolower("Dino") ); //dino
var_dump( strtolower("DINO") ); //dino
strtoupper()
It’s the opposite of strtolower
. It just converts everything to uppercase characters.
<?php
// strtoupper
var_dump( strtoupper("Dino") ); //DINO
var_dump( strtoupper("dino") ); //DINO
ucfirst()
Converts the first character in a string to an uppercase character. One frequent error is that there’s an assumption that the start of each sentence is converted to an uppercase character. That’s not true. Only the first first character in the string is capitalized.
<?php
// ucfirst
var_dump( ucfirst("hey there. how's it going?") );
/app/98 Functions/index.php:50:string 'Hey there. how's it going?' (length=26)
strpos(), stripos(), strrpos(), strripos()
The last bit of string functions that are commonly used in PHP. The strpos
function is a favorite in interviews. strpos
finds the position of the first occurrence of a substring in a string. It’ll return the index value of the substring or false
.
<?php
// strpos
var_dump( strpos("Hi there.", "Hi") ); // 0
var_dump( strpos("Hi there.", "there") ); // 3
var_dump( strpos("Hi there.", "ere") ); // 5
var_dump( strpos("Hi there.", "frank") ); // false
The needle Hi
is found at index 0, so it returns 0
. If you remember, value 0
is falsy. The proper way to check if a substring exists is with the identity operator. Let’s look at the wrong way first.
<?php
if ( strpos("Hi there", "Hi") != false ) {
echo "Found a match";
} else {
echo "Didn't find a match";
}
In this instance, we get Didn’t find a match
even though we did. Remember, index 0 is returned and it’s falsy. Now, let’s look at the right way to do this.
<?php
if ( strpos("Hi there", "Hi") !== false ) {
echo "Found a match";
} else {
echo "Didn't find a match";
}
With the identity operator, !==
, the function must return the boolean false
value in order for it to fail.
The strpos
is case sensitive.
<?php
var_dump( strpos("Hi there.", "hi") );
The example above returns false
. To keep it case-insensitive, use stripos
.
<?php
var_dump( stripos("Hi there.", "hi") );
This time, we get 0
.
strrpos
searches for the last occurrence of a substring in a string.
<?php
var_dump( strrpos("Hi there. Hi.", "Hi") ); // Returns 10 instead of 0.
It is case sensitive. If you want for it to be case-insensitive, you can use strripos
.
<?php
var_dump( strrpos("Hi there. Hi.", "hi") ); // false
var_dump( strripos("Hi there. Hi.", "hi") ); // 10
And that’s it for string functions. Next up, array functions.
https://github.com/dinocajic/php-youtube-tutorials?source=post_page—–acb8bbeae832——————————–
EFFICIENT FILTERING AND VALIDATION
PHP – P103: BUILT IN FUNCTIONS FILTER_VAR
PHP’s filter_var simplifies data validation by efficiently filtering and validating user inputs against predefined or custom filters in PHP.
Strings bend to your will with PHP’s dynamic functions
These are simple built-in functions that require little to grasp. They’re conveniently named to simplify what the operations will accomplish.
NAVIGATING ARRAYS IS AN ART, AND PHP’S FUNCTIONS ARE YOUR PAINTBRUSHES
Array functions in PHP are like a versatile toolkit, offering an array of tools to slice, dice, rearrange, and transform data arrays effortlessly, allowing developers to compose intricate data symphonies with precision and elegance.