Textarea 换行符无法正常工作
Textarea line break no working properly
根据我的经验,我有一个最好的问题,我想通过 fwrite() 编写 .htaccess 文件,当它调试时它在 textarea 旁边显示正常但是当我要提交它时它显示 \n\r\n\r... 我尝试了 str_replace() 及其工作,但这并没有中断。这是我所有的代码,请帮助我。
submit.php
<?php
//.htaccess file write and rewrite query
$file = ".htaccess";
$submit7 = $_POST['submit7'];
$edit = mysql_real_escape_string(str_replace( array("\r\n", "\n"), " ", $_POST['edit']));
function wee() {
echo "<IfModule mod_rewrite.c> \n
\n RewriteEngine on \n";
require('config2.php'); $getquery=mysql_query("SELECT * FROM menu ORDER BY menu_id DESC"); while($rows=mysql_fetch_assoc($getquery)){$menu_id=$rows['menu_id']; $linkname=$rows['linkname'];
echo "\n RewriteRule ^".$linkname."/{0,1}$ pagee.php?menu_id=".$menu_id. "[QSA,L] \n"; }
echo "\n </IfModule>";
}
if ($submit7) {
if ( is_writable( $file ) ) {
// is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
$f = fopen( $file, 'w+');
if ( $f !== false ) {
fwrite( $f, $edit );
fclose( $f );
}
}
}
?>
<form id="form7" name="form7" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>
<input type="submit" name="submit7" value="Write" />
</label>
<textarea name="edit"><?php echo wee(); ?></textarea>
</form>
config2.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("myweb");
?>
<?php
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("myweb");
?>
sql.sql
--
-- Database: `myweb`
--
-- --------------------------------------------------------
--
-- Table structure for table `menu`
--
CREATE TABLE IF NOT EXISTS `menu` (
`menu_id` int(11) NOT NULL,
`mname` text NOT NULL,
`level` text NOT NULL,
`linkname` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `menu`
--
INSERT INTO `menu` (`menu_id`, `mname`, `level`, `linkname`) VALUES
(1, 'Home', 'home', 'aaaa'),
(2, 'Music', 'Music', 'Music'),
(3, 'Movie', 'Movie', 'Movie'),
(4, 'Song', 'Song', 'Song');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `menu`
--
ALTER TABLE `menu`
ADD PRIMARY KEY (`menu_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `menu`
--
ALTER TABLE `menu`
MODIFY `menu_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
.htaccess --- output result now showing
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^Song/{0,1}$ pagee.php?menu_id=4[QSA,L] RewriteRule ^Movie/{0,1}$ pagee.php?menu_id=3[QSA,L] RewriteRule ^Music/{0,1}$ pagee.php?menu_id=2[QSA,L] RewriteRule ^aaaa/{0,1}$ pagee.php?menu_id=1[QSA,L] </IfModule>
But I want that like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^Song/{0,1}$ pagee.php?menu_id=4[QSA,L]
RewriteRule ^Movie/{0,1}$ pagee.php?menu_id=3[QSA,L]
RewriteRule ^Music/{0,1}$ pagee.php?menu_id=2[QSA,L]
RewriteRule ^aaaa/{0,1}$ pagee.php?menu_id=1[QSA,L]
</IfModule>
拜托拜托拜托帮帮我......
四种解法:
1) 使用PHP函数nl2br()
例如
echo nl2br("This\r\nis\n\ra\nstring\r");
// will output
This<br />
is<br />
a<br />
string<br />
2) 将输入包裹在 <pre></pre>
标签中。
参见: http://www.w3.org/wiki/HTML/Elements/pre
3)使用,
$textToStore = nl2br(htmlentities($inputText, ENT_QUOTES, 'UTF-8'));
4)使用,
file_put_contents('.htaccess', $_POST['textarea_value']);
file_put_contents()
结合了fopen
、fwrite
、fclose
的功能
好的,亲爱的,我想我发现你的结果只是做那个替换就可以了,
submit.php
<?php
//.htaccess file write and rewrite query
$file = ".htaccess";
$submit7 = $_POST['submit7'];
if ($submit7)
{
$htfe = file_put_contents('.htaccess', $_POST['edit']);
}
function wee()
{
echo "<IfModule mod_rewrite.c> \n
\n RewriteEngine on \n";
require('config2.php'); $getquery=mysql_query("SELECT * FROM menu ORDER BY menu_id DESC"); while($rows=mysql_fetch_assoc($getquery)){$menu_id=$rows['menu_id']; $linkname=$rows['linkname'];
echo "\n RewriteRule ^".$linkname."/{0,1}$ pagee.php?menu_id=".$menu_id. "[QSA,L] \n"; }
echo "\n </IfModule>";
}
?>
<form id="form7" name="form7" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>
<input type="submit" name="submit7" value="Write" />
</label>
<textarea name="edit"><?php echo wee(); ?></textarea>
</form>
我认为你应该得到解决方案,好的
只需更改此文件submit.php
ok
根据我的经验,我有一个最好的问题,我想通过 fwrite() 编写 .htaccess 文件,当它调试时它在 textarea 旁边显示正常但是当我要提交它时它显示 \n\r\n\r... 我尝试了 str_replace() 及其工作,但这并没有中断。这是我所有的代码,请帮助我。
submit.php
<?php
//.htaccess file write and rewrite query
$file = ".htaccess";
$submit7 = $_POST['submit7'];
$edit = mysql_real_escape_string(str_replace( array("\r\n", "\n"), " ", $_POST['edit']));
function wee() {
echo "<IfModule mod_rewrite.c> \n
\n RewriteEngine on \n";
require('config2.php'); $getquery=mysql_query("SELECT * FROM menu ORDER BY menu_id DESC"); while($rows=mysql_fetch_assoc($getquery)){$menu_id=$rows['menu_id']; $linkname=$rows['linkname'];
echo "\n RewriteRule ^".$linkname."/{0,1}$ pagee.php?menu_id=".$menu_id. "[QSA,L] \n"; }
echo "\n </IfModule>";
}
if ($submit7) {
if ( is_writable( $file ) ) {
// is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
$f = fopen( $file, 'w+');
if ( $f !== false ) {
fwrite( $f, $edit );
fclose( $f );
}
}
}
?>
<form id="form7" name="form7" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>
<input type="submit" name="submit7" value="Write" />
</label>
<textarea name="edit"><?php echo wee(); ?></textarea>
</form>
config2.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("myweb");
?>
<?php
$con = mysql_connect('localhost','root','')
or die(mysql_error());
mysql_select_db ("myweb");
?>
sql.sql
--
-- Database: `myweb`
--
-- --------------------------------------------------------
--
-- Table structure for table `menu`
--
CREATE TABLE IF NOT EXISTS `menu` (
`menu_id` int(11) NOT NULL,
`mname` text NOT NULL,
`level` text NOT NULL,
`linkname` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `menu`
--
INSERT INTO `menu` (`menu_id`, `mname`, `level`, `linkname`) VALUES
(1, 'Home', 'home', 'aaaa'),
(2, 'Music', 'Music', 'Music'),
(3, 'Movie', 'Movie', 'Movie'),
(4, 'Song', 'Song', 'Song');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `menu`
--
ALTER TABLE `menu`
ADD PRIMARY KEY (`menu_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `menu`
--
ALTER TABLE `menu`
MODIFY `menu_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
.htaccess --- output result now showing
<IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^Song/{0,1}$ pagee.php?menu_id=4[QSA,L] RewriteRule ^Movie/{0,1}$ pagee.php?menu_id=3[QSA,L] RewriteRule ^Music/{0,1}$ pagee.php?menu_id=2[QSA,L] RewriteRule ^aaaa/{0,1}$ pagee.php?menu_id=1[QSA,L] </IfModule>
But I want that like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^Song/{0,1}$ pagee.php?menu_id=4[QSA,L]
RewriteRule ^Movie/{0,1}$ pagee.php?menu_id=3[QSA,L]
RewriteRule ^Music/{0,1}$ pagee.php?menu_id=2[QSA,L]
RewriteRule ^aaaa/{0,1}$ pagee.php?menu_id=1[QSA,L]
</IfModule>
拜托拜托拜托帮帮我......
四种解法:
1) 使用PHP函数nl2br()
例如
echo nl2br("This\r\nis\n\ra\nstring\r");
// will output
This<br />
is<br />
a<br />
string<br />
2) 将输入包裹在 <pre></pre>
标签中。
参见: http://www.w3.org/wiki/HTML/Elements/pre
3)使用,
$textToStore = nl2br(htmlentities($inputText, ENT_QUOTES, 'UTF-8'));
4)使用,
file_put_contents('.htaccess', $_POST['textarea_value']);
file_put_contents()
结合了fopen
、fwrite
、fclose
好的,亲爱的,我想我发现你的结果只是做那个替换就可以了,
submit.php
<?php
//.htaccess file write and rewrite query
$file = ".htaccess";
$submit7 = $_POST['submit7'];
if ($submit7)
{
$htfe = file_put_contents('.htaccess', $_POST['edit']);
}
function wee()
{
echo "<IfModule mod_rewrite.c> \n
\n RewriteEngine on \n";
require('config2.php'); $getquery=mysql_query("SELECT * FROM menu ORDER BY menu_id DESC"); while($rows=mysql_fetch_assoc($getquery)){$menu_id=$rows['menu_id']; $linkname=$rows['linkname'];
echo "\n RewriteRule ^".$linkname."/{0,1}$ pagee.php?menu_id=".$menu_id. "[QSA,L] \n"; }
echo "\n </IfModule>";
}
?>
<form id="form7" name="form7" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label>
<input type="submit" name="submit7" value="Write" />
</label>
<textarea name="edit"><?php echo wee(); ?></textarea>
</form>
我认为你应该得到解决方案,好的
只需更改此文件submit.php
ok