Jump to content


This topic is now archived and is closed to further replies.


preg_replace problem

Recommended Posts


I wish to create titles with the use of images insead of embedded fonts, simply to keep the style of font that I have been using throughout the rest of my site.

I have set up a code which I was hoping would replace all of the letters with image tags...

Here's the code I created:


$file_path = "../";

$create_image_title = strtolower("Welcome to my site"); // The title that will be made by the images

$letter[0] = "/a/";
$letter[1] = "/b/";
$letter[2] = "/c/";
$letter[3] = "/d/";
$letter[4] = "/e/";
$letter[5] = "/f/";
$letter[6] = "/g/";
$letter[7] = "/h/";
$letter[8] = "/i/";
$letter[9] = "/j/";
$letter[10] = "/k/";
$letter[11] = "/l/";
$letter[12] = "/m/";
$letter[13] = "/n/";
$letter[14] = "/o/";
$letter[15] = "/p/";
$letter[16] = "/q/";
$letter[17] = "/r/";
$letter[18] = "/s/";
$letter[19] = "/t/";
$letter[20] = "/u/";
$letter[21] = "/v/";
$letter[22] = "/w/";
$letter[23] = "/x/";
$letter[24] = "/y/";
$letter[25] = "/z/";

$image[0] = '<img src="'.$file_path.'images/titleletters/a.gif" border="0" height="40">';
$image[1] = '<img src="'.$file_path.'images/titleletters/b.gif" border="0" height="40">';
$image[2] = '<img src="'.$file_path.'images/titleletters/c.gif" border="0" height="40">';
$image[3] = '<img src="'.$file_path.'images/titleletters/d.gif" border="0" height="40">';
$image[4] = '<img src="'.$file_path.'images/titleletters/e.gif" border="0" height="40">';
$image[5] = '<img src="'.$file_path.'images/titleletters/f.gif" border="0" height="40">';
$image[6] = '<img src="'.$file_path.'images/titleletters/g.gif" border="0" height="40">';
$image[7] = '<img src="'.$file_path.'images/titleletters/h.gif" border="0" height="40">';
$image[8] = '<img src="'.$file_path.'images/titleletters/i.gif" border="0" height="40">';
$image[9] = '<img src="'.$file_path.'images/titleletters/j.gif" border="0" height="40">';
$image[10] = '<img src="'.$file_path.'images/titleletters/k.gif" border="0" height="40">';
$image[11] = '<img src="'.$file_path.'images/titleletters/l.gif" border="0" height="40">';
$image[12] = '<img src="'.$file_path.'images/titleletters/m.gif" border="0" height="40">';
$image[13] = '<img src="'.$file_path.'images/titleletters/n.gif" border="0" height="40">';
$image[14] = '<img src="'.$file_path.'images/titleletters/o.gif" border="0" height="40">';
$image[15] = '<img src="'.$file_path.'images/titleletters/p.gif" border="0" height="40">';
$image[16] = '<img src="'.$file_path.'images/titleletters/q.gif" border="0" height="40">';
$image[17] = '<img src="'.$file_path.'images/titleletters/r.gif" border="0" height="40">';
$image[18] = '<img src="'.$file_path.'images/titleletters/s.gif" border="0" height="40">';
$image[19] = '<img src="'.$file_path.'images/titleletters/t.gif" border="0" height="40">';
$image[20] = '<img src="'.$file_path.'images/titleletters/u.gif" border="0" height="40">';
$image[21] = '<img src="'.$file_path.'images/titleletters/v.gif" border="0" height="40">';
$image[22] = '<img src="'.$file_path.'images/titleletters/w.gif" border="0" height="40">';
$image[23] = '<img src="'.$file_path.'images/titleletters/x.gif" border="0" height="40">';
$image[24] = '<img src="'.$file_path.'images/titleletters/y.gif" border="0" height="40">';
$image[25] = '<img src="'.$file_path.'images/titleletters/z.gif" border="0" height="40">';

    $show_title = preg_replace($letter, $image, $create_image_title); // Create Title from image letters

