wrathican Posted August 11, 2008 Share Posted August 11, 2008 hey peeps i have made a simple CMS and im trying to add a bit more functionality it. i'm trying to do this by adding a simple text formatter. as it stands im having to create my own bbcode functions because my host will not add the PEAR BBCodeParser. ive got the whole insert the tags into the textarea sorted. the only problem is the parser. this is what i'm working on at the moment, but it really doesn't output what i expect: <?php function bbCode($string) { //formatting $string = ereg_replace("[b]", "<strong>", $string); $string = ereg_replace("[/b]", "</strong>", $string); $string = ereg_replace("[u]", "<u>", $string); $string = ereg_replace("[/u]", "</u>", $string); $string = ereg_replace("[i]", "<i>", $string); $string = ereg_replace("[/i]", "</i>", $string); //links //images $string = ereg_replace("[img]", "<img src=\"", $string); $string = ereg_replace("[/img]", "\" alt=\"Image\" />", $string); return $string; } ?> when testing it it outputs this: [<stron<" alt="Image" />" alt="Image" />" alt="Image" /> src=">] while it should just output <strong> This is the test in running: <?php $string = "[b]"; $string = bbCode($string); $string = htmlentities($string); echo $string; ?> any ideas, suggestions? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/119200-text-replacement/ Share on other sites More sharing options...
effigy Posted August 11, 2008 Share Posted August 11, 2008 Square brackets are metacharacters that create character classes, so they need to be escaped to match literals: \[b\]. Additionally, do not use regex when working with static strings--use str_replace. Signed, A peep. Quote Link to comment https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-613922 Share on other sites More sharing options...
DarkWater Posted August 11, 2008 Share Posted August 11, 2008 Square brackets are metacharacters that create character classes, so they need to be escaped to match literals: \[b\]. Additionally, do not use regex when working with static strings--use str_replace. Signed, A peep. LOL I just laughed pretty hard at that Peeps thing. Quote Link to comment https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-613924 Share on other sites More sharing options...
wrathican Posted August 11, 2008 Author Share Posted August 11, 2008 yeah me too.. thanks for the advice. i didn't know about the literals. learn something new. especially with the language.. Quote Link to comment https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-613928 Share on other sites More sharing options...
discomatt Posted August 11, 2008 Share Posted August 11, 2008 Square brackets are metacharacters that create character classes, so they need to be escaped to match literals: \[b\]. Additionally, do not use regex when working with static strings--use str_replace. Signed, A peep. Just for educational purposes, the reason for doing this is that regex is very slow compared to PHP's build in string functions... usually around 30-40x slower for the most basic comparisons. Quote Link to comment https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-613936 Share on other sites More sharing options...
wrathican Posted August 12, 2008 Author Share Posted August 12, 2008 thanks for the info. however i'm still having a bit of trouble with creating the function. this is what i have: <?php function bbCode($string) { $bbcodes = array( '\[b\]', '\[/b\]', '\[i\]', '\[/i\]', '\[u\]', '\[/u\]' ) $htmlcodes = array( '<strong>', '</strong>', '<em>', '</em>', '<u>', '</u>' ) $string = str_replace($bbcodes, $htmlcodes, $string); return $string; } ?> i'm still running the same text and i am having no luck with it. Am i doing something wrong? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-614317 Share on other sites More sharing options...
JasonLewis Posted August 12, 2008 Share Posted August 12, 2008 You don't need to escape the square brackets with str_replace(). Only regular expressions would require that. Also, you're missing two semi-colons ( ; ) after your arrays. Quote Link to comment https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-614326 Share on other sites More sharing options...
wrathican Posted August 12, 2008 Author Share Posted August 12, 2008 ok i removed the escaping and added the semi colons and the output is '3' here is my new source: function bbCode($string) { $bbcodes = array( '[b]', '[/b]', '[i]', '[/i]' ); $htmlcodes = array( '<strong>', '</strong>', '<em>', '</em>' ); $string = str_replace($bbcodes, $htmlcodes, $string); return $string; } testing script: <?php include 'functions.php'; $string = "[b]bllaaaahhhhhh[/b]"; $string = bbCode($string); $string = htmlentities($string); echo $string; ?> help? im an unhappy bunny Quote Link to comment https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-614955 Share on other sites More sharing options...
effigy Posted August 12, 2008 Share Posted August 12, 2008 I get: <strong>bllaaaahhhhhh</strong>. Quote Link to comment https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-614958 Share on other sites More sharing options...
wrathican Posted August 12, 2008 Author Share Posted August 12, 2008 hmm it works now, one question, how would i parse links? links would ideally look like this in BBCode: Link to blah is there a way of getting the contents of the link and the text for the link then wrapping them appropriately? i was going to do it like so: add '[url=' to my array of bbcodes then replace it with '<a href="' then do a similar thing for the end.. :S Thanks Quote Link to comment https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-615002 Share on other sites More sharing options...
JasonLewis Posted August 13, 2008 Share Posted August 13, 2008 Search the Regex within PHP board, you should turn up some good results. Quote Link to comment https://forums.phpfreaks.com/topic/119200-text-replacement/#findComment-615470 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.