title: Replace all relative URLs with absolute URLS
body: <p>I've seen a few answers (like <a href="https://stackoverflow.com/questions/31347340/convert-all-relative-urls-to-absolute-urls-while-maintaining-contents">this one</a>), but I have some more complex scenarios I'm not sure how to account for.</p>
  
  <p>I essentially have full HTML documents. I need to replace <em>every single</em> relative URL with absolute URLs.</p>
  
  <p>Elements from potential HTML look as follows, may be other cases as well:</p>
  
  <pre><code>&lt;img src="/relative/url/img.jpg" /&gt;
  &lt;form action="/"&gt;
  &lt;form action="/contact-us/"&gt;
  &lt;a href='/relative/url/'&gt;Note the Single Quote&lt;/a&gt;
  &lt;img src="//example.com/protocol-relative-img.jpg" /&gt;
  </code></pre>
  
  <p>Desired Output would be:</p>
  
  <pre><code>// "//example.com/" is ideal, but "http(s)://example.com/" are acceptable
  
  &lt;img src="//example.com/relative/url/img.jpg" /&gt;
  &lt;form action="//example.com/"&gt;
  &lt;form action="//example.com/contact-us/"&gt;
  &lt;a href='//example.com/relative/url/'&gt;Note the Single Quote&lt;/a&gt;
  &lt;img src="//example.com/protocol-relative-img.jpg" /&gt; &lt;!-- Unmodified --&gt;
  </code></pre>
  
  <p>I DON'T want to replace protocol relative URLs, since they already function as absolute URLs. I've come up with some code that works, but I'm wondering if I can clean it up a little, as it's <strong>extremely</strong> repetitive.</p>
  
  <p>But I have to account for single and double quoted attribute values for <code>src</code>, <code>href</code>, and <code>action</code> (am I missing any attributes that can have relative URLs?) while simultaneously avoiding protocol relative URLs.</p>
  
  <p>Here's what I have so far:</p>
  
  <pre><code>// Make URL replacement protocol relative to not break insecure/secure links
  $url = str_replace( array( 'http://', 'https://' ), '//', $url );
  
  // Temporarily Modify Protocol-Relative URLS
  $str = str_replace( 'src="//', 'src="::TEMP_REPLACE::', $str );
  $str = str_replace( "src='//", "src='::TEMP_REPLACE::", $str );
  $str = str_replace( 'href="//', 'href="::TEMP_REPLACE::', $str );
  $str = str_replace( "href='//", "href='::TEMP_REPLACE::", $str );
  $str = str_replace( 'action="//', 'action="::TEMP_REPLACE::', $str );
  $str = str_replace( "action='//", "action='::TEMP_REPLACE::", $str );
  
  // Replace all other Relative URLS
  $str = str_replace( 'src="/', 'src="'. $url .'/', $str );
  $str = str_replace( "src='/", "src='". $url ."/", $str );
  $str = str_replace( 'href="/', 'href="'. $url .'/', $str );
  $str = str_replace( "href='/", "href='". $url ."/", $str );
  $str = str_replace( 'action="/', 'action="'. $url .'/', $str );
  $str = str_replace( "action='/", "action='". $url ."/", $str );
  
  // Change Protocol Relative URLs back
  $str = str_replace( 'src="::TEMP_REPLACE::', 'src="//', $str );
  $str = str_replace( "src='::TEMP_REPLACE::", "src='//", $str );
  $str = str_replace( 'href="::TEMP_REPLACE::', 'href="//', $str );
  $str = str_replace( "href='::TEMP_REPLACE::", "href='//", $str );
  $str = str_replace( 'action="::TEMP_REPLACE::', 'action="//', $str );
  $str = str_replace( "action='::TEMP_REPLACE::", "action='//", $str );
  </code></pre>
  
  <p>I mean, it works, but it's <em>uuugly</em>, and I was thinking there's probably a better way to do it.</p>