I have updated this code to one of my hosts... [a href=\"http://www.quizzer.eu/test.php\" target=\"_blank\"]HERE[/a].

As you can see it outputs nothing!

I have tried reducing the amount of letters being replaced and it works fine with one. But obviously I want all of the letters replaced.

Thankyou for any assistance

- Luke [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]

Share this post

Link to post
Share on other sites
the problem with running a match like that is that it will run incrementally through your array of letters, so, when your "a" is replaced with an <img /> tag, it works great, but then, when your match runs for the letter "i", you end up replacing the letter within that tag, so it ends up with something like: <<img />mg />... this continues to happen for every letter within your tags, etc.

what you're going to have to do is modify your regex to match ONLY those letters that are not within tags.

Share this post

Link to post
Share on other sites
OK, I fully understand what is happening now by the way it is in a infinite loop.

I am quite advanced with PHP, but I have not really delved in to the use of preg_replace(); and ereg(); functions so the regex page is really quite confusing as to how I would use it to solve this problem.

If you could give me a further push in the right dierection that would be great.

Thanks again

- Luke

Share this post

Link to post
Share on other sites
You could use something like this instead...

working example
[a href=\"http://www.wisewood.org/projects/imagewithtext.php\" target=\"_blank\"]http://www.wisewood.org/projects/imagewithtext.php[/a]

This example gives you a text box to enter the text you want rendered into an image dynamically on the next page. You could always cut that bit out and use another variable instead to suit the setup of your site though.

Just stick whatever .ttf font file you want to use into the same dir as the script, and declare the name of the ttf file within the code.

if($wtd=="view") {
Header("Content-type: image/png"); //Tells the page this is an image. This must be put before any output.

$w = "250";  //Image Width

$h = "40";  //Image Height

$im = ImageCreate($w, $h);  //Start the image.

$background_color = imagecolorallocate($im, 255, 120, 0);  //Background color. RGB Values.

$text = ImageColorAllocate($im, 255, 255, 255);  //Text Color     RGB Values

ImageRectangle($im,0,0,$w - 1,$h - 1,$BorderColor);  // Create the background

ImageFtText ($im, 20, 0, 15, 30, $text, "wiffles.ttf", "$_POST[content]", array());

//Write the foreground Text

// 22=Font Size  0=Angle  1=Dist from top   10=Dist from left

ImagePng($im); //Draws the image

ImageDestroy($im);  //Finish the image.

} else {?>
<?php }?>

Share this post

Link to post
Share on other sites
Thanks for the idea Wisewood, It is a good idea, However I wish to continue on the replacing thread as I have taken so long creating the images for the title and I feel that it will be more effective than using a big rendered image.

Could anyone point me further in to the right direction as to how to how I can replace each individual letter to an image tag?


Share this post

Link to post
Share on other sites
OK, perfect solution then. I've only done part of the code to nudge you along, but you'll get the idea.

Firstly, change the characters in your variable for their corresponding ASCII character code (which can be found here [a href=\"http://www.lookuptables.com/)\" target=\"_blank\"]http://www.lookuptables.com/)[/a] and then replace the ascii character code with the appropriate image.

Sample code;


$comment = "The quick brown fox jumped over the lazy dog";
$comment = str_replace("A","A", str_replace("a","a",$comment));
$comment = str_replace("B","B", str_replace("b","b",$comment));
$comment = str_replace("C","C", str_replace("c","c",$comment));
$comment = str_replace("D","D", str_replace("d","d",$comment));
$comment = str_replace("E","E", str_replace("e","e",$comment));
$comment = str_replace("F","F", str_replace("f","f",$comment));
$comment = str_replace("G","G", str_replace("g","g",$comment));
$comment = str_replace("H","H", str_replace("h","h",$comment));
$comment = str_replace("I","I", str_replace("i","i",$comment));
$comment = str_replace("J","J", str_replace("j","j",$comment));
$comment = str_replace("K","K", str_replace("k","k",$comment));
$comment = str_replace("L","L", str_replace("l","l",$comment));
$comment = str_replace("M","M", str_replace("m","m",$comment));
$comment = str_replace("N","N", str_replace("n","n",$comment));
$comment = str_replace("O","O", str_replace("o","o",$comment));

$comment = str_replace("A","<img src=A.jpg>", str_replace("a","<img src=a.jpg>",$comment));

echo "$comment";


*code edited as what was there wasn't quite right before.

hmmmm... the character codes are showing up as their corresponding character on the forum lol.

try this
[a href=\"http://www.wisewood.org/projects/test_str_replace.txt\" target=\"_blank\"]http://www.wisewood.org/projects/test_str_replace.txt[/a]

Share this post

Link to post
Share on other sites
Hello again.

Thanks again for any help. I ended up making a code that converts the chanracters to HTML codes EG: #&105

Then I changed them to images using preg_replace.

Thank you again

- Luke ;)

- - Solved - -

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.