DEV Community

Sh Raj
Sh Raj

Posted on

1 1 1 1 1

Check String is a Link or Not - in Different Programming Languages

To check whether a string is a valid link or URL in PHP, you can use regular expressions and the preg_match() function. Here's an example:

function isLink($string) {
    $pattern = '/^(https?:\/\/)?([\w\d\-]+\.)+[\w\d\-]+(\/[\w\d\-./?=#%&]*)?$/i';
    return preg_match($pattern, $string);
}

// Usage:
$string1 = "https://example.com";
$string2 = "www.example.com";
$string3 = "example.com";
$string4 = "not a link";

var_dump(isLink($string1));  // true
var_dump(isLink($string2));  // true
var_dump(isLink($string3));  // true
var_dump(isLink($string4));  // false
Enter fullscreen mode Exit fullscreen mode

In this example, the isLink() function takes a string as input and uses a regular expression pattern to check if it matches the structure of a valid URL. The preg_match() function returns a truthy value (1) if the string matches the pattern, indicating that it is a valid link. Otherwise, it returns a falsy value (0), indicating that the string is not a valid link.


Certainly! Here's the code translated into different programming languages:

Python:

import re

def is_link(string):
    pattern = r'^(https?:\/\/)?([\w\d\-]+\.)+[\w\d\-]+(\/[\w\d\-./?=#%&]*)?$'
    return bool(re.match(pattern, string))

# Usage:
string1 = "https://example.com"
string2 = "www.example.com"
string3 = "example.com"
string4 = "not a link"

print(is_link(string1))  # True
print(is_link(string2))  # True
print(is_link(string3))  # True
print(is_link(string4))  # False
Enter fullscreen mode Exit fullscreen mode

JavaScript:

function isLink(string) {
    var pattern = /^(https?:\/\/)?([\w\d\-]+\.)+[\w\d\-]+(\/[\w\d\-./?=#%&]*)?$/i;
    return pattern.test(string);
}

// Usage:
var string1 = "https://example.com";
var string2 = "www.example.com";
var string3 = "example.com";
var string4 = "not a link";

console.log(isLink(string1));  // true
console.log(isLink(string2));  // true
console.log(isLink(string3));  // true
console.log(isLink(string4));  // false
Enter fullscreen mode Exit fullscreen mode

Java:

import java.util.regex.*;

public class LinkChecker {
    public static boolean isLink(String string) {
        String pattern = "^(https?:\\/\\/)?([\\w\\d\\-]+\\.)+[\\w\\d\\-]+(\\/[\\w\\d\\-./?=#%&]*)?$";
        return Pattern.matches(pattern, string);
    }

    // Usage:
    public static void main(String[] args) {
        String string1 = "https://example.com";
        String string2 = "www.example.com";
        String string3 = "example.com";
        String string4 = "not a link";

        System.out.println(isLink(string1));  // true
        System.out.println(isLink(string2));  // true
        System.out.println(isLink(string3));  // true
        System.out.println(isLink(string4));  // false
    }
}
Enter fullscreen mode Exit fullscreen mode

C#:

using System;
using System.Text.RegularExpressions;

public class LinkChecker
{
    public static bool IsLink(string str)
    {
        string pattern = @"^(https?:\/\/)?([\w\d\-]+\.)+[\w\d\-]+(\/[\w\d\-./?=#%&]*)?$";
        return Regex.IsMatch(str, pattern);
    }

    // Usage:
    public static void Main(string[] args)
    {
        string string1 = "https://example.com";
        string string2 = "www.example.com";
        string string3 = "example.com";
        string string4 = "not a link";

        Console.WriteLine(IsLink(string1));  // True
        Console.WriteLine(IsLink(string2));  // True
        Console.WriteLine(IsLink(string3));  // True
        Console.WriteLine(IsLink(string4));  // False
    }
}
Enter fullscreen mode Exit fullscreen mode

Please note that the translations provided here are simplified versions, and in practice, you may need to handle additional edge cases or optimize the code for specific language conventions.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools can’t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay