Trying to spin a text based on conditions

Discussion in 'PHP' started by hasbehas, Jan 2, 2012.

  1. #1
    What I am tring to do is ;
    depending on the conditions (in this case if the domain is a .com domain ) then choose the first variation. If not, choose the second variation.

    <?
    
    $dom = $_GET['dom'];
    
    function spin($var){
    $words = explode("{",$var);
    foreach ($words as $word)
    {
        $words = explode("}",$word);
        foreach ($words as $word)
        {
            $words = explode("|",$word);
    if ($dom = "com"){$word = $words[0];}
    else {$word = $words[1];}
    echo $word." ";
        }
    }
    }
    $text = "<font size=\"6\">is this the domain {<b>.com</b> |<b>.us</b> } ?, so you are from {anywhere|usa} ?</font><br />";
    spin($text);
    
    
    ?>
    PHP:
     
    hasbehas, Jan 2, 2012 IP
  2. Basti

    Basti Active Member

    Messages:
    625
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    90
    #2
    
    $dom = parse_url($_GET['dom'], PHP_URL_HOST);
    
    Code (markup):
    And for the if matching 1 thing you could use is this.

    
    if (preg_match('/\.com$/i', $dom)) {
        $word = $words[0];
    }
    
    PHP:
    What all this should do is.
    1. parse the get value from $_GET['dom'], which just returns domain, e.g - google.com , google.co.uk, subdomain.google.com etc etc
    2. in the preg match we search for .com and indicate that the search string end after the pattern ($), so it dont match subdomain.commercial.net and the likes
    Search using case-insentive (i), so it also macthes if one write .COM

    For variations replace .com with .co.uk , .net etc
    When you have more variations in iam sure it could be written using less code ( so not using if -> elseif -> elseif -> else )
     
    Basti, Jan 2, 2012 IP
  3. Chronus

    Chronus Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    For variations you may want to grab the TLD and use "switch" instead of "if"
     
    Chronus, Jan 2, 2012 IP
  4. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #4
    thanks all but, my problem (way) is to spin the text, not get the domain name.

    I was trying to spin the text based on what the domain was. :)

    all sorted anyways.. thanks again.
     
    hasbehas, Jan 2, 2012 IP
  5. Basti

    Basti Active Member

    Messages:
    625
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    90
    #5
    Oh a little misread there :D, sorry
    But good you got it sorted :)
     
    Basti, Jan 3, 2012 IP