-1

I’m terrible with regex so I’m hoping someone can help! I’m trying to find any iframes that match any YouTube embeds in a document and replace them, for example any of the following:

<iframe src="http://youtube.com" width="100" height="200"></iframe>
<iframe src='http://youtube.com' width="100" height="200"></iframe>
<iframe width="100" src='http://youtube.com' width="100"></iframe>
etc.

Notice that there could be double quotes, single quotes, the order of the src, height and width attributes could vary etc. Can anyone help!

Pharm
  • 152
  • 1
  • 12

1 Answers1

0

Try this:

/(<iframe.*?src=([\"\'])https?:\/\/w{0,3}.?youtube.com.*?(\2).*?\>)(.*?)(\<\/iframe\>)/gm

https://regex101.com/r/fF4vA6/6

mayo
  • 3,845
  • 1
  • 32
  • 42
  • Is there also a way to check for either http or https? – Pharm Oct 04 '15 at 15:34
  • Here's my usage, I think its because I'm using preg_replace vs preg_match_all: `$input = preg_replace('/()(.*?)(\<\/iframe\>)/gm', "test", $input, 1);` – Pharm Oct 04 '15 at 15:45
  • mmm, maybe, the g to search all matches, and the m is for multi lines, maybe you don't need them ! I updated the regex to support http and https ! – mayo Oct 04 '15 at 15:47
  • look this, http://stackoverflow.com/questions/19061037/warning-preg-replace-unknown-modifier-g ,you don't need use the 'g' param ! :) – mayo Oct 04 '15 at 15:49
  • Thanks for the help :) I did see that, I'm using the following: `$input = ''; $input = preg_replace('/()(.*?)(\<\/iframe\>)/m', "test", $input, 1);` But it seems like nothing is being changed – Pharm Oct 04 '15 at 15:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91308/discussion-between-user2988034-and-mayo). – Pharm Oct 04 '15 at 15:56