I have have a text field for copy-pasting an iFrame code.

<iframe src="https://webpagelink.com/l/3556456/2019-03-06/34576" width="100%" 
height="500" type="text/html" frameborder="0" allowTransparency="true" 
style="border: 0"></iframe>

What I need help with is striping out the URL within the src="" value from the iframe. Is this possible using Twig?

Thank you for your help!

  • 1
    Do you want to remove the src attribute from the iframe HTML code, or do you want to strip everything but the src attribute (e.g. end up with a variable containing the URL in the original src attribute)? Mar 12 '19 at 23:16
  • Yeah like Mats I'm curious what you're trying to achieve here... Mar 13 '19 at 0:59
  • 1
    @MatsMikkelRummelhoff - My client copy/pastes iframe codes in, and now we realized that I need to inject a variable with entry.title into the iframe. So yes, I just need to get the url only. Mar 14 '19 at 15:08

You could write a module that adds a twig extension to return the value of src from the string. That way you get to utilise PHP as per https://stackoverflow.com/questions/17017684/how-to-get-url-from-the-iframe-using-php to get the value.

https://pluginfactory.io/ is a good starting point for adding a module to Craft.

alternative very dirty approach: If the iframe markup is always going to be the same, you could do |replace(), replacing everything before the src=" and everthing after the " width with empty strings. Then replace " width with an empty string.

  • Thank you @mijewe! Never built a plugin, but yes, it will always be generated iframe from a Pardot form every time. I think I will try and get the |replace to work. Mar 13 '19 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.