Help With @ Tagging System

Discussion in 'PHP' started by Jeremy Benson, Mar 3, 2016.

  1. #1
    hey,

    I was wondering if you guys could help me with my @ tagging class. I got one half built, but it's kind of crud. Right now it's doing half the job. It will make <a></a> links around users that are in the DB, but not properly.

    If I write "Hello how are you @JeremyBenson11?" It will tag the name just fine.

    If there is a name before a known name that isn't known it won't tag anything.

    There's probably a few major design flaws, lol.

    Problem function

    
     
        public function handle_user_link($str){
    
                if( preg_match_all("/(@\w+)/", $str, $matches)){
                    $matches = array_slice($matches, 0, 1);
                   
                    foreach($matches[0] as $tag)  
                    {
                       
                        // capture each tag
    
                        $name = ltrim($tag, '@');
                       
                        try{
                           
                            $db = new PDO($this->db['dsn'], $this->db['username'], $this->db['password'], array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
                       
                        }catch(\PDOException $e){}
                       
                        $sqlName = $db->prepare("SELECT `username` FROM `users` WHERE `username` = ?");
                        $sqlName->execute(array($name));
                        $retrievedUser = $sqlName->fetch();
                       
                        if(!empty($retrievedUser))
                        {
                           
                            // tagged user exists.
                            $str = str_replace($tag, '<a href="profile.php?username='.$name.'">' . $tag . '</a>'  , $str);
                           
                           
                        }
                       
                    }
                   
                }
       
        return $str;
        // End handle_user_link
    }
    
    
    
    PHP:
    Class
    <?php
    class pTagging{
    
        private $db = array();
    
        public function db_set($dbSet)
        {
    
            $this->db['dsn'] = $dbSet['dsn'];
            $this->db['username'] = $dbSet['username'];
            $this->db['password'] = $dbSet['password'];
    
        }
    
       
        public function handle_user_link($str){
    
                if( preg_match_all("/(@\w+)/", $str, $matches)){
                    $matches = array_slice($matches, 0, 1);
                   
                    foreach($matches[0] as $tag)  
                    {
                       
                        // capture each tag
    
                        $name = ltrim($tag, '@');
                       
                        try{
                           
                            $db = new PDO($this->db['dsn'], $this->db['username'], $this->db['password'], array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
                       
                        }catch(\PDOException $e){}
                       
                        $sqlName = $db->prepare("SELECT `username` FROM `users` WHERE `username` = ?");
                        $sqlName->execute(array($name));
                        $retrievedUser = $sqlName->fetch();
                       
                        if(!empty($retrievedUser))
                        {
                           
                            // tagged user exists.
                            $str = str_replace($tag, '<a href="profile.php?username='.$name.'">' . $tag . '</a>'  , $str);
                           
                           
                        }
                       
                    }
                   
                }
       
        return $str;
        // End handle_user_link
    }
    
    public function return_names($str)
    {
    
        $names = array();
    
        if( preg_match_all('/([@]+\w+)/', $str, $matches)){
                    $matches = array_slice($matches, 0, 1);
                
                    for($i = 0; $i < count($matches); $i++)
                    {
                        // capture each tag
                        $tag = $matches[0][$i];
                        $name = ltrim($tag, '@');
                    
                        try{
                        
                            $db = new PDO($this->db['dsn'], $this->db['username'], $this->db['password'], array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
                    
                        }catch(\PDOException $e){}
                    
                        $sqlName = $db->prepare("SELECT `username` FROM `users` WHERE `username` = ?");
                        $sqlName->execute(array($name));
                        $retrievedUser = $sqlName->fetch();
                    
                        if(!empty($retrievedUser))
                        {
                        
                            // tagged user exists.
                             array_push($names, $name);
                        
                        
                        }
                    
                    }
                
                }
    
        return $names;
        // End return names
    }
    
    //End Class
    
    }
    
    
    ?>
    PHP:
     
    Last edited: Mar 3, 2016
    Jeremy Benson, Mar 3, 2016 IP
  2. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #2
    I updated the code above. I got it to stop tripping over unknown tags ahead of known ones. There's an issue with similar names though. @JeremyBenson112 wraps a link around the part that matches..
     
    Jeremy Benson, Mar 3, 2016 IP
  3. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    I've cleaned this up a lot with the help of Dormolich, but there's a problem, seemingly narrowed to the part that makes the a element wrap around the tag.

    This part

    
    
          if(!empty($retrievedUser))
                        {
                            echo $name . '<br>';
                            // tagged user exists.
                            //$str = str_replace($tag, '<a href="profile.php?username='.$name.'">' . $tag . '</a>'  , $str);
                            $str = preg_replace("/@(\w+)/" , '<a href="profile.php?username='.$name.'">' . $tag . '</a>' , $str);
                           
                        }
    
    
    PHP:
    I've echoed out $name in there and only JeremyBenson11 is a match. The reason I'me getting tags where I shouldn't be has to be that one little bit that's making them. Any insight?

    
    
    <?php
    class pTagging{
    
        private $db = array();
       
        public function db_set($dbSet)
        {
       
            $this->db['dsn'] = $dbSet['dsn'];
            $this->db['username'] = $dbSet['username'];
            $this->db['password'] = $dbSet['password'];
       
        }
       
        public function handle_user_link($str){
       
                if( preg_match_all("/@(\w+)/", $str, $matches)){
                   
                    try{
                           
                        $db = new PDO($this->db['dsn'], $this->db['username'], $this->db['password'], array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
                       
                    }catch(\PDOException $e){}
                   
                    foreach($matches[0] as $tag)  
                    {
                       
                        // capture each tag
                       
                        $name = ltrim($tag, '@');
                                       
                        $sqlName = $db->prepare("SELECT `username` FROM `users` WHERE `username` = ?");
                        $sqlName->execute(array($name));
                        $retrievedUser = $sqlName->fetch();
                       
                        if(!empty($retrievedUser))
                        {
                            echo $name . '<br>';
                            // tagged user exists.
                            //$str = str_replace($tag, '<a href="profile.php?username='.$name.'">' . $tag . '</a>'  , $str);
                            $str = preg_replace("/@(\w+)/" , '<a href="profile.php?username='.$name.'">' . $tag . '</a>' , $str);
                           
                        }
                   
                    }
                   
                }
       
        return $str;
        // End handle_user_link
    }
    
    public function return_names($str)
    {
       
        $names = array();
       
        if( preg_match_all('/([@]+\w+)/', $str, $matches)){
                    $matches = array_slice($matches, 0, 1);
               
                    for($i = 0; $i < count($matches); $i++)
                    {
                        // capture each tag
                        $tag = $matches[0][$i];
                        $name = ltrim($tag, '@');
                       
                        try{
                           
                            $db = new PDO($this->db['dsn'], $this->db['username'], $this->db['password'], array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
                       
                        }catch(\PDOException $e){}
                       
                        $sqlName = $db->prepare("SELECT `username` FROM `users` WHERE `username` = ?");
                        $sqlName->execute(array($name));
                        $retrievedUser = $sqlName->fetch();
                       
                        if(!empty($retrievedUser))
                        {
                           
                            // tagged user exists.
                            array_push($names, $name);
                           
                           
                        }
                       
                    }
                   
                }
       
        return $names;
        // End return names
    }
    
    //End Class
    
    }
    
    
    ?>
    
    
    PHP:
     
    Jeremy Benson, Mar 3, 2016 IP