SitePoint Sponsor

User Tag List

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

    Replacing different text string with images in a document

    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 Javascript 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
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's probably a better way (PHP)...but I'm tired.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript">
    
    var filepath = 'http://pokerfish.ominion.ws/CardImages/';
    var filetype = '.bmp';
    var spacer = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    
    function showCards()
    {
    	var arg, sCard, sSpacer, sTag, a = 0;
    	for (a; a < arguments.length; ++a)
    	{
    		arg = arguments[a];
    		sCard = arg.match(/\*?(.*)/)[1];
    		sSpacer = (/\*/.test(arg)) ? spacer : '&nbsp;';
    		sTag = sSpacer + '<img src="' + filepath + sCard + filetype + '">';
    		document.write(sTag);
    	}
    }
    
    </script>
    </head>
    <body>
    <hr />
    <script type="text/javascript">
    showCards('Ah','6h','3s','Ah','6h','*3s');
    </script>
    <hr />
    <script type="text/javascript">
    showCards('3s','Ah','6h','6h','*3s','Ah');
    </script>
    <hr />
    </body>
    </html>
    You'd need to simplify your image file naming convention (two letters). The asterisk specifies a space before the card.

    State-of-the-art...bad game, though (not for the dealer).
    ::: certified wild guess :::

  3. #3
    SitePoint Zealot
    Join Date
    May 2002
    Location
    USA
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much, aidos! This is wonderful. I appriciate your taking the time to do this for me.

    Thanks again,

    JRF2k
    Man can learn nothing except by going from the known to the the unknown. -- Claude Bernard


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
  •