Detecting a url using preg_match? without http:// in the string

后端 未结 5 1049
执念已碎
执念已碎 2020-11-30 08:12

I was wondering how I could check a string broken into an array against a preg_match to see if it started with www. I already have one that check for http://www.

         


        
5条回答
  •  囚心锁ツ
    2020-11-30 08:32

    I used this below which allows you to detect url's anywhere in a string. For my particular application it's a contact form to combat spam so no url's are allowed. Works very well.

    Link to resource: https://css-tricks.com/snippets/php/find-urls-in-text-make-links/

    My implementation;

     1000) {
    $messageError = "1000 chars max";
    }
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    if (preg_match($reg_exUrl, $message)) {
    $messageError = "Url's not allowed";
    }
    
    // Validate data
    function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }
    ?>
    

提交回复
热议问题