0

I want to replace width="" height="" of Youtube embed code width class="youtube" instead so that I can set new width and height of Youtube embed with class="youtube" in CSS

I searched for PHP str_replace(), preg_replace() but I still don't know much that they are right to use or do I have to use another function.

Change this :

<iframe width="560" height="315" src="http://" frameborder="0" allowfullscreen></iframe>

To this :

<iframe class="youtube" src="http://" frameborder="0" allowfullscreen></iframe>`
louisfischer
  • 1,968
  • 2
  • 20
  • 38
Yumi
  • 23
  • 1
  • 6
  • use domdocument instead or other html parser – splash58 Oct 10 '17 at 14:11
  • are you trying to make the video responsive or something else? – Nathaniel Flick Oct 10 '17 at 14:11
  • yes,I try to show youtube width and height different in screen size.. – Yumi Oct 10 '17 at 14:14
  • 1
    We are always glad to help and support new coders but ***you need to help yourself first. :-)*** After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Oct 10 '17 at 14:17
  • I am so sorry.I will be more careful next time.Thank you for your advice and support. – Yumi Oct 10 '17 at 14:37
  • This answer is worth a look: https://stackoverflow.com/a/4959046/2943403 It should be simple enough to modify for your needs. – mickmackusa Oct 10 '17 at 23:36

1 Answers1

2

You don't know width or height so you can't use str_replace but preg_replace can solve problem

$newEnbedCode = preg_replace('/width="([0-9]+)" height="([0-9]+)"/', 'class="youtube"', $embedCode);

working example