title: How to change every link href value to different url
body: <p>I'm trying to change every url in a paragraph to encrypted url, so i can track all clicks on outgoing links for e.g</p>
  
  <pre><code>$paragraph = "&lt;p&gt;This is an example &lt;a href='example.com'&gt;My Website&lt;/a&gt;&lt;/p&gt;
  &lt;h2&gt;&lt;a href="example2.com"&gt;another outgoing link&lt;/a&gt;&lt;/h2&gt;";
  </code></pre>
  
  <p>I want to get every above url and encrypt them and replace them.</p>
  
  <p>Suppose i got <code>example.com</code> change it into <code>mywebsite.com/track/&lt;?=encrypt("example.com")?&gt;</code>. and finally paragraph should look like this</p>
  
  <pre><code>$paragraph = "&lt;p&gt;This is an example &lt;a href='mywebsite.com/track/29abbbbc-48f1-4207-827e-229c587be7dc'&gt;My Website&lt;/a&gt;&lt;/p&gt;
  &lt;h2&gt;&lt;a href="mywebsite.com/track/91hsksc-93f1-4207-827e-839c5u7sbejs"&gt;&lt;/a&gt;&lt;/h2&gt;";
  </code></pre>
  
  <p>Here is What I've tried</p>
  
  <pre><code>$message = preg_replace("/&lt;a([^&gt;]+)href=\"http\:\/\/([a-z\d\-]+\.[a-z\d]+\.[a-z]{2,5}(\/[^\"]*)?)/i", "&lt;a$1href=\"encrypted url", $message);
  </code></pre>