MyBB Hacks

Full Version: YouTube profile ID regex
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys, could you help me sort out the YouTube IDs?
Unfortunately YouTube now offers custom IDs to people with 500 min. subscribers only, so I am forced to allow the long weird URLs on my forum as well.

At the moment I have this regex:

PHP Code:
((http|https):\/\/|)(www\.)?youtube\.com\/(channel\/|user\/)[a-zA-Z0-9]{1,}


Which allows - http/https +/ www:

Code:
youtube.com/channel/CoolUserName
youtube.com/user/CoolUserName
youtube.com/channel/xHsadRaNdOmLeTtErS_3-x
youtube.com/user/xHsadRaNdOmLeTtErS_3-x


But it would be amazing if someone could help me with doing it so that you can just enter the ID as well, eg:
http/https and/or www:

PHP Code:
youtube.com/user/CoolUserName - value 2
youtube.com/channel/CoolUserName - value 2
youtube.com/CoolUserName - value 2*
CoolUserName - value 1
youtube.com/user/xHsadRaNdOmLeTtErS_3-x - value 2
youtube.com/channel/xHsadRaNdOmLeTtErS_3-x - value 2
xHsadRaNdOmLeTtErS_3-x - value 1


*youtube.com/CoolUserName - optional.

So the display format would be: {VALUE$1}{VALUE$2}

Unless there is a way to just store the ID (value 1) from the entered URL into the database?

I'm not too sure how you'd differentiate between a channel and a user (is there a distinction?), but you could just simply take your existing regex and make the first part optional.

Code:
(?:((http|https):\/\/|)(www\.)?youtube\.com\/(channel\/|user\/))?[a-zA-Z0-9]{1,}


It's probably possible to just store the ID in the database if you make use of the input formatter (find the last '/' character and take everything from there).

Thanks!

Code:
<if strpos($mybb->input['xthreads_kanaly'], 'youtube.com') !== false then>
substr(strrchr({VALUE}, '/'), 1)
<else>
{VALUE}
</if>


Will something like this work for Input Formatter?

@Edit: The if statement works, not sure how to trim the {VALUE}:

PHP Code:
substr(strrchr({VALUE}, '/'), 1)

Code:
substr({VALUE}, (strrchr({VALUE}, '/') ?: -1)+1)

It's not parsing for some reason, am I missing something?
[Image: k6aMIG9.png]

@Edit:
Nvm: Fixed

Code:
<?=substr(strrchr({VALUE}, '/'), 1)?>


@down:
Thanks Wink

Oops, my mistake, should've been:

Code:
<?=substr({VALUE}, (strrpos({VALUE}, '/') ?: -1)+1)?>

Reference URL's