How can i check if a string starts with a specific word in php?

I'm trying to check if a string starts with http. How can I do this check?

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

How can i check if a string starts with a specific word in php?

asked May 7, 2010 at 18:43

4

PHP 8 or newer:

Use the str_starts_with function:

str_starts_with('http://www.google.com', 'http')

PHP 7 or older:

Use the substr function to return a part of a string.

substr( $string_n, 0, 4 ) === "http"

If you're trying to make sure it's not another protocol. I'd use http:// instead, since https would also match, and other things such as http-protocol.com.

substr( $string_n, 0, 7 ) === "http://"

And in general:

substr($string, 0, strlen($query)) === $query

How can i check if a string starts with a specific word in php?

Pikamander2

6,3833 gold badges42 silver badges63 bronze badges

answered May 7, 2010 at 18:46

Kendall HopkinsKendall Hopkins

41.9k17 gold badges65 silver badges87 bronze badges

10

Use strpos():

if (strpos($string2, 'http') === 0) {
   // It starts with 'http'
}

Remember the three equals signs (===). It will not work properly if you only use two. This is because strpos() will return false if the needle cannot be found in the haystack.

How can i check if a string starts with a specific word in php?

CodeZombie

5,2673 gold badges28 silver badges37 bronze badges

answered May 7, 2010 at 18:45

awgyawgy

16.2k4 gold badges24 silver badges18 bronze badges

14

There is also the strncmp() function and strncasecmp() function which is perfect for this situation:

if (strncmp($string_n, "http", 4) === 0)

In general:

if (strncmp($string_n, $prefix, strlen($prefix)) === 0)

The advantage over the substr() approach is that strncmp() just does what needs to be done, without creating a temporary string.

dave

2,6461 gold badge14 silver badges32 bronze badges

answered May 7, 2010 at 23:26

SidSid

2,0961 gold badge11 silver badges10 bronze badges

4

You can use a simple regex (updated version from user viriathus as eregi is deprecated)

if (preg_match('#^http#', $url) === 1) {
    // Starts with http (case sensitive).
}

or if you want a case insensitive search

if (preg_match('#^http#i', $url) === 1) {
    // Starts with http (case insensitive).
}

Regexes allow to perform more complex tasks

if (preg_match('#^https?://#i', $url) === 1) {
    // Starts with http:// or https:// (case insensitive).
}

Performance wise, you don't need to create a new string (unlike with substr) nor parse the whole string if it doesn't start with what you want. You will have a performance penalty though the 1st time you use the regex (you need to create/compile it).

This extension maintains a global per-thread cache of compiled regular expressions (up to 4096). http://www.php.net/manual/en/intro.pcre.php

answered Dec 6, 2013 at 8:25

user276648user276648

5,7796 gold badges59 silver badges83 bronze badges

8

You can check if your string starts with http or https using the small function below.

function has_prefix($string, $prefix) {
   return substr($string, 0, strlen($prefix)) == $prefix;
}

$url   = 'http://www.google.com';
echo 'the url ' . (has_prefix($url, 'http://')  ? 'does' : 'does not') . ' start with http://';
echo 'the url ' . (has_prefix($url, 'https://') ? 'does' : 'does not') . ' start with https://';

Salim

2,2471 gold badge12 silver badges12 bronze badges

answered Jun 19, 2013 at 14:01

How can i check if a string starts with a specific word in php?

TURTLETURTLE

3,5794 gold badges47 silver badges48 bronze badges

1

How do you check if a string starts with a particular word?

We can use the startsWith() method of String class to check whether a string begins with a specific string or not, it returns a boolean, true or false.

How do I check if a string starts with a specific character in PHP?

To check if string starts with a specific character, use PHP built-in function strpos(). Provide the string and character as arguments to strpos(), and if strpos() returns 0, then we can confirm that the string starts with the specific character, else not.

How do you check the start of a string?

The startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

How do I get the first letter of a word in PHP?

So you can easily use $i[0] to fetch first letter.