Re: generating dynamic heading images



> Ok, I have something working now, but with a few problems, my code is
> below:
>
> <?
> // create a string ..
> $txt = "My String Here";
> $txt = strtoupper($txt);
>
>
> // characters to search for
> $txts = array ( "A", "B", "C", "D", "E", "F", and so forth );
>
> // images array to replace them with.
> $images = array ( "<img src=\"a.gif\" />", "<img src=\"b.gif\" />" ,
> "<img src=\"c.gif\" />" , etc );
> // continue with the <img src>'s with the number pattern above.
>
> // Now, using str_replace to complete all the replacing.
> $final = str_replace ($txts, $images, $txt);
>
> echo $final;
> ?>

You could do away with your arrays by using backreferences and
preg_replace:

$final = preg_replace('/([a-zA-Z])/', '<img src="$1.gif" />', $string);

You would have to adjust things to work with spaces and other special
characters, but this would work with any letter.

--df

.



Relevant Pages

  • Re: Variable naming convention
    ... How would java compilers otherwise know if a string of characters was a ... Prev by Date: ...
    (comp.lang.java.programmer)
  • Re: Dialog titles
    ... take dialog window size of width, ... and add some null characters in the dialog title string ... Prev by Date: ...
    (microsoft.public.pocketpc.developer)
  • Search and Replace question - SEARCHANDREPLACE.fsl (0/1)
    ... string of characters per field. ... I am trying to work on a dbf table ... Prev by Date: ...
    (comp.databases.paradox)
  • Rowfilter in Visual Basic.Net
    ... i face some problem in using rowfilter for visual basic.net. ... if the string contains either, then these two characters must be ... Prev by Date: ...
    (microsoft.public.dotnet.framework.aspnet.datagridcontrol)
  • Re: Each char / letter --> array from string value
    ... The usual way to tear apart a string into an array with characters is ... Prev by Date: ...
    (perl.beginners)

Loading