PHP - Bbcode Parser Problem
Everything works fine, unless I add this stupid thing to get rid of people using HTML
Code: [Select] $text = pun_htmlspecialchars($text); Once I add that to my function, no bbcodes work at all? But I cant use html.. (which is good) but I need to beable to use BBCODE, and parse hackers from using html also, any help? MY CODE absolutely destroyed the forum page here it is: http://pastebin.com/jv7m47kn Similar TutorialsI was wonder how I could make some BBcode for my messaging system I made for a website. I made some simple ones like just by replace [ red ] with font color etc. I want to know how to do something like this: [ url = http://google . ca] Google [ / url ] without the spaces. ~AJ Ok, so i'm working on my forum software in PHP to both get some knowledge out of this and accomplish something in the run. While adding a BBCode parser, I ran into some trouble. This is what happened: http://ebulletin.ballindesign.com/message.php?id=1 As you can see, a <br /> tag shows up after EVERY line... Here is my BBCode parser: <?php function is_odd($intNumber) { if ($intNumber % 2 == 0 ) return true; else return false; } function badlink($link, $prefix) { if ($prefix == "mailto:") { if (strpos($link, "@") === FALSE || strpos($link, ".", (strpos($link, "@")+2)) === FALSE || substr_count($link, "@") > 1 || strpos($link, "@") == 0) { return 1; } } if (strpos($link, ".") == 0 || strpos($link, ".") == strlen($link) || (strpos($link, "/") < strpos($link, ".") && strpos($link, "/") !== FALSE)) { return 1; } }; function setlinks($r, $prefix) { if (substr($r, 0, strlen($prefix)) == $prefix) { $r = "\n".$r; } $r = str_replace("<br>".$prefix, "<br>\n".$prefix, $r); $r = str_replace(" ".$prefix, " \n".$prefix, $r); while (strpos($r, "\n".$prefix) !== FALSE) { list($r1, $r2) = explode("\n".$prefix, $r, 2); if (strpos($r2, " ") === FALSE && strpos($r2, "<br>") === FALSE) { if ($prefix != "mailto:") { $target = ' target="_blank"'; } else { $target = ""; } if (strpos($r2, ".") > 1 && strpos($r2, ".") < strlen($r2) && badlink($r2, $prefix) != 1) { $r = $r1.'<a href="'.$prefix.$r2.'"'.$target.'><font size="2" color="blue">'.$prefix.$r2.'</font></a>'; } else { $r = $r1.$prefix.$r2; } } else { if (strpos($r2, " ") === FALSE || ( strpos($r2, " ") > strpos($r2, "<br>") && strpos($r2, "<br>") !== FALSE)) { list($r2, $r3) = explode("<br>", $r2, 2); if (badlink($r2, $prefix) != 1) { $r = $r1.'<a href="'.$prefix.$r2.'"'.$target.'><font size="3" color="blue">'.$prefix.$r2.'</font></a><br>'.$r3; } else { $r = $r1.$prefix.$r2.'<br>'.$r3; } } else { list($r2, $r3) = explode(" ", $r2, 2); if (strpos($r2, ".") > 1 && strpos($r2, ".") < strlen($r2) && badlink($r2, $prefix) != 1) { $r = $r1.'<a href="'.$prefix.$r2.'"'.$target.'><font size="3" color="blue">'.$prefix.$r2.'</font></a> '.$r3; } else { $r = $r1.$prefix.$r2.' '.$r3; } } } } return $r; }; function BBCode($r) { $r = trim($r); $r = htmlentities($r); $r = str_replace("\r\n","<br>",$r); $r = str_replace("\r\n","</br>",$r); $r = str_replace("[b]","<b>",$r); $r = str_replace("[/b]","</b>",$r); $r = str_replace("[img]http://","<img src='",$r); $r = str_replace("[/img]","'>",$r); $r = str_replace("[IMG]http://","<img src='",$r); $r = str_replace("[/img]","'>",$r); $r = str_replace("[s]","<s>",$r); $r = str_replace("[/s]","</s>",$r); $r = str_replace("[ul]","<ul>",$r); $r = str_replace("[/ul]","</ul>",$r); $r = str_replace("[list][li]","<li>",$r); $r = str_replace("[/li][/list]","</li>",$r); $r = str_replace("[ol]","<ol>",$r); $r = str_replace("[/ol]","</ol>",$r); $r = str_replace("[quote]","<br /><table width='80%' bgcolor='#ffff66' align='center'><tr><td style='border: 1px dotted black'><font color=black><b>Quote:<br></b>",$r); $r = str_replace("[/quote]","</font></td></tr></table>",$r); $r = str_replace("[i]","<i>",$r); $r = str_replace("[/i]","</i>",$r); $r = str_replace("[u]","<u>",$r); $r = str_replace("[/u]","</u>",$r); $r = str_replace("[spoiler]",'[spoiler]<font bgcolor ="#000000" color="#DDDDDD">',$r); $r = str_replace("[/spoiler]","</font>[/spoiler]",$r); //set [link]s while (strpos($r, "[link=") !== FALSE) { list ($r1, $r2) = explode("[link=", $r, 2); if (strpos($r2, "]") !== FALSE) { list ($r2, $r3) = explode("]", $r2, 2); if (strpos($r3, "[/link]") !== FALSE) { list($r3, $r4) = explode("[/link]", $r3, 2); $target = ' target="_blank"'; if (substr($r2, 0, 7) == "mailto:") { $target = ""; } $r = $r1.'<a href="'.$r2.'"'.$target.'><font size="3" color="blue">'.$r3.'</font></a>'.$r4; } else { $r = $r1."[link\n=".$r2."]".$r3; } } else { $r = $r1."[link\n=".$r2; } } $r = str_replace("[link\n=","[link=",$r); ////[link] ///default url link setting $r = setlinks($r, "http://"); $r = setlinks($r, "https://"); $r = setlinks($r, "ftp://"); $r = setlinks($r, "mailto:"); ////links ///emoticons $r = str_replace(":)",'<img src="images/smilie.gif">',$r); $r = str_replace(":(",'<img src="images/sad.gif">',$r); $r = str_replace(":angry:",'<img src="images/angry.gif">',$r); $r = str_replace(":D",'<img src="images/biggrin.gif">',$r); $r = str_replace(":blink:",'<img src="images/blink.gif">',$r); $r = str_replace(":blush:",'<img src="images/blush.gif">',$r); $r = str_replace("B)",'<img src="images/cool.gif">',$r); $r = str_replace("<_<",'<img src="images/dry.gif">',$r); $r = str_replace("^_^",'<img src="images/happy.gif">',$r); $r = str_replace(":huh:",'<img src="images/confused.gif">',$r); $r = str_replace(":lol:",'<img src="images/laugh.gif">',$r); $r = str_replace(":o",'<img src="images/ohmy.gif">',$r); $r = str_replace(":fear:",'<img src="images/fear.gif">',$r); $r = str_replace(":rolleyes:",'<img src="images/rolleyes.gif">',$r); $r = str_replace(":sleep:",'<img src="images/sleep.gif">',$r); $r = str_replace(":p",'<img src="images/tongue.gif">',$r); $r = str_replace(":P",'<img src="images/tongue.gif">',$r); $r = str_replace(":unsu ",'<img src="images/unsure.gif">',$r); $r = str_replace(":wacko:",'<img src="images/wacko.gif">',$r); $r = str_replace(":wink:",'<img src="images/wink.gif">',$r); $r = str_replace(":wub:",'<img src="images/wub.gif">',$r); $r = trim($r); return $r; } ?> Hi there, This isn't really a bbcode parser problem, but it kind of is. And I am awful at explaining things, as you have noticed. The problem is that I'd like to allow input for the main page of a site. But the part has this structu Code: [Select] <div class='mainheader'>TITLE</div> <div class='content'> The content of the webpage blabla</div> I'd like this whole page to be changeable, so also the title and the content. But how can I ensure that people use the content div after the mainheader div? I would like it if it were possible that they only needed to 'select' the header, how can I do this? Thanks in advance! hello dear Freaks
i am currently musing bout the portover of a python bs4 parser to php - working with the simplehtmldom-parser / pr the DOM-selectors... (see below). The project: for a list of meta-data of wordpress-plugins: - approx 50 plugins are of interest! but the challenge is: i want to fetch meta-data of all the existing plugins. What i subsequently want to filter out after the fetch is - those plugins that have the newest timestamp - that are updated (most) recently. It is all aobut acutality... https://wordpress.org/plugins/participants-database ....and so on and so forth.
https://wordpress.org/plugins/wp-job-manager we have the following set of meta-data for each wordpress-plugin: Version: 1.9.5.12 installations: 10,000+ WordPress Version: 5.0 or higher Tested up to: 5.4 PHP Version: 5.6 or higher Tags 3 Tags:databasemembersign-up formvolunteer Last updated: 19 hours ago
the project consits of two parts: the looping-part: (which seems to be pretty straightforward). the parser-part: where i have some issues - see below. I'm trying to loop through an array of URLs and scrape the data below from a list of wordpress-plugins. See my loop below- as a base i think it is good starting point to work from the following target-url:
plugins wordpress.org/plugins/browse/popular with 99 pages of content: cf ...
the Output of text_nodes: ['Version: 1.9.5.12', 'Active installations: 10,000+', 'Tested up to: 5.6 '] but if we want to fetch the data of all the wordpress-plugins and subesquently sort them to show the -let us say - latest 50 updated plugins. This would be a interesting task:
first of all we need to fetch the urls then we fetch the information and have to sort out the newest- the newest timestamp. Ie the plugin that updated most recently List the 50 newest items - that are the 50 plugins that are updated recently ..
we have the following set see here the Soup_ soup = BeautifulSoup(r.content, 'html.parser') target = [item.get_text(strip=True, separator=" ") for item in soup.find( "h3", class_="screen-reader-text").find_next("ul").findAll("li")[:8]] head = [soup.find("h1", class_="plugin-title").text] new = [x for x in target if x.startswith( ("V", "Las", "Ac", "W", "T", "P"))] return head + new with ThreadPoolExecutor(max_workers=50) as executor1: futures1 = [executor1.submit(parser, url) for url in allin] for future in futures1: print(future.result())
see the formal output Quote
background: https://stackoverflow.com/questions/61106309/fetching-multiple-urls-with-beautifulsoup-gathering-meta-data-in-wp-plugins Well - i guess that we c an do this with the simple DOM Parser - here the seclector reference. https://stackoverflow.com/questions/1390568/how-can-i-match-on-an-attribute-that-contains-a-certain-string
look forward to any hint and help.
have a great day Edited May 3, 2020 by dil_bertwhen posting a new message on my forum and it contains a url. How do i automatically add the bbcode url tags to a url in the message? ok bare with me if this sounds stupid. How do i get it so that bbcode can be added within a textarea tag? im assuming it is a lot of str_replace. is there a tutorial on this somewhere? I'm trying to work on BBCode for a forum. I have this: Code: [Select] <?php $bb_Code = array( '[ code]' => '<code>', '[ /code]' => '</code>' ); foreach ($bb_Code as $value => $replace) { $text = str_replace($value, $replace, $text); } ?> Question is.. how would I get it so when I insert information into the database it gets cleaned (strip_tags, etc.) but doesn't disturb what is in the [ code ] tags? So basically it makes the html, etc. in the [ code ] tags just plain text? Thanks in advanced. Hey, I need help with my BBCode function... im trying to allow it to embed you tube videos but im kinda confused as to how i can do it when i need the link to be in the bbcode html ouput twice with my current method. Here is my function: http://www.paste.to/MzE3Mg== I could not post the script as it was messing up with this site's BBcode. Any one can help me please ? Thankss Hello guys, i need help with my BBcoding ... Its about browser game.. but i need someone who has some time to listen me about how things work and than help me.. every time i put : when sending message ingame i automaticly get smiley ... but please if anyone have time let me know Best Regards Right now i used the following code to convert bbcode to html/php. i also need to incorporate QUOTE=name and URLS as well..how do i do that? Code: [Select] function bbcode_format ($str) { $str = htmlentities($str); $start = array( '/\[b\](.*?)\[\/b\]/is', '/\[i\](.*?)\[\/i\]/is', '/\[u\](.*?)\[\/u\]/is', '/\[a\=(.*?)\](.*?)\[\/a\]/is', '/\[color\=(.*?)\](.*?)\[\/color\]/is', '/\[img\](.*?)\[\/img\]/is', '/\[header\](.*?)\[\/header\]/is' ); $finish = array( '<strong>$1</strong>', '<em>$1</em>', '<u>$1</u>', '<a href="$1">$2</a>', '<span style="color: $1">$2</span>', '<img src="$1" />', '<span class="h3">$1</span>' ); $str = preg_replace ($start, $finish, $str); return $str; } I have this function: function bbcode($input){ $input = strip_tags($input); $input = htmlentities($input); $search = array( '/\[b\](.*?)\[\/b\]/is', '/\[i\](.*?)\[\/i\]/is', '/\[u\](.*?)\[\/u\]/is', '/\[img\](.*?)\[\/img\]/is', '/\[url=http://(.*?)\](.*?)\[\/url\]/is', '/\[color=(.*?)\](.*?)\[\/color\]/is', '/\[yt\](.*?)\[\/yt\]/is', ); $replace = array( '<b>$1</b>', '<i>$1</i>', '<u>$1</u>', '<img src="$1" style="border:0">', '<a href="$1">$2</a>', '<font style="color:$1">$2</font>', '<object width="660" height="405"> <param name="movie" value="'.str_replace("watch?v=", "v/", $1).'" /> <param name="allowFullScreen" value="true" /> <param name="allowscriptaccess" value="always" /><embed type="application/x-shockwave-flash" width="660" height="405" src="'.str_replace("watch?v=", "v/", $1).'" allowscriptaccess="always" allowfullscreen="true"></embed> </object>', ); return nl2br(preg_replace($search,$replace,$input)); } There is an error on code: <param name="movie" value="'.str_replace("watch?v=", "v/", $1).'" /> and on: <param name="allowscriptaccess" value="always" /><embed type="application/x-shockwave-flash" width="660" height="405" src="'.str_replace("watch?v=", "v/", $1).'" allowscriptaccess="always" allowfullscreen="true"> How can i use str_replace in this situation ? :S Code: [Select] [b][i][u] test test test test test test test test test test test test test test test. [/b][/i]/[u] If I need to cut the first five words, the bbcode closing tags will be remove. This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=354638.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=348145.0 Code: [Select] $txt = preg_replace( "#\[yt\]http://www.youtube.com/watch?v=(.+?)\[/yt\]#is", '<embed src="http://www.youtube.com/v/\\1" type="application/x-shockwave-flash" wmode="transparent" width="512" height="313" allowfullscreen="true" />', $txt ); Okay this is my code just for inserting it into HTML, the problem is whenever I try to use: Code: [Select] [yt]http://www.youtube.com/watch?v=l1G7TJD6Xu0[/yt] On my form to enter it does not work? But if I use Code: [Select] $txt = preg_replace( "#\[yt\](.+?)\[/yt\]#is", '<embed src="http://www.youtube.com/v/\\1" type="application/x-shockwave-flash" wmode="transparent" width="512" height="313" allowfullscreen="true" />', $txt ); Code: [Select] [yt]l1G7TJD6Xu0[/yt] It works. But I want to use the full URL of the youtube, so I don't want my users to copy/paste just the "l1G7TJD6Xu0" Code , any help? Thank you This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=317329.0 I'm trying to work on BBCode for a forum. Code: [Select] <?php $bb_Code = array( '[ code]' => '<code>', '[ /code]' => '</code>' ); foreach ($bb_Code as $value => $replace) { $text = str_replace($value, $replace, $text); } ?> Question is.. how would I get it so when I insert information into the database it gets cleaned (strip_tags, etc.) but doesn't disturb what is in the [ code ] tags? So basically it makes the html, etc. in the [ code ] tags just plain text? Thanks in advanced. hi, i have the following which parses the quote tag and replaces it with a span $bb[] = "#\[quote\](.*?)\[/quote\]#si"; $html[] = "<span class='quote'>\\1</div>"; what i would like however is for the bbcode to be Quote from: xxxx where xxxx is the name of the person you are quoting. so how would i parse this? I also need the same for font color but if someone can provide a solution to this one then i think i could work it out. |