[SOLVED]Combining Two Functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

[SOLVED]Combining Two Functions

Post by blacksnday »

I have spent the last 5 hours trying to figure this one out and have failed,
so I come here for help :)

I'm working on creating custom BBCode that filters out all html and
only allows the tags I define. Once a user presses the submit button they
then see their post, which is the part that IS working.
I simply use the $_POST['formfieldname']
For a quick example of what I am talking about, make a test post at
http://www.vilificnews.info/dev/testsubmit.php

Ok, now the trouble I am having is when the article is pulled from the database.
I can't seem to get that to work alongside with my bb code replace.

A sample of a function that pulls the article from the database:

Code: Select all

function one_random_news_entry() { 
$result = mysql_unbuffered_query("SELECT * FROM news ORDER BY RAND() LIMIT 1");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$newposts = array ( $row['name'] => "Name", $row['lova'] => "Past Loves", $row['news'] => "Bash" );
foreach ( $newposts as $postfield => $postinput )
{
echo "<b>$postinput:</b>  $postfield<br>";
}
echo "<p>&nbsp;</p>";
}
}
The function I am trying to use to allow for the custom tags to be shown

Code: Select all

function submitTags($html, $media=0, $font=0)
{
    $html = preg_replace('#</?.*?\>#i','',$html); 
    $html = preg_replace('#<script[^>]*?>.*?</script>#i','',$html); 
    $html = htmlspecialchars($html);

 if($media){ 
    $html = preg_replace("#\[link\](.*?)\[/link\]#i", "<a href=\"\\1\" target=\"_blank\" title=\"bashmyex.com Bash Your Ex Girlfriend or Boyfriend Link!\">\\1</a>", $html);
    $html = preg_replace("#\[img\](.*?)\[/img\]#i", "<img src=\"\\1\" border=\"0\" alt=\"bashmyex.com Bash Your Ex Girlfriend or Boyfriend Pic!\">", $html);
    $html = preg_replace("#\[img url=(.*?)\](.*?)\[/img\]#i", "<a href=\"\\1\" target=\"_blank\"><img src=\"\\2\" border=\"0\" alt=\"bashmyex.com Bash Your Ex Girlfriend or Boyfriend Pic!\"></a>",  $html);
}else{
    $html = preg_replace("#\[link\](.*?)\[/link\]#i", "", $html);
    $html = preg_replace("#\[img\](.*?)\[/img\]#i", "", $html);
    $html = preg_replace("#\[img url=(.*?)\](.*?)\[/img\]#i", "", $html);
}
     
 if($font)
    $html = preg_replace("#\[br\]#i", "<br>", $html);
    $html = preg_replace("#\[center\](.*?)\[/center\]#i", "<p align=\"center\">\\1 </p>", $html);
    $html = preg_replace("#\[b\](.*?)\[/b\]#i", "<strong>\\1 </strong>", $html);
    $html = preg_replace("#\[italic\](.*?)\[/italic\]#i", "<em>\\1 </em>", $html);
    $html = preg_replace("#\[line\](.*?)\[/line\]#i", "<u>\\1 </u>", $html);
    $html = preg_replace("#\[strike\](.*?)\[/strike\]#i", "<strike>\\1 </strike>", $html);
    $html = preg_replace("#\[size=small\](.*?)\[/small\]#i", "<font size=\"1\">\\1 </font>", $html);
    $html = preg_replace("#\[size=medium\](.*?)\[/medium\]#i", "<font size=\"4\">\\1 </font>", $html);
    $html = preg_replace("#\[size=big\](.*?)\[/big\]#i", "<font size=\"6\">\\1 </font>", $html);
    $html = preg_replace("#\[color=red\](.*?)\[/color\]#i", "<font color=\"#FF0000\">\\1 </font>", $html);
    $html = preg_replace("#\[color=orange\](.*?)\[/color\]#i", "<font color=\"#FF6600\">\\1 </font>", $html);
    $html = preg_replace("#\[color=blue\](.*?)\[/color\]#i", "<font color=\"#0000FF\">\\1 </font>", $html);
    $html = preg_replace("#\[color=green\](.*?)\[/color\]#i", "<font color=\"#339966\">\\1 </font>", $html);
    $html = preg_replace("#\[color=neon green\](.*?)\[/color\]#i", "<font color=\"00FF00\">\\1 </font>", $html);
    $html = preg_replace("#\[color=black\](.*?)\[/color\]#i", "<font color=\"#000000\">\\1 </font>", $html);
    $html = preg_replace("#\[color=pink\](.*?)\[/color\]#i", "<font color=\"#FF99cc\">\\1 </font>", $html);

     return($html);
     
}

# test string
$viewname = defines what uses the above [tags] [/tags];
//example 
$viewlova   = $_POST['formfieldname'] ; //would define a part of a form to use the tags
$viewbash =  defines what uses the above [tags] [/tags];

