note 71455 deleted from function.preg-replace by colder
| From: | colder@php.net | Date: | Mon, 18 Jun 2007 20:06:27 +0000 |
| Subject: | note 71455 deleted from function.preg-replace by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127712@lists.php.net to get a copy of this message | ||
Note Submitter: godferthomy at hotmail dot fr
----
well in your code I see 2 errors, as I'm quite a newbie, all I can tell you is the path to
follow, can't explain more.
this is one of your lines
<?php
$text=preg_replace('0','<img src=images/num1/0.gif>');
?>
you need to place delimiters at the beginning and the end of the seek string, and in 3rd parameter
you need to tell the string that is analysed, in this case it is $text
<?php
$text=preg_replace('/0/','<img src=images/num1/0.gif>',$text);
?>
then, then way you use preg_replace is pointless, and very costful.
you can just use
<?php
$text=str_replace('0','<img src="images/num1/0.gif">',$text);
?>
but then you can use wisely preg_replace to avoid 12 lines that are almost the same.
<?php
$text=preg_replace('/([0-9]+)/si','<img
src="images/num1/\\1.gif">',$text);
?>
this will change every packet of numbers (THENUMBER) in the text by <img
src="images/num1/THENUMBER.gif">
though this is a bit strict since you won't be able to place any number in your text without
making it a picture..
so you might use a synthax, such as bbcode, ie. [img=THENUMBER]
<?php
$text=preg_replace('/\[img\=([0-9]+)\]/si','<img
src="images/num1/\\1.gif">',$text);
?>
this will change every packet of numbers ([img=THENUMBER]) in the text by <img
src="images/num1/THENUMBER.gif">
else you can change anything like [img=THETEXT] tag simply that way:
<?php
$text=preg_replace('/\[img\=(.*?)\]/si','<img
src="images/num1/\\1.gif">',$text);
?>
it will change any [img=THETEXT] in the text by <img src="images/num1/THETEXT.gif">
where THETEXT is anything inside this img tag.
I can't really explain you how to use the synthax since I'm not sure of my explanation.. I
prefer not to mistake you.
Also an HTML suggestion..
put your tags' attributes between double quotes.. that way
<img src="meow">
Good luck!