Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Update query with nl2br and preg_replace, something funny happening

$
0
0

So what I have is a Rich Text editor I made myself it all works great except when I submit the form. My assumption is that it has to do with either nl2br or preg_replace. I have made my own bbcode converter thingy lol.

 

anyways... after click submit and it updates the database fine, but it will put in a lot of <div> elements in the string and I don't have it set to put in <div>'s anywhere except for justify text left,right,center or full. so heres the code thats involved with this.

 

index.php

<?php
    if (isset($_POST['submit'])) {
	include 'bbcode.php';
	$content = $_POST['hiddenpost'];
	$content = nl2br($content);
	$content = preg_replace($pattern, $replace, $content);
				
	mysqli_query($con, "UPDATE news SET content='$content' WHERE id='1'") or die("Update Error: ".mysqli_error($con));
	echo 'Page has been Updated. Thank You :)<br /><br />'.$content;  // the $content is just there for testing purposes
    }
    $result = mysqli_query($con,"SELECT * FROM news")
	or die('Result error: '. mysqli_error($con));
    while($row = mysqli_fetch_array($result))
        {
	    extract($row);
	    $date = date('D M j, Y',strtotime($date));
?>
    <div class="c_header"><div id="pad_content"><input name="header" value="<?php echo $header; ?>" /></div></div>
    <div class="c_content"><div id="pad_content">
    <div class='gre-toolbar' id='toolbar-gre'>
        <select id="size" onchange="select();">
            <option value="14">14px</option>
            <option value="16">16px</option>
	    <option value="18">18px</option>
    	    <option value="20">20px</option>
            <option value="22">22px</option>
            <option value="24">24px</option>
            <option value="26">26px</option>
        </select>
        <a href="#" class="bold" onclick="wrap('b'); return false;"><img src="gre/images/edit-bold.png" alt="bold" title="Bold"></a>
        <a href="#" class="italic" onclick="wrap('i'); return false;"><img src="gre/images/edit-italic.png" alt="italic" title="Italic"></a>
        <a href="#" class="underline" onclick="wrap('U'); return false;"><img src="gre/images/edit-underline.png" alt="underline" title="Underline"></a>
        <a href="#" class="strikeThrough" onclick="wrap('s'); return false;"><img src="gre/images/edit-strikeThrough.png" alt="strikeThrough" title="Strikethrough"></a>
        <a href="#" class="superscript" onclick="wrap('sup'); return false;"><img src="gre/images/edit-superscript.png" alt="superscript" title="Superscript"></a>
        <a href="#" class="subscript" onclick="wrap('sub'); return false;"><img src="gre/images/edit-subscript.png" alt="subscript" title="Subscript"></a>
        <a href="#" class="justifyLeft" onclick="justify('justify=left'); return false;"><img src="gre/images/edit-justify-left.png" alt="justifyLeft" title="Justify Left"></a>
        <a href="#" class="justifyCenter" onclick="justify('justify=center'); return false;"><img src="gre/images/edit-justify-center.png" alt="justifyCenter" title="Justify Center"></a>
        <a href="#" class="justifyRight" onclick="justify('justify=right'); return false;"><img src="gre/images/edit-justify-right.png" alt="justifyRight" title="Justify Right"></a>
        <a href="#" class="justifyFull" onclick="justify('justify=full'); return false;"><img src="gre/images/edit-justify-full.png" alt="justifyFull" title="Justify Full"></a>
        <a href="#" class="insertHorizontalRule" onclick="hr('hr'); return false;"><img src="gre/images/edit-hr.png" alt="insertHorizontalRule" title="Insert Horizontal Rule"></a>
        <a href="#" class="link" onclick="wrap('url'); return false;"><img src="gre/images/edit-link.png" alt="link" title="Insert URL (Link)"></a>
        <a href="#" class="upload" onclick="javascript:popup('picupload.php', '500', '300'); return false;"><img src="gre/images/edit-upload.png" alt="upload" title="Picture Upload"></a>
	<div class="admin_post" name="admin_post" id="admin_post" contenteditable="true"><?php echo $content; ?></div>
        <form method="post" id="newpost" name="newspost">
            <textarea name="hiddenpost" id="hiddenpost" hidden="true"></textarea><br />
            <center><input type="submit" id="submit" name="submit" value="Send"></center>
        </form>
        <script>
	    $("#submit").click(function() {
	        $("#hiddenpost").val($("#admin_post").html());
	    });
	</script>
    </div>
</div></div>
<div class="c_date"><div id="pad_content"><?php echo $date; ?></div></div>
<?php
    }
?>

bbcode.php

<?php
// convert [url=URL]link_title[/url]
$pattern[] = '/\[url=(.*?)\](.*?)\[\/url\]/i';
$replace[] = '<a href="$1" target="_blank">$2</a>';

// convert [url]url_link[/url]
$pattern[] = '/\[url\](.*?)\[\/url\]/i';
$replace[] = '<a href="$1" target="_blank">$1</a>';

// convert [img]image_link[/img]
$pattern[] = '/\[img\](.*?)\[\/img\]/i';
$replace[] = '<img src="files/thumb/$1">';

// convert [b]text[/b]
$pattern[] = '/\[b\](.*?)\[\/b\]/i';
$replace[] = '<b>$1</b>';

// convert [i]text[/i]
$pattern[] = '/\[i\](.*?)\[\/i\]/i';
$replace[] = '<i>$1</i>';

// convert [size=]font size[/size]
$pattern[] = '/\[size\=(.*?)\](.*?)\[\/size\]/i';
$replace[] = '<span style="font-size: $1px;">$2</span>';

// convert [U]text[/U]
$pattern[] = '/\[U\](.*?)\[\/U\]/i';
$replace[] = '<u>$1</u>';

// convert [s]text[/s]
$pattern[] = '/\[s\](.*?)\[\/s\]/i';
$replace[] = '<strike>$1</strike>';

// convert [sup]text[/sup]
$pattern[] = '/\[sup\](.*?)\[\/sup\]/i';
$replace[] = '<sup>$1</sup>';

// convert [sub]text[/sub]
$pattern[] = '/\[sub\](.*?)\[\/sub\]/i';
$replace[] = '<sub>$1</sub>';

// convert [justify=]text[/justify]
$pattern[] = '/\[justify=(.*?)\](.*?)\[\/justify\]/i';
$replace[] = '<div style="text-align: $1; min-width: 100%;">$2</div>';

// convert [hr]
$pattern[] = '/\[hr\]/i';
$replace[] = '<hr>';
?>

Viewing all articles
Browse latest Browse all 13200

Trending Articles