SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How Can I add a space between picture elements?

    Hello, here's what I am trying to accomplish and I have no idea how to do it or what I am doing. :-)

    I have a 'blog about online poker. I am getting tired of always having to type out all these image tags to display pictures of the cards, so what I was wanting to do is use a PHP to replace text with the picture of the card.

    Such as AhKdTc would display the image of the Ace of Hearts, King of Diamonds and Ten of Clubs. I know that I would need to put all 52 card images into an array, but after that I am flummoxed. I am not sure how to make the script parse through and say 'Ok, I've come to 2c4dAc. I need to display the images for Two of Clubs, Four of Diamons, and the Ace of Clubs.

    Can anyone help me please? My fingers are getting tired typing in all those image tags.

    Thanks a bunch!

    JRF2k
    Last edited by JRF2k; Feb 20, 2004 at 13:00.
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will those cards just be displayed after each other?
    I'm not into Poker, can you explain the syntax of 2c4dAc a bit more detailed?

  3. #3
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's [number][suite], so:
    Kh (King Hearts)
    9d (9 Diamonds)
    Ac (Ace Clubs)
    etc...

  4. #4
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Young Twig has the syntax down. 2c4dAc would be Two of Clubs, Four of Diamonds and Ace of Clubs.

    Basically, it will look like this: CARD1CARD2CARD3 CARD4 CARD5. There's a space after the first three, and the fourth. I tried this with Javascript but it doesn't work very well with the blogger I am using.

    JRF2k
    Last edited by JRF2k; Feb 17, 2004 at 20:19.
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  5. #5
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can no one help me with this?
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  6. #6
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd suggest that you slightly alter your syntax to ensure that the string won't appear anywwhere else in your text - eg. [card]4c[/card] rather than 4c,

    PHP Code:
    $text "I want to display the four of clubs using [card]4c[/card].  That should do it."
    then a str_replace like
    PHP Code:
    $text str_replace("[card]","<img src=/path/to/imagedir/",$text);
    $text str_replace("[/card]",".jpg />",$text); 
    to turn that into

    "I want to display the four of clubs using <img src=/path/to/imagedir/4c.jpg />. That should do it.";

    Make sense? I know there are probably better ways, but I am not too hot on regular expressions at the moment.....
    Your mind is like a parachute. It works best when open.
    (HH The Dalai Lama)

  7. #7
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Untested, hope it helps.
    PHP Code:
    function handle_card_bbc ($string) {
        
    $cards array_map(create_function('$s''return trim($s);'), explode('|'$string));
        
    $suits = array('s''h''c''d');
        
    $ranks = array('A''K''Q''J''10''9''8''7''6''5''4''3''2');
        
    $return '';
        foreach (
    $cards as $card) {
            switch(
    strlen($card)) {
                case 
    2:
                    
    $rank $card{0};
                    
    $suit $card{1};
                    break;
                case 
    3:
                    
    $rank substr($card02);
                    
    $suit $card{3};
                    break;
                default:
                    return 
    '';
            }
            if (!
    in_array($rank$ranks) || !in_array($suit$suits)) {
                return 
    '';
            }
            
    // filename: suit_rank.gif
            
    $filename $suit '_' $rank '.gif';
            
    $return .= '<img src="images/cards/' $filename '" class="card" />';
        }
        return 
    $return;
    }

    $string 'Check out these cards [cards]2c|4d|Ac[/cards] neat, eh?';
    $string preg_replace('~\[cards\](.+?)\[/cards\]~ei'"handle_card_bbc('\\1')"$string); 

  8. #8
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys I will try both methods and see how it goes.
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  9. #9
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both scripts work great! Thanks again guys. My only question is how would I insert a space after the 3rd card and fourth card to produce something like:

    AhAdKc 4d 5c

    Would this be an easy thing to do?

    Thanks again for the scripts they were exactly what I was looking for!
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  10. #10
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any ideas? Everything I have tried has failed.
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  11. #11
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JRF2k
    Any ideas? Everything I have tried has failed.
    You could probably use a similar kind of idea to display a "spacer" image. A 1x1 transparent gif would be a good idea for the image src, an d then you could add an extra str_replace to search for you spacing terminator. eg

    PHP Code:
    $spacertext = [card]10c[/card][card]jc[/card][card]qc[/card][card]spacer[/card][card]kc[/card][card]spacer[/card][card]1c[/card]
    $spacertext str_replace("[card]spacer[/card]","<img src=/path/to/imagedir/spacer.gif width=12>",$spacertext); 
    Your mind is like a parachute. It works best when open.
    (HH The Dalai Lama)


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
  •