Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts

    Basic Text Class

    This can help jumpstart you creating text validations.. parses urls, and has some bbcode.. work off it.

    PHP Code:
    <?php

    class TextClass
    {

        public function 
    Parse($sText)
        {

            if (!
    function_exists(stripos)) {
                function 
    stripos($string$word)
                {
                    
    $retval false;
                    for (
    $i 0$i <= strlen($string); $i++) {
                        if (
    strtolower(substr($string$istrlen($word))) == strtolower($word)) {
                            
    $retval true;
                        }
                    }
                    return 
    $retval;
                }
            }


            if (
    stripos($sText"a href") === false) {

                if (
    stripos($sText"img") === false) {

                    if (
    stripos($sText"[/link]") === false) {

                        if (
    stripos($sText"src=") === false) {

                            if (
    stripos($sText"[/url]") === false) {


                                
    $sText " " $sText;
                                
    $sText eregi_replace('(((f|ht){1}tps?://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
                                    
    '<a href="\\1" target=\"_blank\">\\1</a>'$sText);

                                
    $sText eregi_replace('([[:space:]()[{}])([url]www.[-a-zA-Z0-9@:%_\+.~#?&//=]+[/url])''\\1<a href="http://\\2" target=_blank>\\2</a>',
                                    
    $sText);


                                
    $sText eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
                                    
    '<a href="mailto:\\1">\\1</a>'$sText);

                            }
                        }
                    }
                }
            }


            
            
    $Arr = array(
            
    "/\[b\](.+?)\[\/b\]/si",
            
    "/\[i\](.+?)\[\/i\]/si",
            
    "/\[quote=(.+?)\](.+?)\[\/quote\]/si",
            
    "/\[quote\](.+?)\[\/quote\]/si",
            
    "/\[url=(.+?)\](.+?)\[\/url\]/si"
            
    );
            
            
    $Rep = array(
            
    "<b>$1</b>",
            
    "<i>$1></i>",
            
    "<blockquote>$1 <b>Said</b> <br /> $2</blockquote>",
            
    "<blockquote>$1</blockquote>",
            
    "<a href=\"$1\" target=\"_blank\">$2</a>"
            
    );
            
            
    $sText preg_replace ($Arr$Rep$sText);
            
            return 
    $sText;

        }

    }
    example usage

    PHP Code:
    require ('Text.class.php');

    $oTxt = New TextClass;

    $Str "This is a test [b]string[/b] to [i]test[/i] the text class out. <br><br> [quote=Username]hello there how are you test test test test test test test test test test test test test test test [/quote] <br><br><br> www.google.com ... http://www.google.com ...emai@eamil.com<BR><BR><BR>";

    echo 
    $oTxt->Parse($Str); 
    Last edited by Zangeel; 11-17-2008 at 04:03 PM.

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Thank you for your post.
    A couple of things to look at:
    Quote Originally Posted by Zangeel View Post
    PHP Code:
    require ('Text.class.php') or die("Error"); 
    That "or die" section isn't needed, and breaks the script.

    You should use preg_*(PCRE) vs. ereg_*(POSIX) regex functions because preg_* is faster, and the ereg_* functions are going to be removed soon anyway.

    Also, you have a return after a return in your method... which doesn't make any sense.. the execution will stop when it reaches the first return.

  • #3
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    yea fixed the return thing, meant to have $sText :P
    thanks


  •  

    Posting Permissions

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