title: Finding urls from text string via php and regex?
body: <p>I know the question title looks very repetitive. But some of the solution i did not find here.</p>
  
  <p><strong>I need to find urls form text string:</strong></p>
  
  <pre><code>$pattern = '`.*?((http|https)://[\w#$&amp;+,\/:;=?@.-]+)[^\w#$&amp;+,\/:;=?@.-]*?`i';
  
      if (preg_match_all($pattern,$url_string,$matches)) {
          print_r($matches[1]);
      }
  </code></pre>
  
  <p>using this pattern i was able to find urls with <code>http://</code> and <code>https://</code> which is okey. But i have user input where people add url like <code>www.domain.com</code> even <code>domain.com</code></p>
  
  <p>So, i need to validate the string first where i can replace <code>www.domain.com</code> <code>domain.com</code> with common protocol <code>http://</code> before them. Or i need to comeup with more good pattern?</p>
  
  <p>I am not good with regex and don't know what to do.</p>
  
  <p><strong>My idea</strong> is first finding the urls with <code>http://</code> and <code>https://</code> the put them in an array then replace these url with space(" ") in the text string then use other patterns for it. But i am not sure what pattern to use.</p>
  
  <p>I am using this <code>$url_string = preg_replace($pattern, ' ', $url_string );</code> but that removes if any <code>www.domain.com</code> or <code>domain.com</code> url between two valid url with <code>http://</code> or <code>https://</code></p>
  
  <p>If you can help that will be great.</p>
  
  <p><strong>To make things more clear:</strong></p>
  
  <p>i need a pattern or some other method where i can find all urls in a text sting. the example of url are:</p>
  
  <ol>
  <li>domain.com</li>
  <li>www.domain.com</li>
  <li><a href="http://www.domain.com" rel="nofollow">http://www.domain.com</a></li>
  <li><a href="http://domain.com" rel="nofollow">http://domain.com</a></li>
  <li><a href="https://www.domain.com" rel="nofollow">https://www.domain.com</a></li>
  <li><a href="https://domain.com" rel="nofollow">https://domain.com</a></li>
  </ol>
  
  <p>thanks!
   5. </p>