rjangelov

StringsInPHP - //Problem 4. Extract all valid URLs

Jun 30th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.92 KB | None | 0 0
  1. //Problem 4.    Extract all valid URLs
  2. //Write a regex pattern for extracting all URL’s addresses from given text.
  3. //Hint: http://en.wikipedia.org/wiki/Uniform_resource_locator
  4.  
  5. <!DOCTYPE html>
  6. <html>
  7.     <head>
  8.         <meta charset="UTF-8">
  9.         <title></title>
  10.     </head>
  11.     <body>
  12.         <form method="post" action="ExtractValidURLS.php">
  13.             <input type="text" name="str1">
  14.             <input type="submit" value="GO">
  15.         </form>
  16.     </body>
  17. </html>
  18.  
  19. <?php
  20. $string = $_POST['str1'];//"Hello https://www.bytes.com world ftp://www.yahoo.com http://www.yahoo.com/asd/asd";
  21. preg_match_all('/((([A-Za-z]{3,9}:(?:\/\/)?)'
  22.         . '(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-'
  23.         . ']+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za'
  24.         . '-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?'
  25.         . '\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*'
  26.         . '))?)/', $string, $text);
  27. echo '<pre>' . print_r($text[0], true) . '</pre>';
  28. ?>
Advertisement
Add Comment
Please, Sign In to add comment