vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=111)
-   -   RegEx in Custom Profile Field (https://vborg.vbsupport.ru/showthread.php?t=257663)

Digital Jedi 01-23-2011 02:16 PM

RegEx in Custom Profile Field
 
Having only worked with RegExes in AME, I see that vB handles them a bit differently. I want a Regex that matches typical YouTube URLs along with the extra stings that come along with them. I tried my Regex for my AME YouTube definition:

Code:

^http://[\w.]+youtube\.[\w]+/watch[\?\#!]+v=([\w-]+)[\w&;+=-]*[\#t=]*([\d]*)[&;10shdq=]*|^$
But vB gives me a preg-match error with the exclamation point in there. Didn't work escaping it, either. AME has it's own quirks with Regex, and that's where I learned how to do them. But I suspect that vB does it a little differently. This needs to match these kinds of URLs:

http://www.youtube.com/watch?v=is_b1ahWN3Q
http://www.youtube.com/watch?&v=is_b1ahWN3Q
http://www.youtube.com/watch#!v=is_b1ahWN3Q
http://www.youtube.com/watch?v=is_b1ahWN3Q&some_string=also_with_special_characters_&;+=-

And deep linking examples.

http://www.youtube.com/watch?v=is_b1ahWN3Q#t=5m
http://www.youtube.com/watch?v=is_b1ahWN3Q#t=5m7s
http://www.youtube.com/watch?v=is_b1ahWN3Q#t=120s

Or just be blank. Maybe my matches don't need to be this specific?

kh99 01-23-2011 02:58 PM

This seems to work:

Code:

<?php
$pat = '@^http://[\w.]+youtube\.[\w]+/watch[\?\#!]+v=([\w-]+)[\w&;+=-]*[\#t=]*([\d]*)[&;10shdq=]*|^$@';

$strs = array('http://www.youtube.com/watch?v=is_b1ahWN3Q',
                'http://www.youtube.com/watch?&v=is_b1ahWN3Q',
                'http://www.youtube.com/watch#!v=is_b1ahWN3Q',
                'http://www.youtube.com/watch?v=is_b1ahWN3Q&some_string=also_with_special_characters_&;+=-',
                'http://www.youtube.com/watch?v=is_b1ahWN3Q#t=5m',
                'http://www.youtube.com/watch?v=is_b1ahWN3Q#t=5m7s',
                'http://www.youtube.com/watch?v=is_b1ahWN3Q#t=120s',
    );


foreach ($strs as $str)
{
    preg_match($pat, $str, $matches);
    print_r($matches);
    echo("<BR /><BR />\n");
}
?>


and I get this output:

Code:

Array ( [0] => http://www.youtube.com/watch?v=is_b1ahWN3Q [1] => is_b1ahWN3Q [2] => )

Array ( )

Array ( [0] => http://www.youtube.com/watch#!v=is_b1ahWN3Q [1] => is_b1ahWN3Q [2] => )

Array ( [0] => http://www.youtube.com/watch?v=is_b1ahWN3Q&some_string=also_with_special_characters_&;+=- [1] => is_b1ahWN3Q [2] => )

Array ( [0] => http://www.youtube.com/watch?v=is_b1ahWN3Q#t=5 [1] => is_b1ahWN3Q [2] => 5 )

Array ( [0] => http://www.youtube.com/watch?v=is_b1ahWN3Q#t=5 [1] => is_b1ahWN3Q [2] => 5 )

Array ( [0] => http://www.youtube.com/watch?v=is_b1ahWN3Q#t=120s [1] => is_b1ahWN3Q [2] => 120 )


It seems the second string doesn't match because the pattern doesn't allow for & where it appears in that string.


All times are GMT. The time now is 03:54 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01470 seconds
  • Memory Usage 1,714KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (2)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete