Jump to content

PHP <textarea> text replacement


morrisjohnny

Recommended Posts

Hello.*EDITI have found wha ti'm looking for i just need to find the char for linebreak i've left my problem in for anyone who was having the same.*UN-EDITI've just created a forum for my websiteI'm having problems with a <text area>when you user types in

MoreText

The display is just

More Text

I want it to stay

MoreText

I'm just wondering how would i detec where their is a line break and type in <br> after it has been sumitted?While the topics open what would happen if they put in :) how would i turn this into an image?Thanks-Johnny

Link to comment
Share on other sites

Thanks I'm just having a look now.I'll report back with my finding seems to be perfect.*EDITFCKeditor is something which is a bit too complex since it lets users input checkboxs. I'm hoping to find something i little more simple although i would rather do it all manually to learn. Also i don't know how to 'install' then and extract the data the user is inputting to insert into the data base. I will find one suitable and play around.I will look for a much simpler one**RE-EDITI have found out how to use string replace which is what i want for the replacement does anybody now know what the char is for linebreak for i can replace it with <br>$_message=str_replace("(line-break-char","<br>".$_POST['a_answer'])

Link to comment
Share on other sites

Did you forget the ending semi-colon (:) at the line above that line?I think that's something like that, that generates the error, as there's no apparent problem with that line.Check your syntax.. :?)

Link to comment
Share on other sites

If your using str_replace as this:

$_message = str_replace('/\n/', '<br>', $_POST['a_answer']);

you need to change it to:

$_message = str_replace("\n", '<br />', $_POST['a_answer']);

I removed the slashes (/) as they are a part of the reg.exp. (Perl-style) which str_replace() doesn't use.I also replaced the sinqle-quotes (') with double-quotes (") as escapes (\n) aren't treatet in "sinqle-quote-strings".Also replaced the break-tag with a "corrct" XHTML-syntax... :?)All this also means that if you want use preg_replace it would look like this:

$_message = preg_replace("/\n/", '<br />', $_POST['a_answer']);

NOT:

$_message = preg_replace('/\n/', '<br />', $_POST['a_answer']);

Hope that helped. :) Good Luck and Don't Panic!

Link to comment
Share on other sites

thanks working perfectly so how would i find out the other functions like would it just$_message = str_replace('', '<b>', $_POST['a_answer']);is it possible to insert code into the input box form clicking a button?for the likes of inserting after clicking the bold links

Link to comment
Share on other sites

the easiest way to use bb-/format-code is tou use preg_replace and arrays:

$patterns = array();$replaces = array();// Add codes$patterns[] = '/\[b\](.*)\[\/b\]/'; // Matches [b]TEXT[/b]$replaces[] = '<span class="bold">$1</span>';$patterns[] = '@\[i\](.*)\[/i\]@'; // Matches [i]TEXT[/i] , The @'s let us skip the escaping of / in [/i] (see the one above for refference)$replaces[] = '<span class="italic">$1</span>';$patterns[] = '@\[url\](.*)\[/url\]@'; // Matches [url]URL[/url]$replaces[] = '<a href="$1" target="_blank">$1</a>';$patterns[] = '@\[url=(.*)\](.*)\[/url\]@'; // Matches [url=URL]TEXT[/url]$replaces[] = '<a href="$1" target="_blank">$2</a>';$patterns[] = '@\[color=(.*)\](.*)\[/color\]@'; // Matches [color=COLOR]TEXT[/color]$replaces[] = '<span style="color: $1">$2</span>';// Adding the newline replace here, then you don't need multiple call to a replace function...$patterns[] = "/\n/";$replaces[] = "<br />\n";$_message = preg_replace( $patterns, $replaces, $_POST['a_answer']);

Then you can expand the list of patterns with anything you need, you could also add some sort of syntax check for the url's and maybe some pattern that "auto-detects" web- and mail-adresses which doesn't have </a> or <a href="http://" target="_blank">.EDIT:You can use this function if you feel that the way to add patterns above is abit complicated:

