【问题标题】:Textarea line break no working properlyTextarea换行符无法正常工作
【发布时间】:2015-05-31 19:54:01
【问题描述】:

根据我的经验,我有一个最好的问题,我想通过 fwrite() 编写 .htaccess 文件,当它在 textarea 中调试它的显示正常但当我要提交它时它显示 \n\r\n \r....我尝试了 str_replace() 及其工作,但这并没有断线。这是我所有的代码,请帮助我。

提交.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>

请帮帮我......

【问题讨论】:

  • 也许是您问题的线索。它是特定于操作系统的。 Linux 使用“\n”作为换行符,而 Windows 使用“\r\n”。当我遇到类似问题时,我不得不根据服务器操作系统转换换行符。
  • 你试过 PHP_EOL 吗?
  • @GeorgiosDimitriadis:谢谢,我用的是windows操作系统,但我的问题还没有解决......
  • @Falt4rm:是的,先生;和 /n/r/n/r/n/r/n/r/n/r 一样…………
  • @Falt4rm 当我使用 textarea 提交时,问题就来了

标签: php sql .htaccess str-replace fwrite


【解决方案1】:

四种解决方案:

1)使用PHP函数nl2br()

例如

echo nl2br("This\r\nis\n\ra\nstring\r");

// will output
This<br />
is<br />
a<br />
string<br />

2)将输入包裹在&lt;pre&gt;&lt;/pre&gt;标签中。

见: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的功能

【讨论】:

  • 这是我在文件中的输出 (<IfModule mod_rewrite.c>
    \r\n
    \r\n
    \r\n RewriteEngine on \r\n
    \r\n
    \r\n
    \r\n
    \r\n
    \r\n
    \r\n
    \r\n
    \r\n RewriteRule ^Song/{0,1}$ pagee.php?menu_id=4[QSA,L]
    \r\ n
    \r\n RewriteRule ^Movie/{0,1}$ pagee.php?menu_id=3[QSA,L]
    \r\n
    \r\n RewriteRule ^Music /{0,1}$ pagee.php?menu_id=2[QSA,L]
    \r\n
    \r\n RewriteRule ^aaaa/{0,1}$ pagee.php?menu_id =1[QSA,L]
    \r\n
    \r\n </IfModule> )
  • 尝试使用第四个解决方案。
  • 好的,先生,我也会这样做
  • 删除所有文件打开或写入功能,只需使用file_put_contents将内容写入文件。
  • 好的,先生,我也会等的。
【解决方案2】:

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

提交.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.phpok

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2020-10-08
    • 2013-09-07
    • 2016-10-05
    • 2016-01-18
    相关资源
    最近更新 更多