note 71455 added to function.preg-replace

From: Date: Wed, 29 Nov 2006 22:13:36 +0000
Subject: note 71455 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-120169@lists.php.net to get a copy of this message
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! ---- Server IP: 217.174.203.10 Probable Submitter: 130.79.213.50 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/71455 Del: integrated -- https://master.php.net/note/delete/71455/integrated Del: useless -- https://master.php.net/note/delete/71455/useless Del: bad code -- https://master.php.net/note/delete/71455/bad+code Del: spam -- https://master.php.net/note/delete/71455/spam Del: non-english -- https://master.php.net/note/delete/71455/non-english Del: in docs -- https://master.php.net/note/delete/71455/in+docs Del: other reasons-- https://master.php.net/note/delete/71455 Reject -- https://master.php.net/note/reject/71455 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120169) next »