1

I have some php and javascript, everythings work and I do not get any error message. I can post data without any error to database. And everythings looks good but I cannot use some function in php.

Example.

I have a textarea and I send its data with ajax to a php file. In php file a need to use str_replace function. I can insert data to database in same php file without any error but the function that I try to use like str_replace or mysqli_real_escape_string, etc. do not work.

What would be the reason?

Here codes.

    $(".editBoxButton").click(function(){

        var yazi = jQuery('#editInput').val();

            $.ajax({
                type: 'POST' ,
                url : 'ajax/editEntry.php',
                    data: {
                    text: yazi,
                    },
                success : function(d){ 
                alert(d);
                location.reload();  //refresh
                }
            });

    });

ajax/editEntry.php

<?php

$yeniyazi=$_POST['text'];   
$yeniyazi = str_replace("\n", "<br>", $yeniyazi);

$s=$yeniyazi;

echo json_encode($s);

?>

in the alert, I get still \n. it does not replaced.

I do not get any error. only str_replace do not work that is my problem. expect str_replace, it works properly.

2
  • make sure you use double quotes for special characters as \n (happened to me). I know your answer has double quotes, just in case you didn't copy-paste Commented Aug 7, 2013 at 7:17
  • json_encode turns an array/object into a string. You seem to be working on a string already, so there is no need for a json_encode. Commented Aug 7, 2013 at 7:22

4 Answers 4

1

You should probably just do:

$yeniyazi = nl2br($yeniyazi);

If you really want to manually replace, use regex.

$yeniyazi = preg_replace("/\r?\n/s", "<br />", $yeniyazi);

Sign up to request clarification or add additional context in comments.

1 Comment

nl2br also has the benefit that it will handle different forms of new lines (i.e. \r\n, \n\r, \n, and \r)
1

Actually, you have to escape the \ with another one or use singlequotes instead.

$val = str_replace("\\n" , "<br>", $val);

Or you can do it in a single line like this :

<?php 
echo json_encode(nl2br($_POST['text']));   

Comments

1

i hope this example help you:

<?php
// Order of replacement
$str     = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
$order   = array("\r\n", "\n", "\r");
$replace = '<br />';

// Processes \r\n's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);

 // Outputs F because A is replaced with B, then B is replaced with C, and so on...
// Finally E is replaced with F, because of left to right replacements.
$search  = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);

// Outputs: apearpearle pear
// For the same reason mentioned above
$letters = array('a', 'p');
$fruit   = array('apple', 'pear');
$text    = 'a p';
$output  = str_replace($letters, $fruit, $text);
echo $output;
?>

Comments

0

I also have problem with \n using in javascript and PHP.

Current Env.:

  • PHP 5.3.10

  • Javascript

  • XHTML 1.0

The problem:

....
if (rowcount == 5) {
    openShipments += '\n';
    rowcount = 0;
}
....

It works when using \\n bellow:

....
if (rowcount == 5) {
    openShipments += '\\n';
    rowcount = 0;
}
....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.