PHP String Functions: It saves time
Have you ever found yourself knee-deep in a coding project, only to bump into a code (either yours or someone else's) that is doing things the hardest way possible? That was me a few weeks ago. I was working on the user registration part of a web application that was a mess. With a little bit of refactoring and a lot of PHP string functions, everything became so much cleaner. Let's dive in.
I inherited a project where users would register, and I needed to process their input: check the length of usernames, convert names to proper cases, trim unwanted spaces, and replace certain characters. I removed some of the code and change it a little bit to fit the example but basically looked something like this:
<?php
function processUserInput($username, $fullname, $bio) {
// Check username length
$usernameLength = 0;
for ($i = 0; isset($username[$i]); $i++) {
$usernameLength++;
}
if ($usernameLength < 5 || $usernameLength > 15) {
return "Username must be between 5 and 15 characters.";
}
// Convert full name to title case
$fullnameArray = explode(" ", $fullname);
for ($i = 0; $i < count($fullnameArray); $i++) {
$fullnameArray[$i][0] = strtoupper($fullnameArray[$i][0]);
for ($j = 1; isset($fullnameArray[$i][$j]); $j++) {
$fullnameArray[$i][$j] = strtolower($fullnameArray[$i][$j]);
}
}
$properFullname = implode(" ", $fullnameArray);
// Trim spaces from bio
$bio = rtrim(ltrim($bio));
// Replace certain words in bio
$bioArray = explode(" ", $bio);
for ($i = 0; $i < count($bioArray); $i++) {
if ($bioArray[$i] == "badword") {
$bioArray[$i] = "***";
}
}
$cleanBio = implode(" ", $bioArray);
return "Username: $username\nFull Name: $properFullname\nBio: $cleanBio";
}
?>
This type of code is very common in projects that I take and I think is because people don't know much about php functions. PHP is constantly adjusting these stuff for us. Maybe I'll do a ebook about string functions in the fuuture. Who knows?
This code worked, but it was cumbersome, error-prone, and not very efficient. It's like trying to build a rocket ship with a butter knife. The funny part was that each part of the code could be replaced by a PHP String function.
<?php
function processUserInput($username, $fullname, $bio) {
// Check username length using strlen()
if (strlen($username) < 5 || strlen($username) > 15) {
return "Username must be between 5 and 15 characters.";
}
// Convert full name to title case using ucwords()
$properFullname = ucwords(strtolower($fullname));
// Trim spaces from bio using trim()
$bio = trim($bio);
// Replace certain words in bio using str_replace()
$cleanBio = str_replace("badword", "***", $bio);
return "Username: $username\nFull Name: $properFullname\nBio: $cleanBio";
}
?>
Refactoring someone else's code using PHP string functions transformed it from a convoluted mess into a sleek, efficient piece of software. Not only did this improve readability and maintainability, but it also saved me a ton of time and headaches. So, if you ever find yourself in a stringy mess, remember: PHP string functions are here to save the day. Whether you're here to learn, laugh, or just see what random topic I’ll rant about next, I'm glad you stopped by.
Edie