please help with regular expression

Ask about a PHP problem here.
Post Reply
gt3000
Posts: 11
Joined: Wed Feb 20, 2013 11:46 am

please help with regular expression

Post by gt3000 »

Hi dear better php!
I have a problem and hope for your help to resolve it.

1.I need to get a code from the youtube link (11 digits, which comes after watch?v=).
2.And remove all text which can be before and after the link, also the text could not be on a page, so anyway i need to get that video code.

For example: "lorem ipsum bla bla htttp://www.youtube.com/watch?v=12345678911 lorem ipsum blablabla".

I was wrote some expression but it doesnt work well :

$new_text = preg_replace('/.[https:\/\/www\.youtube\.com\/watch\?v=].*https:\/\/\www\.youtube\.com\/watch\?v=*(.{11}).*/s', '<img src=timthumb.php?src=http://img.youtube.com/vi/$1/maxresdefa ... h=90&w=230 />', $profile_post_content);
icey2k
Posts: 16
Joined: Mon Jul 04, 2011 4:26 pm

Re: please help with regular expression

Post by icey2k »

You can use substr() ?
$str = "http://youtube.com/watch?v=12324567";
$start = strpos($str, "=");

$newstr = substr($str, $start);
$newstr[0] = "";
echo $newstr;
Probably not the best way of doing it, but it does the job.
Post Reply