#  conversion. combines the overall function and the defines from the immediate above variables
$viewname = submitTags($viewname, 1, 1);
$viewlova = submitTags($viewlova, 1, 1);
$viewbash = submitTags($viewbash, 1, 1);
Any ideas how I can have the articles pulled from database to be written over with the
bbcode stuff?
Last edited by blacksnday on Thu Aug 25, 2005 9:04 am, edited 3 times in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

foreach ( $newposts as $postfield => $postinput ) 
{ 
echo "<b>".submitTags($postinput).":</b> $postfield<br>";
}
That would be my guess =)

just call the function submitTags where you want the data to be formatted... or just format the data before you insert it into the database
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

thanks for the help, but that isn't working.

This is a little more complicated then it seems because of a few things....

Code: Select all

$viewname = variable name here;
Needs to define what the function will be converting to use the text with the
[tag] [/tag] aleady in place.

Code: Select all

$viewname = submitTags($viewname, 1, 1);
This defines two things. One being the function, and two being what that first snippet
above will be using to convert.

I could make it just be one such as

Code: Select all

$html = stuff to define here; 
$html = submitTags($html, 1, 1);
but then I would still be in same situation with how to get my
random_news_function to read and accept through two bits of code from another
function.
With the example of

Code: Select all

$html = echo 'text to [b]convert here[/b]'; 
$html = submitTags($html, 1, 1);
would output
text to convert here

maybe now it makes more sense why I am having trouble with the database info?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're still flipping named indices and their values ;)

try something like this

Code: Select all

function one_random_news_entry() {
  $result = mysql_unbuffered_query("SELECT * FROM news ORDER BY RAND() LIMIT 1");
  while ($row = mysql_fetch_assoc($result))
  {
    $newposts = array ( "Name" => $row['name'], "Past Loves" => $row['lova'], "Bash" => $row['news'] );
    foreach ( $newposts as $postfield => $postinput )
    {
      if($postfield == 'Bash') $postinput = submitTags($postinput, 1, 1);
      echo "<b>{$postfield}:</b>  {$postinput}<br>";
    }
    echo "<p>&nbsp;</p>";
  }
}
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

still isn't working.
Not throwing any errors.. and that is priceless alone :p
However also still won't strip html and convert tags.

I can understand why most people don't make their own
bbcode, lol.... after about 6 hours of making the bbcode work,
and over 5 hours now trying to get it to work correctly with
data pulled from database... Im pretty tired i think :D

I have tried so many things to make this work I already forgot half of them

I wish this was as easy as displaying the submitted info once user
hits the submit form, since I already got that part working lol
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

where are these tags? Did you already send the text through submitTags() on insertion to the database?
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

Yes.. once submitted they are stored in database.
The function in question then converts those tags to html, while at
same time stripping any html that is not in the form of a pre-made tag.

To see it work once you submit, go to
http://www.vilificnews.info/dev/testsubmit.php
and use the tags available.
Upon submission you see the tags converted to html,
while any html you may have used WITHOUT using the tags
are no longer there.

Yes the tags are stored AS IS, in the database once submitted.
Even the html is stored, but taken away upon viewing.

The function is there to convert those tags to html for browsers.

The thanks.php is currently using the code in the following way to display
and convert correctly:

Code: Select all

//The three below are saying that $view....
//belong to the formfield name for the given value
$viewname = $_POST['username']; 
$viewlova = $_POST['lova']; 
$viewbash = $_POST['userbash']; 

//The three below are saying the above are part of the overall function
$viewname = submitTags($viewname, 1, 1);
$viewlova = submitTags($viewlova, 1, 1);
$viewbash = submitTags($viewbash, 1, 1);


//This is how it is displayed on the thanks.php page
   echo "<div align=center>Username: $viewname</div><br />"; 
   echo "<div align=center>Past Loves: $viewlova</div><br />"; 
   echo "<div align=center>Bash: $viewbash</div><br />";
Based on the above logic... I am having trouble figuring out how to
make the DB query follow the same rules
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I revision of my previous code post:

Code: Select all

function one_random_news_entry() {
  $result = mysql_unbuffered_query("SELECT * FROM news ORDER BY RAND() LIMIT 1");
  while ($row = mysql_fetch_assoc($result))
  {
    $newposts = array ( "Name" => $row['name'], "Past Loves" => $row['lova'], "Bash" => $row['news'] );
    foreach ( $newposts as $postfield => $postinput )
    {
      $postinput = submitTags($postinput, 1, 1);
      echo "<b>{$postfield}:</b>  {$postinput}<br>";
    }
    echo "<p>&nbsp;</p>";
  }
}
If that doesn't work, your storage will come into question...
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

Sweet! that works!
Man... looking at that change, it wasnt much either :P
Thanks alot for the help!

btw, I should mention I got my bbcode to work with same tags
multiple times due to this thread
viewtopic.php?t=37134&highlight=pregreplace
I did spend a few hours on that also.

Hey, I'm proof that I do search before I ask :P

Thanks alot again!
Post Reply