phpSensei Posted July 19, 2007 Share Posted July 19, 2007 Can someone help me with this? I know exactly how to write a preg_replace script, but dont know how to do the following... When a user posts a message, he can add the following stuff: <"b"><"/b"> Sorry, I had to put double quotes because this site reads it as a bold. <center></center> - tongue ;p - Wink :0 - Suprised How would you make it so the script finds a string wrapped by the bold, and center, and to find the smilies, and replace them with something of my own. And please dont give me answers that dont relate to anything. Link to comment https://forums.phpfreaks.com/topic/60874-solved-preg_replace/ Share on other sites More sharing options...
akitchin Posted July 19, 2007 Share Posted July 19, 2007 for simple replacements like these, i think you'd be better off using str_replace(). all you need to do is specify a search string, a replacement string, and the subject to search on. it's well-suited for smilies in particular. the regex side of things need only come in for complicated tag patterns, where you might be missing a closing tag or opening tag and don't want to propagate those open tags past the string you're outputting. for the most part, this isn't a huge issue if your posters are responsible. Link to comment https://forums.phpfreaks.com/topic/60874-solved-preg_replace/#findComment-302880 Share on other sites More sharing options...
phpSensei Posted July 19, 2007 Author Share Posted July 19, 2007 <?php $sql="SELECT * FROM frontpage ORDER BY id DESC LIMIT 3"; $result=mysql_query($sql); while($row = mysql_fetch_array($result)) { $happy = str_replace("", "happy", $row['message']); echo ' <table width="80%" border="0" align="center"> <tr> <td colspan="2"><div align="center" class="style6"> <h3>' . $malestr . '</h3> </div></td> </tr> <tr align="center" valign="top"> <td colspan="2"><div align="center" class="style6"> <div align="center"><span class="style8">' . $row['message'] . ' <br> <br> </span> <hr size="2"> </div> </div></td> </tr> <tr> <td width="100%" height="21"><div align="left"><span class="style5"><img src="files/images/admin_avy.png" width="12" height="12"> user: -- </span> <img src="files/images/comment.png"><span class="style5"> Comments ( 0 ) <img src="files/images/written.png" width="12" height="12"> Date Written:<em>' . $row['date'] .'</em></span></div></td> <td width="0%" class="style5"><div align="left"></div></td> </tr> </table>'; } ?> I did this, but it doesnt work. Link to comment https://forums.phpfreaks.com/topic/60874-solved-preg_replace/#findComment-302892 Share on other sites More sharing options...
phpSensei Posted July 19, 2007 Author Share Posted July 19, 2007 Screw what I said, Heres the thing... $happy = str_replace("", '<img src="files/smilies/happy.gif" width="18" height="24">', $row['message']); echo $happy; What if I want to replace more than one thing in the same variable? Link to comment https://forums.phpfreaks.com/topic/60874-solved-preg_replace/#findComment-302902 Share on other sites More sharing options...
akitchin Posted July 20, 2007 Share Posted July 20, 2007 you can use arrays, matching their order with the order of the replacements: $replace = array('1', '2', '3', '4'); $with = array('one', 'two', 'three', 'four'); $new = str_replace($replace, $with, $old); Link to comment https://forums.phpfreaks.com/topic/60874-solved-preg_replace/#findComment-303656 Share on other sites More sharing options...
phpSensei Posted July 20, 2007 Author Share Posted July 20, 2007 THANNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNKYYYYYYYYYYYYOU SOOOOOOOOOOOOOO MUCCCCCCH K I wont ever attempt to do something like that again in big, but thankyou thankyou... Link to comment https://forums.phpfreaks.com/topic/60874-solved-preg_replace/#findComment-303662 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.