SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 37 of 37
  1. #26
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    In the spirit of sharing, here's my URIParser class which I recently completed.

    PHP Code:
    <?php
    /**
     * @desc        Parses the supplied URI into its base components
     *                 conforming to RFC 3986, either strictly or loosly.
     * 
     * @author        Anthony Sterling
     *
     * @credit        Steven Levithan - http://blog.stevenlevithan.com/archives/parseuri
     */
    class URIParser
    {
        
    /**
         * @desc    This pattern attempts to split URIs according to RFC 3986.
         *             Specifically, in loose mode, directories don't need to 
         *             end with a slash (e.g., the "dir" in "/dir?query" is 
         *             treated as a directory rather than a file name), and the 
         *             URI can start with an authority without being preceded 
         *             by "//" (which means that the "yahoo.com" in "yahoo.com/search/" 
         *             is treated as the host, rather than part of the directory path).
         *
         * @var        String
         */
        
    private $sStrictPattern '~^(?:(?![^:@]+:[^:@\\/]*@)([^:\\/?#.]+):)?(?:\\/\\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\\/?#]*)(?::(\\d*))?)(((\\/(?:[^?#](?![^?#\\/]*\\.[^?#\\/.]+(?:[?#]|$)))*\\/?)?([^?#\\/]*))(?:\\?([^#]*))?(?:#(.*))?)~';

        
    /**
         * @desc    This pattern deviates slightly from the official generic URI 
         *             spec (RFC 3986), but by doing so allows the function to split 
         *             URIs in a way that most end users would expect intuitively. 
         *             However, the finer details of loose mode preclude it from 
         *             properly handling relative paths which do not start from 
         *             root (e.g., "../file.html" or "dir/file.html").
         *
         * @var        String
         */
        
    private $sLoosePattern '~^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)~';

        
    /**
         * @desc    This array holds all the URI components we match using
         *             the specifed pattern.
         *
         * @var        Array
         */
        
    private $aURIComponents = array();

        
    /**
         * @desc    Instantiates the URIParser
         *
         * @param    String    $sURI        The URI to be parsed
         * @param    Boolean    $bStrict
         *
         * @return     URIParser
         */
        
    public function __construct$sURI$bStrict false )
        {
            
    $aMatches = array();
            
    preg_match$bStrict $this->sStrictPattern $this->sLoosePattern$sURI$aMatches );
            foreach(array(
    'source''protocol''authority''userInformation''username''password''host''port''relative''path''directory''file''queryString''anchor') as $iKey => $mValue)
            {
                
    $this->aURIComponents[$mValue] = (isset($aMatches[$iKey]) && !empty($aMatches[$iKey])) ? $aMatches[$iKey] : null ;
            }
        }

        
    /**
         * @desc    Returns the full URI passed to the parser.
         *
         * @return    String
         */
        
    public function getSource()
        {
            return 
    $this->getComponent('source');
        }

        
    /**
         * @desc    Returns <b>'http'</b> 
         *             in http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top
         *
         * @return    String
         */
        
    public function getProtocol()
        {
            return 
    $this->getComponent('protocol');
        }

        
    /**
         * @desc    Returns <b>'usr:pwd@www.test.com:81'</b> 
         *             in http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top
         *
         * @return    String
         */
        
    public function getAuthority()
        {
            return 
    $this->getComponent('authority');
        }

        
    /**
         * @desc    Returns <b>'usr:pwd'</b> 
         *             in http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top
         *
         * @return    String
         */
        
    public function getUserInformation()
        {
            return 
    $this->getComponent('userInformation');
        }

        
    /**
         * @desc    Returns <b>'usr'</b> in http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top
         *
         * @return    String
         */
        
    public function getUsername()
        {
            return 
    $this->getComponent('username');
        }

        
    /**
         * @desc    Returns <b>'pwd'</b> 
         *             in http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top
         *
         * @return    String
         */
        
    public function getPassword()
        {
            return 
    $this->getComponent('password');
        }

        
    /**
         * @desc    Returns <b>'www.test.com'</b> 
         *             in http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top
         *
         * @return    String
         */
        
    public function getHost()
        {
            return 
    $this->getComponent('host');
        }

        
    /**
         * @desc    Returns <b>'81'</b> 
         *             in http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top
         *
         * @return    Integer
         */
        
    public function getPort()
        {
            return 
    $this->getComponent('port');
        }

        
    /**
         * @desc    Returns <b>'/dir/dir.2/index.htm?q1=0&&test1&test2=value#top'</b> 
         *             in http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top
         *
         * @return    String
         */
        
    public function getRelative()
        {
            return 
    $this->getComponent('relative');
        }

        
    /**
         * @desc    Returns <b>'/dir/dir.2/index.htm'</b> 
         *             in http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top
         *
         * @return    String
         */
        
    public function getPath()
        {
            return 
    $this->getComponent('path');
        }

        
    /**
         * @desc    Returns <b>'index.htm'</b> 
         *             in http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top
         *
         * @return    String
         */
        
    public function geFile()
        {
            return 
    $this->getComponent('file');
        }

        
    /**
         * @desc    Returns <b>'q1=0&&test1&test2=value'</b> 
         *             in http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top
         *
         * @return    String
         */
        
    public function getQueryString()
        {
            return 
    $this->getComponent('queryString');
        }

        
    /**
         * @desc    Returns <b>'top'</b> 
         *             in http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=value#top
         *
         * @return    String
         */
        
    public function getAnchor()
        {
            return 
    $this->getComponent('anchor');
        }

        
    /**
         * @desc    Provides a single point of access for the public
         *             methods to access URI data.
         *
         * @return    Mixed
         */
        
    private function getComponent$sKey )
        {
            return 
    $this->aURIComponents[$sKey];
        }
    }
    ?>
    PHP Code:
    <?php
    require_once('class.URIParser.php');
    $oParser = new URIParser('http://www.sitepoint.com/forums/showthread.php?p=4120759#post4120759');
    echo 
    $oParser->getHost(); #www.sitepoint.com
    ?>
    Thinking about it, you never really see other people's libraries on here...any idea's why?

    It would be a good thing, no?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  2. #27
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Quote Originally Posted by SilverBulletUK View Post
    Thinking about it, you never really see other people's libraries on here...any idea's why?

    It would be a good thing, no?
    Hi,

    Thank you for the share. I have added it to my code bank. Now i did'nt looked for class becuase the script i am coding has to be as simple as possible using very less code as the script will go on alot of websites. So I have to keep it in a single file and make it so that its small and fast.

    Oh btw can you share your db class also ?

    Thanks.

  3. #28
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Tapan View Post
    Okay fixed by doing this:

    SELECT DISTINCT domain, url, linktext FROM links ORDER BY RAND() LIMIT 0, 12
    that didn't fix it

    i mean, it did not meet the requirement that you stated earlier, namely that you wanted 12 different domains

    if you tested it and you got 12 different domains, you were simply just lucky

    this query will actually return 12 different domain/url/linktext combinations
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #29
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Then how will i get rest of the fields using your query ?

    Thanks.

  5. #30
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Tapan View Post
    Then how will i get rest of the fields using your query ?
    if i understand what i think you're asking, then the answer is not a mysql solution at all

    pull 50 links at random

    start printing them out, keep track of the domains that you've printed, and skip over any links for domains that you've already printed

    doing the same in mysql has way too many issues
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #31
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    if i understand what i think you're asking, then the answer is not a mysql solution at all

    pull 50 links at random

    start printing them out, keep track of the domains that you've printed, and skip over any links for domains that you've already printed

    doing the same in mysql has way too many issues
    Hi,

    I think it can work:

    I will use query: SELECT DISTINCT domain, id FROM links LIMIT 0, 12

    then while looping to get the data it has got

    i will then do another query:

    SELECT * FROM links WHERE id = $a1["id"] LIMIT 1;

    This will get me unique data i think for sure.

    Becuase ID field is also primaery key, unique, not null and auto number so it will always be DISTINCT anyways. Let me know what you think about it ?

    Thanks.

  7. #32
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Tapan View Post
    Let me know what you think about it ?
    i think you should test it and see for yourself

    i will be glad to answer any questions afterwards
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #33
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Upon further testing it ain't working

    Thanks.

  9. #34
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    If i get all data which is sent by db into an array then can i filter duplicate data in the array and show unique only ?

    Thanks.

  10. #35
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    That's awful approach, sorry.

    If you only need unique data, just get unique data.

    If you cannot do this via the database, check if the value already exists in the target array before adding it.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #36
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Tapan View Post
    If i get all data which is sent by db into an array then can i filter duplicate data in the array and show unique only ?
    yes, of course

    but as i suggested earlier, there is probably no reason to go beyond 50 links in order to find 12 unique domains
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  12. #37
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Phew at last! Its working. This was a close one! ;-)

    Thanks Guys!


Bookmarks

Posting Permissions

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