How to check if a string starts with a specified string?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to check if a string starts with a specified string?

📝🌐✅ How to Check If a String Starts With a Specified String?

Introduction 💡 Have you ever needed to check if a string begins with a specific substring? Imagine you have a string and you want to know if it starts with "http". Well, you're in the right place! In this guide, I'll show you some easy solutions to this common problem. Let's dive in! 💪

The Challenge 🤔 Recently, I received a question from a reader who was struggling with this issue. They wanted to check if a given string starts with "http". Here's an example to illustrate the problem:

$string1 = 'google.com';
$string2 = 'http://www.google.com';

The reader was wondering how they could determine whether these strings start with "http" or not. Let's take a closer look at the solutions. 🕵️‍♀️

Solution 1: Using the strpos() Function 🌟 One straightforward approach is to use the strpos() function, which searches for the first occurrence of a substring in a string. By finding the position of the substring within the string, we can determine if it's at the beginning.

Here's the code to check if a string starts with a specified substring:

function startsWith($str, $substr) {
    return strpos($str, $substr) === 0;
}

// Usage
echo startsWith($string1, 'http') ? 'Yes' : 'No';  // Output: No
echo startsWith($string2, 'http') ? 'Yes' : 'No';  // Output: Yes

In this example, the startsWith() function compares the position of the substring with 0. If the substring is found at the beginning of the string, the function returns true; otherwise, it returns false.

Solution 2: Using the substr() Function 🌟 Alternatively, we can utilize the substr() function, which extracts a part of a string starting at a specified position.

Here's how you can use the substr() function to check if a string begins with a specific substring:

function startsWith($str, $substr) {
    return substr($str, 0, strlen($substr)) === $substr;
}

// Usage
echo startsWith($string1, 'http') ? 'Yes' : 'No';  // Output: No
echo startsWith($string2, 'http') ? 'Yes' : 'No';  // Output: Yes

In this snippet, the startsWith() function compares the extracted substring from position 0 to the length of the searched substring with the provided substring. If they match, it returns true; otherwise, it returns false.

Time to Take Action! 💪 Now that you know how to check if a string starts with a specific substring, it's time to try it out in your own projects. Apply these techniques to your code and see the magic happen! If you have any questions or face any challenges, feel free to reach out and ask for help. Together, we can conquer any coding hurdles! 🚀

Conclusion 🎉 Checking if a string starts with a specified substring is a common problem that developers encounter. Fortunately, we have explored two easy solutions using the strpos() and substr() functions in PHP. By leveraging these built-in functions, we can effortlessly determine whether a string begins with a specific substring. I hope this guide has been helpful and piqued your curiosity to explore more PHP wonders. Happy coding! 😊

[CTA] 👉 Have you ever faced a tricky string manipulation challenge? Share your experience in the comments below and let's learn from each other! Don't forget to subscribe to our newsletter for more valuable tech tips and tutorials. Keep coding! 🎉🔥

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my