View Full Version : str_replace help!

04-20-2003, 09:52 AM

I am currently writing a news script that retrievs news from a db.

Now I am making it so like if you do ; ) it makes a ;) and so forth, its working great, but can anybody tell me how do make it so if i like do this:

[img=http://url here]

How can i make it so that the script then gets the image?


04-20-2003, 11:25 AM
For this you will need regex. I am FAR to regex challenged to make anything, but I could try...

$message = "Your message with [img=http://mysite.com/myimage.gif] image tags";

$regex = "\\[img=(.*)\\]";

$img = preg_replace($regex, "<img src=\"$1\" border="0" />", $message);

It may or may not work :rolleyes: , But I hope this helps.

04-20-2003, 11:27 AM
thanks i will give it a go!

can anybody also give a little more description into this?



04-20-2003, 11:43 AM
I can give you a bit of a description of what regex is:

Regex, or Regular Expressions, are used in PHP (and many other languages) to match a string pattern...

Take this example:


Will match 1 character that is: a-z, A-Z or 0-9.

There are many other operators:

? Will match 1 or 0 instances of a pattern:

[a-z]? Will match 1 or 0 characters that are a-z. If there is more than one instance, it will not match it.

And +, {,x},{x,},{x},{x,x}

I hope this gives you a brief I idea. I cant think of anywhere to look for info.

04-20-2003, 01:29 PM
I kinda cheated when I did this, I did it the very long way around, but I could understand it.

$replace[0] = "/:p/";
$replace[1] = "/:\)\)/";
$replace[2] = "/:D/";
$replace[3] = "/:\?/";
$replace[4] = "/\;\)/";
$replace[5] = "/:\)/";
$replace[6] = "/:o/";
$replace[7] = "/:-D/";
$replace[8] = "/8\)/";
$replace[9] = "/:\\\'\(/";
$replace[10] = "/:eek:/";
$replace[11] = "/:evil:/";
$replace[12] = "/:frown:/";
$replace[13] = "/:mad:/";
$replace[14] = "/:\|/";
$replace[15] = "/:\\$/";
$replace[16] = "/:rolleyes:/";
$replace[17] = "/:\(/";
$replace[18] = "/:twist:/";
$replace[19] = "/;\)/";

$replaced[0] = "<img src='images/icon_razz.gif' title='Razz' alt='Razz'/>";
$replaced[1] = "<img src='images/icon_lol.gif' title='LOL' alt='LOL'/>";
$replaced[2] = "<img src='images/icon_biggrin.gif' title='Big grin' alt='Big grin'/>";
$replaced[3] = "<img src='images/icon_confused.gif' title='Confused' alt='Confused'/>";
$replaced[4] = "<img src='images/icon_wink.gif' title='Wink' alt='Wink'/>";
$replaced[5] = "<img src='images/icon_smile.gif' title='Smile' alt='Smile' />";
$replaced[6] = "<img src='images/icon_eek.gif' title='Eek' alt='Eek' />";
$replaced[7] = "<img src='images/icon_cheesygrin.gif' title='Cheesy grin' alt='Cheesy grin' />";
$replaced[8] = "<img src='images/icon_cool.gif' title='Cool' alt='Cool' />";
$replaced[9] = "<img src='images/icon_cry.gif' title='Cry' alt='Cry' />";
$replaced[10] = "<img src='images/icon_eek.gif' title='Eek' alt='Eek' />";
$replaced[11] = "<img src='images/icon_evil.gif' title='Evil' alt='Evil' />";
$replaced[12] = "<img src='images/icon_frown.gif' title='Frown' alt='Frown' />";
$replaced[13] = "<img src='images/icon_mad.gif' title='Mad' alt='Mad' />";
$replaced[14] = "<img src='images/icon_neutral.gif' title='Neutral' alt='Neutral' />";
$replaced[15] = "<img src='images/icon_redface.gif' title='Red face' alt='Red face' />";
$replaced[16] = "<img src='images/icon_rolleyes.gif' title='Roll eyes' alt='Roll eyes' />";
$replaced[17] = "<img src='images/icon_sad.gif' title='Sad' alt='Sad' />";
$replaced[18] = "<img src='images/icon_twisted.gif' title='Twisted' alt='Twisted' />";
$replaced[19] = "<img src='images/icon_wink.gif' title='Wink' alt='Wink' />";

$entry = preg_replace($replace,$replaced,$entry);

<edit>forgot to disable smilies</edit>
<edit part="2">Forum's stripped out slashes now, so you'll get errors :rolleyes: Add a backslash before the ),(,' and ? You might have to add more than one backslash before the '</edit>

04-20-2003, 01:33 PM
ok thanks!

but how would i make this so it automatically finds the : ) and stuff in the database