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
<a href="https://vimeo.com/videoid">Some text</a>.
A post's body might look somewhat like this:
https://vimeo.com/id1 //this line should get replaced with embed code by module
<a href="https://vimeo.com/id1"> Check out this video </a> //here, anything that is in href="" should not be replaced
on <a href="https://vimeo.com"> Vimeo </a>
In this whole post body, the module matches a url (https://vimeo.com/id1) and then uses str_replace() to replace each url with the generated embed code.
$embed_code = $this->convertVimeoUrlToEmbedCode($url);
$return['text'] = str_replace($url, $embed_code, $return['text']);
So far what I tried to do was:
- 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.
- Remove all urls from the text that are in between quotation marks (fixed half of the problem)
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