So I’m working with some pretty awesome HTML strings stored in our DB and I need to be able to parse out the string between the “forum-style” youtube tags as in the example below. I have a solution, but it feels a bit hackish. I’m thinking there’s probably a more elegant way to handle this problem.
<?php
$video_string = '<p><span style="font-size: 12px;"><span style="font-family: verdana,geneva,sans-serif;">[youtube]KbI_7IHAsyw[/youtube]<br /></span></span></p>';
$matches = array();
preg_match('/\][_A-Za-z0-9]+\[/', $video_string, $matches);
$yt_vid_key = substr($matches[0], 1, strlen($matches[0]) - 2 );
I’d change the regex a bit:
Adding the ‘youtube’ part to not replace ALL bb-codes – only the right ones.
I’ve also added the ‘?’ to make the regex less greedy (incase there are multiple YT videos in one post.
I added the pattern modifiers i and s, to be able to match case-insensitive and multiline strings.
Edit:
You may also rather want to use preg_replace, it’ll be a bit less code that way.