Check If Variable Starts with 'Http'

Check if variable starts with 'http'

if (strpos($source, 'http') === 0) {
$source = "<a href=\"$source\">$source</a>";
}

Note I use ===, not == because strpos returns boolean false if the string does not contain the match. Zero is falsey in PHP, so a strict equality check is necessary to remove ambiguity.

Reference:

http://php.net/strpos

http://php.net/operators.comparison

Check if a string starts with http using Javascript

export const getValidUrl = (url = "") => {
let newUrl = window.decodeURIComponent(url);
newUrl = newUrl.trim().replace(/\s/g, "");

if(/^(:\/\/)/.test(newUrl)){
return `http${newUrl}`;
}
if(!/^(f|ht)tps?:\/\//i.test(newUrl)){
return `http://${newUrl}`;
}

return newUrl;
};

Tests:

expect(getValidUrl('https://www.test.com')).toBe('https://www.test.com');
expect(getValidUrl('http://www.test.com')).toBe('http://www.test.com');
expect(getValidUrl(' http : / / www.test.com')).toBe('http://www.test.com');
expect(getValidUrl('ftp://www.test.com')).toBe('ftp://www.test.com');
expect(getValidUrl('www.test.com')).toBe('http://www.test.com');
expect(getValidUrl('://www.test.com')).toBe('http://www.test.com');
expect(getValidUrl('http%3A%2F%2Fwww.test.com')).toBe('http://www.test.com');
expect(getValidUrl('www . test.com')).toBe('http://www.test.com');

Regex to test if string begins with http:// or https://

Your use of [] is incorrect -- note that [] denotes a character class and will therefore only ever match one character. The expression [(http)(https)] translates to "match a (, an h, a t, a t, a p, a ), or an s." (Duplicate characters are ignored.)

Try this:

^https?://

If you really want to use alternation, use this syntax instead:

^(http|https)://

How to check if a string contains http:// at the start

If you don't need a regex to do this (depending on what language you're using), you could simply look at the initial characters of your string. For example:

// C#
if (!str.StartsWith("http://"))
str = "http://" + str;

// Java
if (!str.startsWith("http://"))
str = "http://" + str;

// JavaScript/TypeScript
if (str.substring(0, 7) !== 'http://')
str = 'http://' + str;

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

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

PHP how to check if variable starts with 'pa_' or not?

Use strpos or access chars in a string by using an offset value:

 $paName = $product_attribute['name'];
if($paName[0] . $paName[1] . $paName[2] == $test_str) {
$pa_array[]= $paName;
}

Check if string begins with something?

Use stringObject.substring

if (pathname.substring(0, 6) == "/sub/1") {
// ...
}

verify, if the string starts with given substring

You should check with the identity operator (===), see the documentation.

Your test becomes:

if (strpos($str, 'http://') === 0) echo 'yes';

As @Samuel Gfeller pointed out: As of PHP8 you can use the str_starts_with() method. You can use it like this:

if (str_starts_with($str, 'http://')) echo 'yes';

Javascript: check if an url is valid and starts with http// or https://

Just change ^(https?:\\/\\/)?' to ^(https?:\\/\\/)', the ? means that you want to match zero or one occurnece of a pattern. You actually want exactly one occurence, so don't use ? :)

From regular-expressions:

? - Makes the preceding item optional. Greedy, so the optional item is included in the match if possible.



Related Topics



Leave a reply



Submit