无法从PHP中的字符串中删除所有换行符

无法从PHP中的字符串中删除所有换行符

问题描述:

I've got a basic textarea form. What i'm trying to do is remove all line breaks from the textarea after its been submitted.

Here's my PHP:

<?php 
   if (isset ($_POST['comment_form_submit'])) {
    $body = mysql_real_escape_string($_POST['body']);

    $breaks = array("
", "
", "");
    $newtext = str_replace($breaks, "", $body);

    echo $newtext;
   }
?>

Now let say I when filling out the form I press Shift+enter, that creates a in my string, but after submitting the form, $newtext still equals " ", shouldn't " " be removed because I'm replacing it with ""

Thanks.

我有一个基本的textarea表单。 我要做的是在提交后删除textarea中的所有换行符。 p>

这是我的PHP: p>

 &lt  ;?php 
 if(isset($ _POST ['comment_form_submit'])){
 $ body = mysql_real_escape_string($ _ POST ['body']); 
 
 $ breaks = array(“
 
”  ,“
”,“
”); 
 $ newtext = str_replace($ breaks,“”,$ body); 
 
 echo $ newtext; 
} 
?&gt; 
  code  >  pre> 
 
 

现在让我说填写表单时我按Shift + Enter,在我的字符串中创建一个 ,但在提交表单后,$ newtext仍然等于“ ” ,不应该删除“ ”,因为我用“”替换它“ p>

谢谢。 p> div>

The problem is that you're escaping the for MySQL first. Try this:

<?php 
   if (isset ($_POST['comment_form_submit'])) {
    $body = $_POST['body'];

    $breaks = array("
", "
", "");
    $newtext = str_replace($breaks, "", $body);

    $newtext = mysql_real_escape_string($newtext);

    echo $newtext;
   }
?>

Read about the escape function you're using to see why: http://php.net/mysql_real_escape_string

Please use this below code, Or copy paste the content in CK editor and replace the character which u want to replace...

 <?php
    $desc = trim(stripslashes($crw['Description']));
    $desc=str_replace(","," ",$desc);
    $desc=str_replace("
"," ",$desc);
    $desc=str_replace("
"," ",$desc);
    $desc=str_replace("<br>"," ",$desc);
    $desc=str_replace("<br />"," ",$desc);
    echo $desc; ?>