SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to perform this ??

    Hello forums !!
    Consider the following case:
    PHP Code:
    $ref_url "http://www.xyz.com";
    // or $ref_url = "http://xyz.com" (without 'www')
    $check_url "http://xyz.com/index.php?action=a&mode=b&blabla"
    I want to check the if the $check_url contains the $ref_url or not.
    Thanks in advance to all of you.

  2. #2
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume you only want $ref_url to be at the start of $check_url, and not at any other part. For this, do if(0 === strpos($check_url, $ref_url)). If you just want to check that it is in there and doesn't matter where, do if(false !== strpos($check_url, $ref_url)).

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are experienced on regex then go for some regiex functions preg_match() or simply strpos() as Hysteresis suggested.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if he was experienced at regex he wouldn't have needed help would he?

    your main problem here is that your check url may not contain the "www" whereas the ref url might.

    If you can make your ref url to always be http://xyz.com rather then http://www.xyz.com, then it will be much easier.

    for example, you could do something like this to check if it is at the start:
    Code:
    $ref_url = "http://xyz.com";
    $check_url = "http://xyz.com/index.php?action=a&mode=b&blabla";
    
    $cut_url = substr($check_url, 0, strlen($ref_url));
    if( $ref_url.$cut_url == $check_url ) {
     // REF URL is at the start of CHECK URL
    }
    Of course, this is not the best way to do this, but w/e.

    If you merely want to check if the ref url is in the check url, we do this:
    Code:
    // ref, check
    $checkme = strpos($check_url, $ref_url);
    // note the three equal signs
    if($checkme === true) {
     // CHECK contains REF
    }
    hope this helps.

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    <?php
     
    function compareHost ($r, $s) {
     
        $s = parse_url($s);
     
        $r = strtolower(str_replace('www.', '', $r));
        $s = strtolower(str_replace('www.', '', $s['host']));
     
        if ($r !== $s) {
            return false;
        }
     
        return true;
     
    }
     
     
     
    $referance = 'www.xyz.com'; # No http:// or / at the end just host name.
    $source    = 'http://xyz.com/index.php?action=a&mode=b&blabla';
     
    var_dump(compareHost($referance, $source));

    http://www.php.net/manual/en/function.parse-url.php

    There is a problem using strpos() it will return true if http://xyz.com is contained in any part.

    Code:
    $ref = 'http://xyz.com/';
    $c  = 'http://badurl.com/something/page.php?host=http://xyz.com&var=val';
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Php_penguin View Post
    Code:
    // ref, check
    $checkme = strpos($check_url, $ref_url);
    // note the three equal signs
    if($checkme === true) {
     // CHECK contains REF
    }
    hope this helps.
    strpos() returns an int. strpos($a, $b) === true will return false for all values of $a and $b. Of course, don't use == as if it is at the beginning, strpos will return 0 which will be treated as false in == meaning you'll miss all the cases where the needle occurs at the start of the haystack.

    I like logic_earth's solution the best.


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
  •