1566548606
Every PHP developer should know functions like strlen()
, addslashes()
and strpos()
. In this Top 10 list you will find the most important ones of the approx. 100 string functions in PHP and learn how to use them correctly.
Let’s start right away with one of my favorite features. This function is interesting e.g. for IDs and strings, which must have a certain length, but don’t necessarily have it. Here is an example to understand it better:
<?php
$example = "706";
// Output: 70600000
echo str_pad($example, 8, "0");
// Output: 00000706
echo str_pad($example, 8, "0", STR_PAD_LEFT);
// Output: 00706000
echo str_pad($example, 8, "0", STR_PAD_BOTH);
// Output: 706-------
echo str_pad($example, 10, "-");
?>
The function expects two parameters: The string to be changed and the expected string length. Optionally, the fill character can be specified as the third parameter. In addition, you can also specify on which side the string is to be filled. STR_PAD_RIGHT
for right (default), STR_PAD_LEFT
for left and STR_PAD_BOTH
for both sides.
Complete documentation: here
If you often work with arrays and JSON data you won’t be able to avoid this function either. It can split your string into an array based on a specific character or whole string. This can be useful, for example, for URL parameters or file names:
<?php
$example = "jquery.min.js";
// split string at the . char
$splitted_string = explode(".", $example);
// Output: Array ( [0] => jquery [1] => min [2] => js )
print_r($splitted_string);
// Output: File Extension: js
echo "File Extension: " . $splitted_string[2];
?>
Optionally, a limit can be specified as the third parameter. This ensures, for example, that only two elements are present in the array when more is not needed.
Complete documentation: here
This function is the opposite of* *explode()
. The function can reassemble an array with a certain separator to a string. You will use this function quite often in web development, because it is used for lists, numbers or SQL queries, for example:
<?php
$example = array("Tony Stark", "Peter Parker", "Bruce Wayne");
// Joins the array with a , char
$imploded_string = implode(", ", $example);
// Output: Top heros: Tony Stark, Peter Parker, Bruce Wayne
echo "Top heros: " . $imploded_string;
?>
Of course you can also program this function yourself within a few minutes. But you don’t always have to reinvent the wheel.
Complete documentation: here
Not to think away. This function is so versatile, e.g. it can be used to validate user entries or before entering them into the database in order not to exceed a certain string length. The function returns the length of a string. Spaces are also counted. Characters are counted. Here is an illustrative example:
<?php
// Output: 12
echo strlen("Hello World!");
// Output: 0
echo strlen("");
// Output: 1
echo strlen(" ");
// Output: 8
echo strlen("WebDEasy");
?>
Complete documentation: here
This function checks whether a character or an entire character string occurs in a string. This function is often used, for example, to evaluate user input. Here are a few good examples:
<?php
// Output: 2
echo strpos("Hello World", "l");
// Output:
echo strpos("Hello World", "H");
// Output: 6
echo strpos("Hello World", "World");
// Check if the string contains 'World'
if(strpos("Hello World", "World")) { }
// Output:
echo strpos("Hello World", "Hello", 2);
?>
In the last example, a third parameter is given. This parameter specifies an offset. This means that the function only checks whether the characters occur in the string from the second character onwards. A negative value in this parameter sets the offset of the string from behind.
Complete documentation: here
Pooh! This means we already have half of the top 10 PHP string functions. Second half:
This function is extremely important from a safety point of view. Ever heard of SQL Injections? Good, because this function can have a part in preventing these attacks. Special characters, here (‘, “, , NUL) are masked here. This means that a backslash () is prefixed so that these characters do not enter the system unfiltered:
<?php
// Output: What\'s your name?
echo addslashes("What's your name?");
// Output: This is \"Me\"!
echo addslashes('This is "Me"!');
// Prevent SQL-Injection
// Output WITHOUT addslashes: INSERT INTO testuser (id, name) VALUES (1, ''; SELECT * FROM testuser;');
// Output WITH addslashes: INSERT INTO testuser (id, name) VALUES (1, '\'; SELECT * FROM testuser;');
$name = "'; SELECT * FROM testuser;";
echo "INSERT INTO testuser (id, name) VALUES (1, '" . addslashes($name) . "');";
?>
The difference in the SQL example is small, but can have devastating consequences!
Stripslashes()
is the reverse function, the documentation can be found here.
Complete documentation: here
This function is known to all programmers because it exists in almost every programming language. This function returns an excerpt from the string:
<?php
// Output: Easy
echo substr("WebDEasy", 4);
// Output: DE
echo substr("WebDEasy", 3, 2);
?>
You can specify the beginning of the extract and of course the length. I don’t think you have to say more about that.
Complete documentation: here
This function can replace a certain occurrence of characters with other characters. This is useful for decimal numbers, for example, where you must distinguish between the German format (with commas) and the English format (with periods). Here is an example:
<?php
// Output: 20,000
echo str_replace(".", ",", "20.000");
// Output: 20.000
echo str_replace(",", ".", "20,000");
// Output: Ths s n str_rplc xmpl n php!
echo str_replace(array("a", "e", "i", "o", "u"), "", "This is an str_replace example in php!");
?>
Arrays can also be passed as replacement parameters. This saves some function calls.
Complete documentation: here
My absolute favorite function. Ok, admittedly not only meant for strings, but I would like to show them anyway. Beside the simple output of “normal” strings you can also output arrays. You can also write strings or arrays into the logs without losing the formatting. The second parameter must be set to true:
<?php
// Output: Hello World!
print_r("Hello World!");
// Output in log files: Hello World
error_log(print_r("Hello World", true));
// Output: Array ( [0] => This [1] => is [2] => an [3] => array )
print_r(array("This", "is", "an", "array"));
?>
Complete documentation: here
This function can output HTML code without tags. Sort of like the str_replace()
function, except that you don’t have to pass each HTML tag individually. This is how it looks like:
<?php
// Output: Hello World!
echo strip_tags("<h1>Hello World!</h1>");
// Output: Hello World!
echo strip_tags("<div class=\"testclass\"><p>Hello <b>World!</b></p></div>");
?>
Complete documentation: here
The most important PHP string functions are now known to you. Which string functions are most important for your everyday life?
Here you can find a complete list of PHP string functions. I’m sure you can now do something with our other PHP posts as well:
Originally published at*** ***webdeasy.de* *on 8. May 2019
#web-development #php
1622201416
One programming language that has its root in Website development even at present is PHP Website Development. The PHP programming is executed on the server side which means it functions on web servers which helps the website in its performance.
Want to develop a website on PHP?
WebClues Infotech with its years of experience in Web Development helps individuals and businesses in launching a business website on PHP. The experienced development team with more than 20 years of experience is the solution to your every web development needs.
Want to know more about PHP website development?
Visit: https://www.webcluesinfotech.com/php-web-development/
Share your requirements https://www.webcluesinfotech.com/contact-us/
View Portfolio https://www.webcluesinfotech.com/portfolio/
#custom php development company #php web development service #php development services #php web development company india #php development services #hire php developers
1617276472
A framework that can drastically cut down the requirement to write original code to develop the web apps as per your requirement is PHP Framework. PHP frameworks offer code libraries for commonly used functions to reduce the development time.
Want to use PHP Web Frameworks for your web applications?
WebClues Infotech offers a service to hire dedicated PHP developers for all of the below-mentioned frameworks
Not sure which framework to use for your PHP web application?
Schedule Interview with PHP Developer https://bit.ly/3dsTWf0
Email: sales@webcluesinfotech.com
#hire php developer #hire php web developers #hire php developer in 2021 #hire php developers & dedicated php programmers #hire php developers india #hire and outsource freelance php developers
1624863764
Choose PixelCrayons as your preferred PHP web development company and get secure, scalable and advanced PHP based web applications. PixelCrayons is one of the best PHP development companies which has dedicated php developers who have expertise in the latest version of PHP frameworks like Laravel, Symfony, Yii, CakePHP, etc. Our PHP web development services are top-class.
Agile/DevOps approach for on time delivery
Save upto 60% on overall php development project
PHP website development company
#php development company #outsource php development #php application development #php web development companies #php web development #php development india
1624337661
PHP is the best language to choose for developing a web app. PHP is especially used for web app development. A web app is very important from the business point of view as it helps to improve the business and also increases the number of customers.
If you also want to develop a web app for your own business then you need to select a PHP development company. Nevina Infotech is one of the best PHP development companies that can help you to develop your web app with the help of its dedicated developers.
#php website development services #php web development company in india #custom php web development services #php website development #php development services #php development company in usa
1594034194
PHP is a general-purpose, server-side scripting language most commonly used for Website and Web Application Development. PHP as a web development option is secure, fast, and reliable that offers lots more advantages to make it accessible to a lot of people.
Is this something you are looking for? Contact Skenix Infotech now to know more about our PHP Development Services & pricing.
#php development company in india #php web development #php development services #hire php developers #php development #web development