Textarea line break无法正常工作

时间:2015-05-31 19:54:02

标签: php sql .htaccess str-replace fwrite

根据我的经验,我有一个最好的问题,我想通过fwrite()编写.htaccess文件,当它在textarea旁边调试它的show ok时,但是当我要提交它然后它显示\ 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 ---输出结果现在显示

<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>
  

但我希望这样:

<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>

请请帮助我......

2 个答案:

答案 0 :(得分:3)

四种解决方案:

1)使用PHP函数nl2br()

<强> e.g。

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()结合了fopenfwritefclose

的功能

答案 1 :(得分:1)

好的亲爱的,我想我发现你的结果只是替换好了,

  

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确定

即可