Results 1 to 6 of 6

Thread: php regex help!

  1. #1
    Join Date
    Feb 2007
    Location
    Colorado, USA
    Posts
    3,716
    Mentioned
    51 Post(s)
    Quoted
    624 Post(s)

    Default php regex help!

    PHP Code:
    function BBCode ($string) {

    $search = array(

            
    '@\[(?i)b\](.*?)\[/(?i)b\]@si',

            
    '@\[(?i)i\](.*?)\[/(?i)i\]@si',

            
    '@\[(?i)u\](.*?)\[/(?i)u\]@si',

            
    '%(@post#)(\d+)%',

            
    '@\[(?i)img\](.*?)\[/(?i)img\]@si',

            
    '@(?:https://)?(?:http://)?(?:www\.)?(?:youtube\.com/(?:v/|watch\?v=)|youtu\.be/)([\w-]+)(?:\S+)?@si',

            
    '@\[(?i)url\](?:https?:\/\/)?(.*?)\[/(?i)url\]@si'

    );

    $replace = array(

            
    '<b>\\1</b>',

            
    '<i>\\1</i>',

            
    '<u>\\1</u>',

            
    '<a href="#$2" onclick="qp($2)">$1$2</a>',

            
    '<img src="\\1" onclick="imagesize(this)" alt="~img broke/loading~" style="height:200px" />',

            
    '<br><a href="https://youtu.be/$1" target="_blank">https://youtu.be/$1</a> <span onClick="ytvid(&quot;a$1&quot;)"><img id="a$1img" src="http://img.youtube.com/vi/$1/0.jpg" height="150px" style="display:block;"></span><embed id="a$1vid" width="600" height="425" src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" wmode="direct" allowfullscreen="true" style="display:none">',

            
    '<a href="http://\\1" target="_blank">\\1</a>'

    );

    return 
    preg_replace($search$replace$string);

    }
    function 
    str_insert($str$search$insert) {

      
    $index strpos($str$search);

      if(
    $index === false) {

          return 
    $str;

      }

      return 
    substr_replace($str$search.$insert$indexstrlen($search));

    }

    function 
    generateRandomString($length 5) {

        
    $characters '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

        
    $randomString '';

        for (
    $i 0$i $length$i++) {

            
    $randomString .= $characters[rand(0strlen($characters) - 1)];

        }

        return 
    $randomString;

    }

    $post1 bbcode($post);

    $randcode generateRandomString();

    $post1 =  str_insert($post1'onClick="ytvid(&quot;a'$randcode);

    $post1 =  str_insert($post1'<embed id="a'$randcode);

    $post1 =  str_insert($post1'"><img id="a'$randcode);

    $post1 preg_replace('/(\r\n|\n|\r)/','<br>',$post1); 

    Hello, this is a chunk of code I have used on my custom forums for many months

    It works extremely well, please ignore my youtube code part, as it's very good at what it does for my client side javascript for youtube videos & smartphones etc


    I need help with getting URL's to auto embed, and youtube videos.. currently if a youtube video is posted, from youtu.be or youtube.com it'll auto embed it with my special stuff, it works amazingly.. however URL's have to be wrapped in the [url] I have thought of ghetto ways of fixing this in the past, however I want to be able to just post any youtube url & it slaps that into the code I have, or if you linked any link to a website (other than youtube) it'll just do a normal <a href= type deal

    anyone able to help? skype or other places
    The only true authority stems from knowledge, not from position.

    You can contact me via matrix protocol: @grats:grats.win or you can email me at the same domain, any user/email address.

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by grats View Post
    ...anyone able to help? skype or other places...


    I not a pro at php or anything and my standards might be old (it's been a while) or bad but I'm not sure if your regex for the youtube tag is correct? Well it might be but I couldn't get it to work so that's why I say that.

    Here is how I did it (Test.php)..

    php Code:
    <?php
        $StrToSearch = "http://www.youtube.com/watch?v=2vAWjBmGZAk";
       
    //[NoParse] //Makes villavu's forum not parse my regex as an embedded url :l
       
            $Patterns = array("@(https://)?(http://)?(?:www\.)?(?:(youtube\.com)|(youtu\.be))/(?:v/|watch\?v=)([A-Za-z0-9]+)@si");
           
    //[/NoParse]


    //NOTE: You can replace $2 in the below code with "http://" or "https://" in case the user doesn't have that part in the url.

        $ObjReplacement = array(
                "<object data='$2$3/v/$5' type='application/x-shockwave-flash' wmode='direct' width='425' height='350'>
                    <param name='src' value='$2$3/v/$5' />
                    <param name='allowFullScreen' value='true' />
                </object>"
    );
       
        $EmbedReplacement = array("<embed src='$2$3/v/$5' type='application/x-shockwave-flash' allowfullscreen='true' wmode='direct' width='425' height='350'></embed>");
       
       
        print "<h2>Using Object tags:</h2><br />";
        print preg_replace($Patterns, $ObjReplacement, $StrToSearch);
       
        print "<br /><br /><h2>Using Embed tags:<h2><br />";
        print preg_replace($Patterns, $EmbedReplacement, $StrToSearch);
    ?>


    And it ends up looking like:

    Last edited by Brandon; 10-08-2013 at 11:08 PM.
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Feb 2007
    Location
    Colorado, USA
    Posts
    3,716
    Mentioned
    51 Post(s)
    Quoted
    624 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    I not a pro at php or anything and my standards might be old (it's been a while) or bad but I'm not sure if your regex for the youtube tag is correct? Well it might be but I couldn't get it to work so that's why I say that.

    Here is how I did it (Test.php)..

    php Code:
    <?php
        $StrToSearch = "http://www.youtube.com/watch?v=2vAWjBmGZAk";
       
    //[NoParse] //Makes villavu's forum not parse my regex as an embedded url :l
       
            $Patterns = array("@(https://)?(http://)?(?:www\.)?(?:(youtube\.com)|(youtu\.be))/(?:v/|watch\?v=)([A-Za-z0-9]+)@si");
           
    //[/NoParse]


    //NOTE: You can replace $2 in the below code with "http://" or "https://" in case the user doesn't have that part in the url.

        $ObjReplacement = array(
                "<object data='$2$3/v/$5' type='application/x-shockwave-flash' wmode='direct' width='425' height='350'>
                    <param name='src' value='$2$3/v/$5' />
                    <param name='allowFullScreen' value='true' />
                </object>"
    );
       
        $EmbedReplacement = array("<embed src='$2$3/v/$5' type='application/x-shockwave-flash' allowfullscreen='true' wmode='direct' width='425' height='350'></embed>");
       
       
        print "<h2>Using Object tags:</h2><br />";
        print preg_replace($Patterns, $ObjReplacement, $StrToSearch);
       
        print "<br /><br /><h2>Using Embed tags:<h2><br />";
        print preg_replace($Patterns, $EmbedReplacement, $StrToSearch);
    ?>


    And it ends up looking like:

    uhh, thanks.. but my youtube has worked fine for the year my forums have used it, I need url & youtube to auto embed, instead of having to put [url] brackets around urls

    as I said, the youtube requires the client side javascript, which isn't on this thread

    also the method this forum uses, and all other forum softwares for that matter.. to embed youtube videos, is horrid.. I use a more of youtube method, which doesn't load flash for every video on a page & crash them. It loads the picture and when you click it, the video then plays... this is the main goal of my youtube, which, needs no fixing
    The only true authority stems from knowledge, not from position.

    You can contact me via matrix protocol: @grats:grats.win or you can email me at the same domain, any user/email address.

  4. #4
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by grats View Post
    uhh, thanks.. but my youtube has worked fine for the year my forums have used it, I need url & youtube to auto embed, instead of having to put [url] brackets around urls

    as I said, the youtube requires the client side javascript, which isn't on this thread

    also the method this forum uses, and all other forum softwares for that matter.. to embed youtube videos, is horrid.. I use a more of youtube method, which doesn't load flash for every video on a page & crash them. It loads the picture and when you click it, the video then plays... this is the main goal of my youtube, which, needs no fixing

    I'm confused by youtube and url's auto embedding. Doesn't your youtube auto embed already? Then the only problem is URLs? Or is the url regex matching with the youtube one as well?

    Not sure what "exactly" it is you need but does the below help at all?

    PHP Code:
    $SomeUserPost = "testing url stuff http://villavu.com/forum/member.php?u=8135 other stuff here..";
       
    $Patterns = array("@(^|\s+)((http://)|(https://)|(mailto:)|(ftp://)|(www\.))([A-Za-z0-9!\*'\(\);:\@\&=\+\$\,/\?\#\[\]-_\.~]+)(\s|$)@si");
       
    $Replacements = array(" <a href='$0' target='_blank'>Link Here</a> ");
       
    print "<br /><br /><br /><br />";
    print preg_replace($Patterns, $Replacements, $SomeUserPost);


    If I have to guess again, I think what you really want is to have a tag that contains a hidden video and when a link is clicked, show that video on the page?


    In that case, this should work:

    PHP Code:
    <?php
        print "<html>\n";
        print "<head>\n";
        print "  <title>Test Page</title>\n";
        print "  <style>\n";   
        print "    .vis {display:block;}\n    .invis {display:none;}\n";
        print "  </style>\n";
        print "<script>";

        print "
        function show() {  
            document.getElementById('video').className='vis';
            return true;
        }  

        function hide() {  
            document.getElementById('video').className='invis';  
            return true;
        }
       
        function toggle() {
            if (document.getElementById('video').className == 'invis') {
                show();
                document.getElementById('showhide').innerHTML = \"<a id='showhide' href='#' onclick='return show();'>Click Here To Hide Video</a>\"
            }
            else {
                hide();
                document.getElementById('showhide').innerHTML = \"<a id='showhide' href='#' onclick='return hide();'>Click Here To Show Video</a>\"
            }
            return true;
        }
       
        \n"
    ;
       
        print"</script>\n";
        print "</head>\n";
       
        print "<body>\n\n";
       
       
        $StrToSearch = "http://www.youtube.com/watch?v=2vAWjBmGZAk";
        $Patterns = array("@(https://)?(http://)?(?:www\.)?(?:(youtube\.com)|(youtu\.be))/(?:v/|watch\?v=)([A-Za-z0-9]+)@si");
        $EmbedReplacement = array("<embed src='$2$3/v/$5' type='application/x-shockwave-flash' allowfullscreen='true' wmode='direct' width='425' height='350'></embed>");
       
        print "<a id='showhide' href='#' onclick='return toggle();'>Click Here To Show Video</a>";
        print "<div id='video' class='invis'>";
        print preg_replace($Patterns, $EmbedReplacement, $StrToSearch);
        print "</div>";
       
        print "\n</body>\n";
        print "</html>";
    ?>

    The above create a toggle link with a video embedded in it starting invisible. On click, the video shows. On click again, it hides.
    Only thing that would need to be done is to assign each div a unique id and pass that to the toggle/show/hide dynamically.

    If I'm still misunderstanding then I'm not sure what is required :l
    Last edited by Brandon; 10-09-2013 at 03:26 AM.
    I am Ggzz..
    Hackintosher

  5. #5
    Join Date
    Feb 2007
    Location
    Colorado, USA
    Posts
    3,716
    Mentioned
    51 Post(s)
    Quoted
    624 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    I'm confused by youtube and url's auto embedding. Doesn't your youtube auto embed already? Then the only problem is URLs? Or is the url regex matching with the youtube one as well?

    Not sure what "exactly" it is you need but does the below help at all?

    PHP Code:
    $SomeUserPost = "testing url stuff [url]http://villavu.com/forum/member.php?u=8135[/url] other stuff here..";
       
    $Patterns = array("@(^|\s+)(([url]http://)|(https://)|(mailto:)|(ftp://)|(www\.))([/url][A-Za-z0-9!\*'\(\);:\@\&=\+\$\,/\?\#\[\]-_\.~]+)(\s|$)@si");
       
    $Replacements = array(" <a href='$0' target='_blank'>Link Here</a> ");
       
    print "<br /><br /><br /><br />";
    print preg_replace($Patterns, $Replacements, $SomeUserPost);


    If I have to guess again, I think what you really want is to have a tag that contains a hidden video and when a link is clicked, show that video on the page?


    In that case, this should work:

    PHP Code:
    <?php
        print "<html>\n";
        print "<head>\n";
        print "  <title>Test Page</title>\n";
        print "  <style>\n";   
        print "    .vis {display:block;}\n    .invis {display:none;}\n";
        print "  </style>\n";
        print "<script>";

        print "
        function show() {  
            document.getElementById('video').className='vis';
            return true;
        }  

        function hide() {  
            document.getElementById('video').className='invis'  ;  
            return true;
        }
       
        function toggle() {
            if (document.getElementById('video').className == 'invis') {
                show();
                document.getElementById('showhide').innerHTML = \"<a id='showhide' href='#' onclick='return show();'>Click Here To Hide Video</a>\"
            }
            else {
                hide();
                document.getElementById('showhide').innerHTML = \"<a id='showhide' href='#' onclick='return hide();'>Click Here To Show Video</a>\"
            }
            return true;
        }
       
        \n"
    ;
       
        print"</script>\n";
        print "</head>\n";
       
        print "<body>\n\n";
       
       
        $StrToSearch = "http://www.youtube.com/watch?v=2vAWjBmGZAk";
        $Patterns = array("@([url]https://)?(http://)?(?:www\.)?(?:(youtube\.com)|(youtu\.be))/(?:v/|watch\?v=)([/url][A-Za-z0-9]+)@si");
        $EmbedReplacement = array("<embed src='$2$3/v/$5' type='application/x-shockwave-flash' allowfullscreen='true' wmode='direct' width='425' height='350'></embed>");
       
        print "<a id='showhide' href='#' onclick='return toggle();'>Click Here To Show Video</a>";
        print "<div id='video' class='invis'>";
        print preg_replace($Patterns, $EmbedReplacement, $StrToSearch);
        print "</div>";
       
        print "\n</body>\n";
        print "</html>";
    ?>

    The above create a toggle link with a video embedded in it starting invisible. On click, the video shows. On click again, it hides.
    Only thing that would need to be done is to assign each div a unique id and pass that to the toggle/show/hide dynamically.

    If I'm still misunderstanding then I'm not sure what is required :l
    what? Are you messing with me or something? I am so confused

    yes, the youtube works, as I've said like 10 times.
    I want both the youtube and the url to auto embed (the youtube) and auto link (the url) when someone posts them, instead of having to put [url] brackets around them...? I have absolutely no idea how to explain it better


    this is a URL
    http://youtu.be/pwud6Qh4e_c
    it is going to turn into a link when I post, without having to put on brackets

    this is a youtube vid, it REQUIRES me to wrap the tags around it




    on my forum there are no tags for youtube, there are tags for url, I want to have no tags for both.
    The only true authority stems from knowledge, not from position.

    You can contact me via matrix protocol: @grats:grats.win or you can email me at the same domain, any user/email address.

  6. #6
    Join Date
    Jan 2008
    Location
    10° north of Hell
    Posts
    2,035
    Mentioned
    65 Post(s)
    Quoted
    164 Post(s)

    Default

    Did you every get this how you wanted?

    How I take it you want a regular expression to get URLs out of a post.

    Then automaticly insert the [url]URL[/url] tags if it's not a youtube video link.

    If it is a youtube video link add [youtube]URL[/youtube] tags.


    I've got to say how you're handling tags is... the harder way?

    Also does it actually work in nested tags with incorrect endings?
    Last edited by Dgby714; 10-16-2013 at 06:29 AM.

    Dg's Small Procedures | IRC Quotes
    Thank Wishlah for my nice new avatar!
    Quote Originally Posted by IRC
    [22:12:05] <Dgby714> Im agnostic
    [22:12:36] <Blumblebee> :O ...you can read minds

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •