3

Possible Duplicate:
How to parse and process HTML with PHP?

I want to publish an app for android (wordpress blog). The app displays also some YouTube videos. I need a method to replace YouTube IFRAMES + EMBED-Codes (old embed method) with an a - tag:

<iframe ...


<a href="http://www.youtube.com/watch?v=id"><img src="http://i1.ytimg.com/vi/id/hqdefault.jpg" /></a>

The replacement: either via php (server) or via java (eclipse). I have only found some snippets...

/embed\/([a-zA-Z0-9_-]{11})/

But how could I use it? Please help me!

EDIT: I try to replace the following CODE:

<iframe width="560" height="315" src="http://www.youtube.com/embed/**ID**" frameborder="0" allowfullscreen></iframe>

@Stephan

$content = "contains <iframe... and article"
$search = '/<iframe.+?src="http://www.youtube.com/embed/([a-zA-Z0-9_-]{11})"[^>]+?></iframe>/i';
$replace = '<a href="http://www.youtube.com/watch?v=$1"><img src="http://i1.ytimg.com/vi/$1/hqdefault.jpg" /></a>';
$content = preg_replace($search, $replace, $content);
echo $content;

How could I used it? Error: Unknown modifier '/' (in PHP).

Community
  • 1
  • 1
user1756209
  • 573
  • 10
  • 23

1 Answers1

2

Try this :

~<iframe.+?src="https?://www.youtube.com/embed/([a-zA-Z0-9_-]{11})"[^>]+?></iframe>~i


DEMO :

https://regex101.com/r/2mjn1r/1

Stephan
  • 41,764
  • 65
  • 238
  • 329
  • No, thats not my question :( I will show the images with a link to the video. I have many posts (wordpress). In these posts are YT-IFRAMES. They could played in an android app very bad (flash...). I will replace these IFRAMES automatically with PHP (on server side) or JAVA (on client side). – user1756209 Oct 18 '12 at 12:41
  • Thanks! I've updated my question! How could I used it? – user1756209 Oct 18 '12 at 12:58
  • what if my frame looks like this it has ?featured=oembed in the end – CBeTJlu4ok Sep 26 '13 at 23:01
  • 1
    @Mpa4Hu You can use something like this : `#]+?>#` – Stephan Sep 27 '13 at 00:37
  • thank you for answering, it's not working, I think because I have also content inside those frames (document).. could you open a chat. it's really small – CBeTJlu4ok Sep 27 '13 at 21:31
  • If using in PHP you'll need to escape the slashes and I'd also recommend adding `s?` after http so that you can support https videos. The result is `]+?><\/iframe>` – Pete Oct 27 '16 at 23:06
  • @Pete Thanks for noticing `s?`. Slashes need to be escaped if the regex delimiter is slash. If a different delimiter is used, escaping isn't mandatory anymore. – Stephan Oct 28 '16 at 08:34