title: Insert embed code only in place of urls that are not in href
body: <p>I am customizing a module on a drupal 8 website, where certain vimeo or youtube urls are converted into embedded iframe videos. The module itself works great, however a problem arises when there's a tag</p>
  
  <p><code>&lt;a href="https://vimeo.com/videoid"&gt;Some text&lt;/a&gt;.</code> </p>
  
  <p>A post's body might look somewhat like this:</p>
  
  <pre><code>    https://vimeo.com/id1   //this line should get replaced with embed code by module
  
      &lt;a href="https://vimeo.com/id1"&gt; Check out this video &lt;/a&gt; //here, anything that is in href="" should not be replaced
  on &lt;a href="https://vimeo.com"&gt; Vimeo &lt;/a&gt;
  </code></pre>
  
  <p>In this whole post body, the module matches a url (<a href="https://vimeo.com/id1" rel="nofollow noreferrer">https://vimeo.com/id1</a>) and then uses str_replace() to replace each url with the generated embed code.</p>
  
  <pre><code>$embed_code = $this-&gt;convertVimeoUrlToEmbedCode($url);
  $return['text'] = str_replace($url, $embed_code, $return['text']);
  </code></pre>
  
  <p>So far what I tried to do was:</p>
  
  <ul>
  <li>Put a placeholder text in place of the url in the href tag, then replace all urls with the embed code, then put the url back into the placeholder.</li>
  <li>Remove all urls from the text that are in between quotation marks (fixed half of the problem)</li>
  </ul>
  
  <p>In essence, what I'm looking for is for a clean way of replacing the $urls with the embed code while ignoring the $urls that are in quotes or printed in any other way in html tags</p>