kilz 0 Report post Posted September 28, 2004 how to replace html/php code using php function. . . I know that -- - -I use htmlspecialchars but I would like the replace them with bbcode, such as . . . please help. . . Share this post Link to post Share on other sites
overture 0 Report post Posted September 28, 2004 so instead of <B></B> you want it to be replaced with . you could use the str_replace() function. it goes like this: $message = $_POST['message'];//replace <b> with [b] in the message field from the form.$message= str_replace("<b>","[b]",$message);$message= str_replace("</b>","[/b]",$message); you can do that for every tag or you can do it using an array within the str_replace() function like this: str_replace(array('<b>','</b>','<i>','</i>'),array('[b]','[/b]','[i]','[/i]'),$message); i am not sure if this is what you want exactly, but there it is anyways. Share this post Link to post Share on other sites
kilz 0 Report post Posted September 28, 2004 thanks a lot, I got an idea now, but I don't know if I need to use a function or not. . .what a function would be like??function bbcode () {. . .}I've never writted any function by myself, only know php built in function. Share this post Link to post Share on other sites
overture 0 Report post Posted September 28, 2004 You could use a function but i don't think that you would have to. You can just put that in your php script/s, enter all of the tags you want to replace and what you want to replace them with in the array and they will all be converted. is there anything you need? Share this post Link to post Share on other sites
Hercco 0 Report post Posted October 4, 2004 thanks a lot, I got an idea now, but I don't know if I need to use a function or not. . . what a function would be like?? function bbcode () { . . . } I've never writted any function by myself, only know php built in function. <{POST_SNAPBACK}> The function wouild be something like this: function bbcode($message){ str_replace(array('<b>','</b>','<i>','</i>'),array('[b]','[/b]','[i]','[/i]'),$message); //add other operations you want to do here return $message;} And you would call it like this, assuming that you have the stuff to convert in $msg variable. $msg = bbcode($msg); That would replace the contents of the $msg variable with that created by the function. Quite easy isn't it? Share this post Link to post Share on other sites
kilz 0 Report post Posted October 19, 2004 I got troubles with the tag!! function bbcode($a) { $a = nl2br(htmlspecialschars($a)); $a = str_replace (array ("[/url]", ""), array ("<a href=>", "</a>"), $a); return $a; } its not working!!! help; please Share this post Link to post Share on other sites
lhunath 0 Report post Posted October 19, 2004 if you run your code $a = str_replace (array ("[url]", "[/url]"), array ("<a href=>", "</a>"), $a); on test, then the output will be:[.url=test.html]test</a>(Ignore the dot in the thing, it's just to make sure this forum shows the url stuff instead of making it a link..)Look at your code. It will replace with <a href=>. Two problems:1. There is no http://forums.xisto.com/in your string! Look, only an , so no replace will be made.2. Should any replace be made, how is your PHP going to know that it has to put the test.html inbetween the = and the > in your Anchor tag?Easiest is just using some regex.. function bbcode($a) {$sRes=eregi_replace($a,"\[url=(.*?)\]","<a href=\"\1\">");return $a;} ( I don't know for sure but you might have to escape the escape slashes themselves as well for the PHP engine to pass them to the regex... in that case, (if it doesn't work like this), use this: eregi_replace($a,"\\" ,"<a href=\"\\1\">") )Php Regex Manual The best regex tutorial Share this post Link to post Share on other sites