preg_match: ensure the start and the end contains something

Hi,

I want to have the regular expression that makes sure the beginning of the string contains ‘http://’ and ‘/’ and the end.

This is a longer version I came up with,

if(!preg_match("/(^http:\\/\\//", $site_http)) 
    {
     $error = true;
     echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link contains http:// at the start."/>';
    }
    elseif (!preg_match("/\\/$/", $site_http)) 
    {
     $error = true;
     echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link has ended with a /."/>';
    }

but I thought these two expressions can put together like below, but it wont work,

 if(!preg_match("/(^http:\\/\\/)&(\\/$)/", $site_http)) 
    {
     $error = true;
     echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link contains http:// at the start and a / at the end."/>';
    }

the multiple expressions that I try to combine must be wrong! any idea?

thanks,
Lau

thank you so much! :slight_smile:


$url = 'http://www.example.com/';

if (preg_match('/^http:\\/\\/.+\\.[a-z]+\\/$/i', $url, $match)) {
    var_dump($match);
}

$url = 'http://example.com/';

if (preg_match('/^http:\\/\\/.+\\.[a-z]+\\/$/i', $url, $match)) {
    var_dump($match);
}

array(1) {
  [0]=>
  string(23) "http://www.example.com/"
}
array(1) {
  [0]=>
  string(19) "http://example.com/"
}