Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    3
    Thanked 0 Times in 0 Posts

    PHP and HTML Code

    Okay so I have this forum and I want to allow people to post HTML code into the site as a post, such as from photobucket.
    PHP Code:
    <?php
    session_start
    ();
    //create_cat.php
    include 'connect.php';
    include 
    'header.php';

    $sql "SELECT
                topic_id,
                topic_subject
            FROM
                topics
            WHERE
                topics.topic_id = " 
    mysql_real_escape_string($_GET['id']);
                
    $result mysql_query($sql);

    if(!
    $result)
    {
        echo 
    'The topic could not be displayed, please try again later.';
    }
    else
    {
        if(
    mysql_num_rows($result) == 0)
        {
            echo 
    'This topic doesn&prime;t exist.';
        }
        else
        {
            while(
    $row mysql_fetch_assoc($result))
            {
                
    //display post data
                
    echo '<table class="topic" border="1">
                        <tr>
                            <th colspan="2">' 
    $row['topic_subject'] . '</th>
                        </tr>'
    ;
            
                
    //fetch the posts from the database
                
    $posts_sql "SELECT
                            posts.post_topic,
                            posts.post_content,
                            posts.post_date,
                            posts.post_by,
                            users.user_id,
                            users.user_name
                        FROM
                            posts
                        LEFT JOIN
                            users
                        ON
                            posts.post_by = users.user_id
                        WHERE
                            posts.post_topic = " 
    mysql_real_escape_string($_GET['id']);
                            
                
    $posts_result mysql_query($posts_sql);
                
                if(!
    $posts_result)
                {
                    echo 
    '<tr><td>The posts could not be displayed, please try again later.</tr></td></table>';
                }
                else
                {
                
                    while(
    $posts_row mysql_fetch_assoc($posts_result))
                    {
                        echo 
    '<tr class="topic-post">
                                <td class="user-post">' 
    $posts_row['user_name'] . '<br/>' date('d-m-Y H:i'strtotime($posts_row['post_date'])) . '</td>
                                <td class="post-content">' 
    htmlentities(stripslashes($posts_row['post_content'])) . '</td>
                              </tr>'
    ;
                    }
                }
                
                if(!
    $_SESSION['signed_in'])
                {
                    echo 
    '<tr><td colspan=2>You must be <a href="signin.php">signed in</a> to reply. You can also <a href="signup.php">sign up</a> for an account.';
                }
                else
                {
                    
    //show reply box
                    
    echo '<tr><td colspan="2"><h2>Reply:</h2><br />
                        <form method="post" action="reply.php?id=' 
    $row['topic_id'] . '">
                            <textarea name="reply-content"></textarea><br /><br />
                            <input type="submit" value="Submit reply" />
                        </form></td></tr>'
    ;
                }
                
                
    //finish the table
                
    echo '</table>';
            }
        }
    }

    include 
    'footer.php';
    ?>
    and this is what the forum shows:

    I want it to automatically convert all posts to this html format, even if it is just regular text

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    try

    PHP Code:

    htmlspecialchars
    ($row['post_content']) 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Arcticwarrio View Post
    try

    PHP Code:

    htmlspecialchars
    ($row['post_content']) 
    No that is how to convert special characters to HTML: http://php.net/manual/en/function.htmlspecialchars.php

    I need to figure out how to get it to display the photo from the HTML code
    Last edited by logepoge1; 04-05-2013 at 05:30 PM. Reason: Paraphrase sentence better

  • #4
    New Coder
    Join Date
    May 2010
    Posts
    69
    Thanks
    3
    Thanked 10 Times in 10 Posts
    PHP Code:
    <?php

    $code 
    '<a href="somelink&with&entities">test</a>';
    $encoded htmlentities($code);
    $encoded str_replace(
        array(
    '&lt;''&gt;''&quot;'),
        array(
    '<''>''"'),
        
    $encoded
    );

    echo 
    $encoded;
    Like helpful posts in your topic? Thank the people offering you help :)

  • #5
    Regular Coder
    Join Date
    Sep 2011
    Posts
    407
    Thanks
    18
    Thanked 26 Times in 26 Posts
    This will convert all of the text back to the original characters
    PHP Code:
    <?php
    $row
    ['post_content'] = str_replace(
                array(
    "&lt;""&gt;"'&amp;'''', '&quot;', '&apos;'),
                array("<", ">", '
    &', ''''"''\''),
                
    htmlspecialchars_decode($row['post_content'])
            );
    ?>

  • #6
    New Coder
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Doesnt work

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    407
    Thanks
    18
    Thanked 26 Times in 26 Posts
    No idea why this post was changed or something because that's not what I posted. I copied and pasted it straight from my source and only changed the variables.

    Anyways, here's the real version:
    PHP Code:
    $row['post_content'] = str_replace(
                array(
    '&lt;''&gt;''&amp;''&#039;''&quot;''&apos;'),
                array(
    '<''>''&''\'''"''\''),
                
    htmlspecialchars_decode($row['post_content'])
            ); 
    Edit:
    Apparently the forum was taking some of the characters literally and used the character it represents to display. I fixed that in this post.
    Last edited by Dubz; 05-05-2013 at 07:21 AM.

  • Users who have thanked Dubz for this post:

    logepoge1 (05-08-2013)

  • #8
    New Coder
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I guess what I am asking is similar to how this site works. Where I can click insert image and enter the web url of the image which it would then proceed to post

  • #9
    New Coder
    Join Date
    Jan 2011
    Posts
    75
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by logepoge1 View Post
    I guess what I am asking is similar to how this site works. Where I can click insert image and enter the web url of the image which it would then proceed to post
    You're asking for BBCODE? BBCODE depends on you and rather what you want to use as BBCODE. Or are you asking for a rich text editor?


    http://php.net/manual/en/function.bbcode-create.php <--- BBCODE

    http://www.webdesignerdepot.com/2008...-text-editors/ <--- Rich Text Editor



    EDIT: Upon reading your 1st post again.

    Here is what you are looking for.

    Replace

    PHP Code:
    ' . htmlentities(stripslashes($posts_row['post_content'])) . ' 
    with this code

    PHP Code:
    ';
    $post_content = $_POST['
    post_content'];
    if(get_magic_quotes_gpc())
    {
        $post_content = stripslashes($post_content);
    }
    $post_content = mysql_real_escape_string(($post_content));
    echo ' 
    Last edited by xiong_ster123; 05-08-2013 at 05:57 AM. Reason: Adding more information


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •