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 http is in the string?
You can use the URL webApi.
https://developer.mozilla.org/en-US/docs/Web/API/URL/URL
If the string passed to the constructor is not a valid URL format, it will throw an error that you can catch.
What's the best way to check if a String contains a URL in Java/Android?
Best way would be to use regular expression, something like below:
public static final String URL_REGEX = "^((https?|ftp)://|(www|ftp)\\.)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";
Pattern p = Pattern.compile(URL_REGEX);
Matcher m = p.matcher("example.com");//replace with string to compare
if(m.find()) {
System.out.println("String contains URL");
}
Regex to check if http or https exists in the string
From the looks of it, you're just checking if http or https exists in the string. Regular expressions are a bit overkill for that purpose. Try this simple code using indexOf
:
function validateText(str)
{
var tarea = str;
if (tarea.indexOf("http://") == 0 || tarea.indexOf("https://") == 0) {
// do something here
}
}
PHP validate string to ensure it does not contain http:// or https:// or www
Here's the regex #^(https?://|www\.)#i
, and here's a way to test for future URLs (command line, change \n
to <br>
if testing in a browser).
<?php
$urls = array('http://www.website.com', 'https://www.website.com', 'http://website.com', 'https://website.com', 'www.website.com', 'website.com');
foreach($urls as $url) {
if (preg_match('#^(https?://|www\.)#i', $url) === 1){
echo $url . ' matches' . "\n";
} else {
echo $url . ' fails' . "\n";
}
}
Output:http://www.website.com matches
https://www.website.com matches
http://website.com matches
https://website.com matches
www.website.com matches
website.com fails
How to Check if User entered text contains http:// in Javascript
Try this:
var field = document.getElementById('tinput');
if (!/^https?:\/\//.test(field.value)) field.value = 'http://'+field.value;
A couple of points to note:your current check allows for
http(s)://
to be anywhere in the string - you should check for it at the start onlyin the interests of code readability, and because the only value under zero that
indexOf()
can return is -1, it's better to check for that explicitly, rather than< 0
you're creating a global variable (
b
) for no reasondon't escape - as xdevel said in his comment, this will mean you check for an encoded version of the string, not the string itself
Check string containing URL for http://
NSString responds to rangeOfString:
, not rangeWithString:
.
The variable urlAddress is declared both in the if
statement, and in the else
statement. That means it only lives in that scope. Once you leave the if/else statement, the variable is gone.
For a URL it's best if it begins with the scheme (like "http://"), and your code will gladly accept apple.http://.com as being valid.
You can use the hasPrefix:
method instead, like this:
BOOL result = [[check lowercaseString] hasPrefix:@"http://"];
NSURL *urlAddress = nil;
if (result) {
urlAddress = [NSURL URLWithString: textField.text];
}
else {
NSString *good = [NSString stringWithFormat:@"http://%@", [textField text]];
urlAddress = [NSURL URLWithString: good];
}
NSURLRequest *requestObject = [NSURLRequest requestWithURL:urlAddress];
PHP: check if string contain the same word more than once
Well, since if your string contains "https", then it also contains "http", you can directly count the occurrences of "http", for example by using the function substr_count:
if(substr_count($your_string, "http") > 1) {
// do something
}
else {
// do something else
}
Related Topics
How to Check Is Timezone Identifier Valid from Code
Laravel 5 Auth Post Submit - Tokenmismatchexception in Verifycsrftoken.PHP Line 46
Jquery:: Ajax Powered Progress Bar
Find Windows 32 or 64 Bit Using PHP
Pdo_Sqlite Driver Not Present.. What to Do
Image Upload from iPhone Strips Exif Data
PHP Readfile() Causing Corrupt File Downloads
Remove Extra Spaces But Not Space Between Two Words
Encoding Byte Data into Digits
How to Call a Static Method on a Variable Class
How to Reference a Column Other Than 'Id' for a Joincolumn
Howto Generate JSON with Smarty
How to Get Content of Remote HTML Page
Prevent Back Button After Logout
File_Get_Contents(): Stream Does Not Support Seeking/When Was PHP Behavior About This Changed
Regular Expression for French Characters
How to Access Magento Customer's Session from Outside Magento