javascript - How to replace new line with %0D%0A with PHP - Stack Overflow

I have a JavaScript code that can replace new lines with %0D%0AI need the same code in PHP.This is how

I have a JavaScript code that can replace new lines with %0D%0A

I need the same code in PHP.

This is how my JavaScript code looks like:

text = text.replace(/\n\r?/g, '%0D%0A');

I tried with PHP but I am getting only one line without newlines.

I have a JavaScript code that can replace new lines with %0D%0A

I need the same code in PHP.

This is how my JavaScript code looks like:

text = text.replace(/\n\r?/g, '%0D%0A');

I tried with PHP but I am getting only one line without newlines.

Share Improve this question edited Mar 1, 2014 at 15:06 dachi 1,60211 silver badges15 bronze badges asked Mar 1, 2014 at 14:57 user1477332user1477332 3253 gold badges4 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

You don't need regular expressions for that. Simple string replacement will be enough.
Use str_replace function:

$test = 'Some very long text with multiple lines...';
$newText = str_replace(PHP_EOL, '%0D%0A', $text);

New line character differs in various systems. It's not a good idea to hard-code \n\r. Better solution is to use PHP_EOL constant.

a literal translation of your java code into php could use preg_replace http://www.php/preg_replace

<?php
$text = "some text";
$text = preg_replace("\r\n","%0D%0A",$text);
?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743762446a4502880.html

相关推荐

  • javascript - How to replace new line with %0D%0A with PHP - Stack Overflow

    I have a JavaScript code that can replace new lines with %0D%0AI need the same code in PHP.This is how

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信