How to parse a variable and replace characters with graphics

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gretsch
    New Member
    • Sep 2007
    • 31

    How to parse a variable and replace characters with graphics

    Hi All,
    Your help/guidance please.
    I have a variable* which I need to parse and replace each character with its corresponding graphic image.

    Ideally, its got to be efficient, as actually I have a LOT of varibles to parse ;-).
    I realize I could use an embedded font, but I want to avoid browser security issues for visitor. {oh, & I haven't got/used a font editor before :-}

    Nb The variables only contains digits "1" to "9" + "," (ie 1000s separator)

    Thanks.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Gretsch.

    The easy way to do it is to create a graphic file for each character and name it 1.jpg, 2.jpg ... ,.jpg.

    Then simply:
    [code=php]
    $str = preg_replace('/[\\d,]/', '<img src="path/to/\\0.jpg" alt="\\0" />', $str);
    [/code]

    Note the '\\0's in the replacement string; these tell preg_replace() to insert whatever it matched into the src and alt attributes.

    Comment

    • Gretsch
      New Member
      • Sep 2007
      • 31

      #3
      Thanks,

      I see you've called the code PHP.

      For reasons too complicated to type ;-).... I have to do it client side. Is the syntax/effect exacly the same in Javascript?

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Gretsch.

        Good thing I'm on top of these things :P

        In JavaScript, the syntax would be similar, but you'll need to do things a wee bit differently:
        [code=javascript]
        document.getEle mentById('where TheNumbersGo'). innerHTML = str.replace(/[\d,]/, '<img src="path/to/\\0.jpg" alt="\\0" />');
        [/code]

        Comment

        • Gretsch
          New Member
          • Sep 2007
          • 31

          #5
          Brilliant, thanks very much 4 your help.
          This really is a great forum.

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, Gretsch.

            Good luck with your project, and if you ever need anything, post back anytime :)

            Comment

            Working...