Match URL : Regular Expressions « String « PHP






Match URL

 
<?php

  $hostRegex = "([a-z\d][-a-z\d]*[a-z\d]\.)*[a-z][-a-z\d]*[a-z]";
  $portRegex = "(:\d{1,})?";
  $pathRegex = "(\/[^\s?]+)?";
  $queryRegex = "(\?[^<>#\"\s]+)?";

  $urlRegex = "/(?:(?<=^)|(?<=\s))((ht|f)tps?:\/\/" . $hostRegex . $portRegex . $pathRegex . $queryRegex . ")/";

  $str = "This is my homepage:  http://home.example.com.";
  $str2 = "This is my homepage:  http://home.example.com:8181/index.php";

  $sample1 = preg_replace($urlRegex, "<a href=\"\\1\">\\1</a>", $str);
  $sample2 = preg_replace($urlRegex, "<a href=\"\\1\">\\1</a>", $str2);

  echo $sample1 . "\n";
  echo $sample2 . "\n";

?>
  
  








Related examples in the same category

1.Brackets [] finds a range of characters.
2.Character Classes
3.Complete list of regular expression examples
4.\b and \B, equate to "On a word boundary" and "Not on a word boundary," respectively.
5.^ and $ are line anchors.
6.Line Anchors
7.Match an IP address
8.Match the smallest number of characters starting with "p" and ending with "t"
9.Matching GUIDs/UUIDs
10.Matching a Valid E-mail Address
11.Matching a Valid IP Address
12.Matching using backreferences
13.Matching with Greedy vs. Nongreedy Expressions
14.Matching with character classes and anchors
15.Matching with |
16.Define a pattern and use parentheses to match individual elements within it
17.Greedy Qualifiers
18.Greedy and non-greedy matching
19.Greedy versus nongreedy matching
20.Grouping captured subpatterns
21.Validating Pascal Case Names
22.Validating U.S. Currency
23.Validating a credit card number
24.Nongreedy Qualifiers
25.POSIX Regular Expressions Character Classes
26.POSIX Regular Expressions Character Classes
27.Ranges
28.Option patterns:
29.Predefined Character Ranges (Character Classes)
30.Pattern matches:
31.Pattern match extenders:
32.Perl-Compatible Regular Expressions (PCRE)
33.Qualifiers restrict the number of times the preceding expression may appear.
34.Quantifiers for Matching a Recurring Character
35.Quantifiers: +, *, ?, {int. range}, and $ follow a character sequence:
36.Special classes for regular expression
37.Regular expressions using character classes