/** * AddPattern * @param list		The array/list to add the pattern to * @param pattern	The (perl) regular expression pattern to add * @param string	The string that the pattern should be replaced with * @param escape	Should we "escape" the pattern (escapes /, [ and ] ) and adds  *							"delitimers" ( /pattern/ ) and any defined "rule" * @param modifs	Any modifiers that should be added to the pattern (ignored if escape is false) * 						could be i (case-insensitive) etc. (see manual) * @author Christopher Hindefjord - Mr_CHISOL - Kachtus.net  2007 **/function AddPattern( &$list, $pattern, $string, $escape = false, $modifs = '' ) {	// Just in case...	if (!isset($list[0])) {		$list[0] = array();		$list[1] = array();	}	// If we should "escape" the string and treat it... (default: No)	if ($escape) {		// Escape...		$pattern = str_replace( '/', '\/', $pattern );		$pattern = str_replace( '[', '\[', $pattern );		$pattern = str_replace( ']', '\]', $pattern );		// add /		$pattern = '/'.$pattern.'/'.$modifs;	}	$list[0][] = $pattern;	$list[1][] = $string;}

Here's how you could use it (see http://www.php.net/manual/en/ref.pcre.php for more info about reg.exp and modifiers):

$patterns = array( array(), array() );AddPattern( $patterns, '[b](.*)[/b]', '<span class="bold">$1</span>', true, 'i' ); // ignore case and escape itAddPattern( $patterns, '[i](.*)[/i]', '<span class="italic">$1</span>', true, 'i' ); // ignore case and escape itAddPattern( $patterns, '[url](.*)[/url]', '<a href="$1">$1</span>', true, 'i' ); // ignore case and escape it// You can add the rest yourself...// Adding the newline replace here, then you don't need multiple call to a replace function...AddPattern( $patterns, "/\n/", "<br />\n" );$_message = preg_replace( $patterns[0], $patterns[1], $_POST['a_answer']);

Please NOTE: the function is currently untested, but it should work just fine...

Link to comment
Share on other sites

would i jut place that where i placed the line break code?so it does for <b> for <i> for <u>hyperlink for <a href='link>Hyperlink</a>ect right? would that work fine or would i need to install bb code or something?that would still turn out like <html> tags when displayed right?

Link to comment
Share on other sites

The linebreak code is included in the examples, but yes, put it where you put the linebreak code.Just taught of something, when do you run this code, when you add the post to the db or when you display it.The best would be the 2nd; when you display it, this also means that you can add more flexible "codes" if you would like (as counting days to a surtain date), but that's more for fun then real use (often at least...) ;?)Yes, it should come out as html, unless you have something that removes html-tags or something like that.

Link to comment
Share on other sites

I'm using this while i post it into the database. should i make it so it changes it when it's being loaded instead?what do you mean by flexible codes?so thoses tags are right then? for <b> ect yeah?cheers i'll add it in nowis their anyway you can reset the auto_increment back to 1 or anything? i'm doing so much testing it's on about 90 and i want it to be 'fresh' or would that jsut imple being running the sql_query again?

Link to comment
Share on other sites

Here's how you could use it (see http://www.php.net/manual/en/ref.pcre.php for more info about reg.exp and modifiers):

$patterns = array( array(), array() );AddPattern( $patterns, '[b](.*)[/b]', '<span class="bold">$1</span>', true, 'i' ); // ignore case and escape itAddPattern( $patterns, '[i](.*)[/i]', '<span class="italic">$1</span>', true, 'i' ); // ignore case and escape itAddPattern( $patterns, '[url](.*)[/url]', '<a href="$1">$1</span>', true, 'i' ); // ignore case and escape it// You can add the rest yourself...// Adding the newline replace here, then you don't need multiple call to a replace function...AddPattern( $patterns, "/\n/", "<br />\n" );$_message = preg_replace( $patterns[0], $patterns[1], $_POST['a_answer']);

Please NOTE: the function is currently untested, but it should work just fine...After putting this into my script it says "call to undefined function AddPattern"I have no idea on functions but i'm gussing it's not be 'set-up'correctly? i'm gussing?

Link to comment
Share on other sites

Yes it's better to have the "formatting" done when displaying instead of posting, here's one advantage:You write your text, post it and then discoveres that you did something wrong, misspelled or just want to add more text.Then when you open the post for edit (as you can do in in this and most forums for example) you will see the HTML-code, which make's it harder to read and understand, for all of us, but mostely those who doesn't understand HTML...You are almost right; will not be replaced with <b>, as it's not valid XHTML, it would be replaced with <span class="bold">, so you need to add a class named bold to your CSS (if you don't have one, aswell add an italic class etc.Why not use <b>, <i> and <font>? It's simply not valid "anymore" (XHTML-standards), and why write "unvalid code" when there's no big difficulty in writing valid code.First, the thing with flexible codes is that's there (often) not a ""serious"" use of them (there is in some applications), as I said.Some examples of flexible code:

// code => use => result[date]  => Current date is [date] => Current date is 2007-04-06[time] => Current time is [time] => Current time is 13:37[user] => Hello [user], Greetings! => Hello morrisjohnny[daysleft 2007-04-13] => There's [daysleft 2007-04-13] days left to friday the 13th => There's 7 days left to friday the 13th

EDIT:Did you included the function that I posted together with that code?Tested the function on my computer and worked as intended

Link to comment
Share on other sites

okay, I', understand it. I'll try and convert it all to when it's being displayed :)Thanks :)*EDITI didn't add the middle part i have added it now if i put something in text it just sends nothing?like i said i'm going to convert everyting so it changes on display rather than post.*UN-EDITYou have been a big help :)*EDITNow when i show the text it just display it like it is in the databasewith [ b]bold[ /b] without the spaceshere is my code

<?phpinclude ("../logincheck.php");?><?php if ($logged_in) { ?><?php$host="localhost"; // Host name$username="root"; // Mysql username$password=""; // Mysql password$db_name="mt"; // Database name$tbl_name="forum"; // Table name// Connect to server and select databse.mysql_connect("$host", "$username", "$password")or die("cannot connect");mysql_select_db("$db_name")or die("cannot select DB");// get value of id that sent from address bar$id=$_GET['id'];$sql="SELECT * FROM $tbl_name WHERE id='$id'";$result=mysql_query($sql);$rows=mysql_fetch_array($result);?><html><header><link href='../style.css' rel='stylesheet' type='text/css'></header><body><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF"><tr><td class="fheader"><strong>Title:<? echo $rows['topic']; ?></strong></td></tr><tr><td class="fsub"><b>By</b> <? echo $rows['name']; ?> <b>On:</b> <? echo $rows['datetime']; ?></td></tr><tr><td class="fmain"><? echo $rows['detail']; ?></td></tr></table></td></tr></table><br><?php$tbl_name2="forum_answer"; // Switch to table "forum_answer"$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";$result2=mysql_query($sql2);while($rows=mysql_fetch_array($result2)){<?php/*** AddPattern* @param list        The array/list to add the pattern to* @param pattern    The (perl) regular expression pattern to add* @param string    The string that the pattern should be replaced with* @param escape    Should we "escape" the pattern (escapes /, [ and ] ) and adds*                            "delitimers" ( /pattern/ ) and any defined "rule"* @param modifs    Any modifiers that should be added to the pattern (ignored if escape is false)*                         could be i (case-insensitive) etc. (see manual)* @author Christopher Hindefjord - Mr_CHISOL - Kachtus.net  2007**/function AddPattern( &$list, $pattern, $string, $escape = false, $modifs = '' ) {    // Just in case...    if (!isset($list[0])) {        $list[0] = array();        $list[1] = array();    }    // If we should "escape" the string and treat it... (default: No)    if ($escape) {        // Escape...        $pattern = str_replace( '/', '\/', $pattern );        $pattern = str_replace( '[', '\[', $pattern );        $pattern = str_replace( ']', '\]', $pattern );        // add /        $pattern = '/'.$pattern.'/'.$modifs;    }    $list[0][] = $pattern;    $list[1][] = $string;}$patterns = array( array(), array() );AddPattern( $patterns, '[b](.*)[/b]', '<span class="bold">$1</span>', true, 'i' ); // ignore case and escape itAddPattern( $patterns, '[i](.*)[/i]', '<span class="italic">$1</span>', true, 'i' ); // ignore case and escape itAddPattern( $patterns, '[url="http://(.*)"](.*)[/url]', '<a href="$1">$1</span>', true, 'i' ); // ignore case and escape it// You can add the rest yourself...// Adding the newline replace here, then you don't need multiple call to a replace function...AddPattern( $patterns, "/\n/", "<br />\n" );$_message = preg_replace( $patterns[0], $patterns[1], $rows['a_answer']);?>?><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td class="fsub"><b>By:</b> <? echo $rows['a_name']; ?> <b>On</b> <? echo $rows['a_datetime']; ?></td></tr><tr><td class="fmain"><? echo $_message; ?></td></tr></table></td></tr></table><br><?}$sql3="SELECT view FROM $tbl_name WHERE id='$id'";$result3=mysql_query($sql3);$rows=mysql_fetch_array($result3);$view=$rows['view'];// if have no counter value set counter = 1if(empty($view)){$view=1;$sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";$result4=mysql_query($sql4);}// count more value$addview=$view+1;$sql5="update $tbl_name set view='$addview' WHERE id='$id'";$result5=mysql_query($sql5);mysql_close();?><BR><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><form name="form1" method="post" action="forum_add_answer.php"><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td valign="top"><strong></strong></td></tr><tr><td colspan='2'><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td></tr><tr><td><input name="id" type="hidden" value="<? echo $id; ?>"></td><td align='center'><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td></tr></table></td></form></tr></table><?php } else { ?><?php include("../log_in.php") ?><?php } ?>  </body></html>

Link to comment
Share on other sites

First move the function outside the while-loop (above it or in a include-file where you store all your functions that's needed in more than one script...)Second: Tried the following:

....print_r( $patterns );echo preg_replace( $patterns[0], $patterns[1], "[b]aa[/b]\n[i]ddd[/i]");

and it looks Ok...This feels wrong:

$_message = preg_replace( $patterns[0], $patterns[1], $rows['a_answer']);?>?><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr>

did you remove something before posting or does it look like this? Any way that shouldnt be the problem, I will tak a closer look..This is also wrong:

while($rows=mysql_fetch_array($result2)){<?php... // Here comes the function

(The function should be moved, but this is still wrong...)Also noted that you end and start the php-code block many times (and most of them for no good reason). Ex:

<?php } else { ?><?php include("../log_in.php") ?><?php } ?>

this is better and is easier to debug:

<?php } else {  include("../log_in.php"); } ?>

And also try to be concistent in your use of <?php and <? (use one of then, the first is recommended, all the time...)Do you have this site live to the web? or can you post the output...

Link to comment
Share on other sites

if you give me about 15 mins i THINK i can have it on the web.I think i have a host that will let me. I've bin doing it locally. I'll get to work and message you when i'm doneokay i don't have a host. Or at least i do but not one that is working correctlyhttp://jnymris.my-php.net/index.phpso nope i don't have it online. What do you want to see?

Link to comment
Share on other sites

output

<html><header><link href='../style.css' rel='stylesheet' type='text/css'></header><body><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF"><tr><td class="fheader"><strong>Title:Welcome</strong></td></tr><tr><td class="fsub"><b>By</b> jnymris <b>On:</b> 06/04/07 01:27:04</td></tr><tr><td class="fmain">Welcome To The Forums<br /><br />Please use your comman sense</td></tr></table></td></tr></table><br><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td class="fsub"><b>By:</b> jnymris <b>On</b> 06/04/07 07:39:42</td></tr><tr><td class="fmain">Testing Replies</td></tr></table></td></tr></table><br><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td class="fsub"><b>By:</b> admin <b>On</b> 06/04/07 12:47:52</td></tr><tr><td class="fmain">[b]larger Bold B's [/b][b]smaller bold B's[/b]</td></tr></table></td></tr></table><br><BR><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><form name="form1" method="post" action="forum_add_answer.php"><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td valign="top"><strong></strong></td></tr><tr><td colspan='2'><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td></tr><tr><td><input name="id" type="hidden" value="3"></td><td align='center'><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td></tr></table></td></form></tr></table>  </body></html>

__________________________The Scriptslog_in.php

<html>  <header>   <link href="style.css" rel="stylesheet" type="text/css">   <title>Log In - Management Tycoon</title><script language='javascript' type='text/javascript'> if (top.location != self.location) {top.location.replace(self.location)};</script>  </header>  <body><table align='center' border='1'><tr><td class='indextitle' colspan='2'><img src='banner.bmp'></tr></td><tr><td class='indextitle' colspan='2'>Welcome To Management Tycoon</td></tr><tr><td width='50%'>	<table with='100%' valign='top'>		<tr><td>Management Tycoon is a new online text based game, where you become the manager of your own business in hope to make millions. You being the manager will have to deal with the creation of your product, the staff, sponsors backing your product in order ho help you make millions.<br>But is it as simple as that?</td></tr>	</table>	</td><td>	<table width='50%' border='0'>		<tr><td>Existing Members can log in below</td></tr>		<tr><td><?php echo "<font color='red'>" .$msg . "</font>"; ?><form action="" method="post"></td></tr>    			<tr><td>Username:<input type="text" name="username" /></td></tr>    			<tr><td>Password:<input type="password" name="password" /></td></tr>			<tr><td><input type="submit" name="submit" value="Log In" /></td></tr>			<tr></td></form></td></tr>	</table></td></tr><tr><td align='center' colspan='2'>- <a href='screenshots.html'>Screenshots</a> - <a href='register.php'>Register</a> -</td></tr></table>

forum_view_topic.php

<?phpinclude ("logincheck.php");?><?php if ($logged_in) { ?><?php$host="localhost"; // Host name$username="root"; // Mysql username$password=""; // Mysql password$db_name="mt"; // Database name$tbl_name="forum"; // Table name// Connect to server and select databse.mysql_connect("$host", "$username", "$password")or die("cannot connect");mysql_select_db("$db_name")or die("cannot select DB");// get value of id that sent from address bar$id=$_GET['id'];$sql="SELECT * FROM $tbl_name WHERE id='$id'";$result=mysql_query($sql);$rows=mysql_fetch_array($result);?><html><header><link href='style.css' rel='stylesheet' type='text/css'></header><body><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF"><tr><td class="fheader"><strong>Title:<? echo $rows['topic']; ?></strong></td></tr><tr><td class="fsub"><b>By</b> <? echo $rows['name']; ?> <b>On:</b> <? echo $rows['datetime']; ?></td></tr><tr><td class="fmain"><? echo $rows['detail']; ?></td></tr></table></td></tr></table><br><?php$tbl_name2="forum_answer"; // Switch to table "forum_answer"$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";$result2=mysql_query($sql2);while($rows=mysql_fetch_array($result2)){?><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td class="fsub"><b>By:</b> <? echo $rows['a_name']; ?> <b>On</b> <? echo $rows['a_datetime']; ?></td></tr><tr><td class="fmain"><? echo $rows['a_answer']; ?></td></tr></table></td></tr></table><br><?}$sql3="SELECT view FROM $tbl_name WHERE id='$id'";$result3=mysql_query($sql3);$rows=mysql_fetch_array($result3);$view=$rows['view'];// if have no counter value set counter = 1if(empty($view)){$view=1;$sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";$result4=mysql_query($sql4);}// count more value$addview=$view+1;$sql5="update $tbl_name set view='$addview' WHERE id='$id'";$result5=mysql_query($sql5);mysql_close();?><BR><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><form name="form1" method="post" action="forum_add_answer.php"><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td valign="top"><strong></strong></td></tr><tr><td colspan='2'><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td></tr><tr><td><input name="id" type="hidden" value="<? echo $id; ?>"></td><td align='center'><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td></tr></table></td></form></tr></table><?php } else { ?><?php include("log_in.php") ?><?php } ?>  </body></html>

logincheck.php

<?phpsession_start();$logged_in = false;$msg = "";if ($_SESSION['online'])  $logged_in = true;else if (isset($_POST['username'])){  $db=mysql_connect("localhost", "root", "");  mysql_select_db("mt", $db);  $username = htmlentities($_POST['username']);  $password = htmlentities($_POST['password']);  $username = mysql_real_escape_string($username);  $password = mysql_real_escape_string($password);  $query = mysql_query("SELECT login, pazzword FROM Users WHERE login = '$username' AND pazzword = '$password'");  if(mysql_num_rows($query) == 1)  {    $_SESSION['online'] = true;    $_SESSION['username'] = $username;    $logged_in = true;  }  else    $msg .= "The username and password did not match.<br /><br />";  mysql_free_result($query);  mysql_close($db);}/*at this point you can do a header redirect if necessary because you have not sent any outputheader("Location: login.php");exit();*/?>

NEWoutput

<html><header><link href='../style.css' rel='stylesheet' type='text/css'></header><body><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF"><tr><td class="fheader"><strong>Title:Welcome</strong></td></tr><tr><td class="fsub"><b>By</b> jnymris <b>On:</b> 06/04/07 01:27:04</td></tr><tr><td class="fmain">Welcome To The Forums<br /><br />Please use your comman sense</td></tr></table></td></tr></table><br><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td class="fsub"><b>By:</b> jnymris <b>On</b> 06/04/07 07:39:42</td></tr><tr><td class="fmain"></td></tr></table></td></tr></table><br><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td class="fsub"><b>By:</b> admin <b>On</b> 06/04/07 12:47:52</td></tr><tr><td class="fmain"></td></tr></table></td></tr></table><br><BR><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><form name="form1" method="post" action="forum_add_answer.php"><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td valign="top"><strong></strong></td></tr><tr><td colspan='2'><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td></tr><tr><td><input name="id" type="hidden" value="3"></td><td align='center'><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td></tr></table></td></form></tr></table>  </body></html>

forum_view_topic.php

<?phpinclude ("../logincheck.php");?><?php if ($logged_in) { ?><?php$host="localhost"; // Host name$username="root"; // Mysql username$password=""; // Mysql password$db_name="mt"; // Database name$tbl_name="forum"; // Table name// Connect to server and select databse.mysql_connect("$host", "$username", "$password")or die("cannot connect");mysql_select_db("$db_name")or die("cannot select DB");// get value of id that sent from address bar$id=$_GET['id'];$sql="SELECT * FROM $tbl_name WHERE id='$id'";$result=mysql_query($sql);$rows=mysql_fetch_array($result);?><html><header><link href='../style.css' rel='stylesheet' type='text/css'></header><body><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF"><tr><td class="fheader"><strong>Title:<? echo $rows['topic']; ?></strong></td></tr><tr><td class="fsub"><b>By</b> <? echo $rows['name']; ?> <b>On:</b> <? echo $rows['datetime']; ?></td></tr><tr><td class="fmain"><? echo $rows['detail']; ?></td></tr></table></td></tr></table><br><?php$tbl_name2="forum_answer"; // Switch to table "forum_answer"$sql2="SELECT * FROM $tbl_name2 WHERE question_id='$id'";$result2=mysql_query($sql2);/*** AddPattern* @param list        The array/list to add the pattern to* @param pattern    The (perl) regular expression pattern to add* @param string    The string that the pattern should be replaced with* @param escape    Should we "escape" the pattern (escapes /, [ and ] ) and adds*                            "delitimers" ( /pattern/ ) and any defined "rule"* @param modifs    Any modifiers that should be added to the pattern (ignored if escape is false)*                         could be i (case-insensitive) etc. (see manual)* @author Christopher Hindefjord - Mr_CHISOL - Kachtus.net  2007**/function AddPattern( &$list, $pattern, $string, $escape = false, $modifs = '' ) {    // Just in case...    if (!isset($list[0])) {        $list[0] = array();        $list[1] = array();    }    // If we should "escape" the string and treat it... (default: No)    if ($escape) {        // Escape...        $pattern = str_replace( '/', '\/', $pattern );        $pattern = str_replace( '[', '\[', $pattern );        $pattern = str_replace( ']', '\]', $pattern );        // add /        $pattern = '/'.$pattern.'/'.$modifs;    }    $list[0][] = $pattern;    $list[1][] = $string;}$patterns = array( array(), array() );AddPattern( $patterns, '[b](.*)[/b]', '<span class="bold">$1</span>', true, 'i' ); // ignore case and escape itAddPattern( $patterns, '[i](.*)[/i]', '<span class="italic">$1</span>', true, 'i' ); // ignore case and escape itAddPattern( $patterns, '[url="http://(.*)"](.*)[/url]', '<a href="$1">$1</span>', true, 'i' ); // ignore case and escape it// You can add the rest yourself...// Adding the newline replace here, then you don't need multiple call to a replace function...AddPattern( $patterns, "/\n/", "<br />\n" );$_message = preg_replace( $patterns[0], $patterns[1], $rows['a_answer']);while($rows=mysql_fetch_array($result2)){?><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td class="fsub"><b>By:</b> <? echo $rows['a_name']; ?> <b>On</b> <? echo $rows['a_datetime']; ?></td></tr><tr><td class="fmain"><? echo $_message; ?></td></tr></table></td></tr></table><br><?}$sql3="SELECT view FROM $tbl_name WHERE id='$id'";$result3=mysql_query($sql3);$rows=mysql_fetch_array($result3);$view=$rows['view'];// if have no counter value set counter = 1if(empty($view)){$view=1;$sql4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";$result4=mysql_query($sql4);}// count more value$addview=$view+1;$sql5="update $tbl_name set view='$addview' WHERE id='$id'";$result5=mysql_query($sql5);mysql_close();?><BR><table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><form name="form1" method="post" action="forum_add_answer.php"><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td valign="top"><strong></strong></td></tr><tr><td colspan='2'><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td></tr><tr><td><input name="id" type="hidden" value="<? echo $id; ?>"></td><td align='center'><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td></tr></table></td></form></tr></table><?php } else { ?><?php include("../log_in.php") ?><?php } ?>  </body></html>

Link to comment
Share on other sites

Hi Simply use the following code for new line..

 <?php 	if(isset($_POST['Submit'])) 		echo nl2br($_POST['myarea']);?> <form name="form1" id="form1" method="post" action="" enctype="multipart/form-data"> <textarea name="myarea" cols="30" rows="3" ></textarea>  <input type="submit" name="Submit" value="Submit" /></form>

suppose this will help you..Regards,Vijay

Link to comment
Share on other sites

  • 2 weeks later...

codes in this post is very useful for me, but I want to ask more about link typing your BBcode used

[url]http://.....[/url]

for link like below

...$patterns[] = '@\[url\](.*)\[/url\]@'; // Matches [url]URL[/url]$replaces[] = '<a href="$1" target="_blank">$1</a>';  ...$a_answer = preg_replace( $patterns, $replaces, $a_answer);

how can I just typing the url address and it appears as link without using

[url] ... [/url]

thanks !

Link to comment
Share on other sites

You need to use a regular expression to match all valid web addresses, and add the extra tags around the matches. Here is a pattern to match URLs as defined by the RFC:

(([\w]+:)?//)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...