0
// handle URL
    if (preg_match("/^https?:\/\//", $destination))
    {
        header("Location: " . $destination);
    }

I was wondering why the string inside preg_match has extra /^ and / characters at the start and end of the string. I know that the "/" "/"s are used to escape the slashes, but I just couldn't find the use for the packaging escapes.

1

the first argument that preg_match takes according to the documentation is a pattern. that is, a regular expression to test $destination against in this case.

the forward slashes (i.e., /) at the start and the end of the pattern denote the start and the end of it.

the ^http tests whether $destination starts with http. s? tests whether the http is optionally followed by an s. the :\/\/ tests whether following that is a :// (a backslash before a forward slash inside the pattern escapes it).

so this basically tests whether $destination starts with http:// or https://.

learn more!

1
  • Thank you, this was very helpful! :) Sep 28 '15 at 1:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .