首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将PHP中的新行替换为JavaScript

问将PHP中的新行替换为JavaScript
EN

Stack Overflow用户
提问于 2012-06-08 00:09:57
回答 4查看 238关注 0票数 3

情况很简单:

我发布了一个带有文本区域的普通HTML表单。然后,在PHP中,我根据表单的内容注册一个JavaScript函数。

这是使用以下代码完成的:

代码语言:javascript
复制
$js = sprintf("window.parent.doSomething('%s');", $this->textarea->getValue());

在我尝试处理换行符之前,它就像一个护身符。我需要将换行符替换为char 13 (我相信),但是我找不到一个有效的解决方案。我尝试了以下几种方法:

代码语言:javascript
复制
$textarea = str_replace("\n", chr(13), $this->textarea->getValue());

以及以下内容:

代码语言:javascript
复制
$js = sprintf("window.parent.doSomething('%s');", "'+String.fromCharCode(13)+'", $this->textarea->getValue());

有没有人知道如何正确处理这些换行符?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-06-14 18:36:12

你就快到了,你只是忘了换行符。

这应该能起到作用:

代码语言:javascript
复制
$js = sprintf("window.parent.doSomething('%s');"
    , preg_replace(
              '#\r?\n#'
            , '" +  String.fromCharCode(13) + "'
            , $this->textarea->getValue()
);
票数 3
EN

Stack Overflow用户

发布于 2012-06-08 00:22:25

你想做的是:

代码语言:javascript
复制
str_replace("\n", '\n', $this->textarea->getValue());

将所有换行符替换为原义字符串'\n'。

但是,最好将其编码为JSON:

代码语言:javascript
复制
$js = sprintf(
    "window.parent.doSomething('%s');",
    json_encode($this->textarea->getValue())
);

这也将修复引号。

票数 1
EN

Stack Overflow用户

发布于 2012-06-14 18:37:29

你的问题已经在我们的代码库的其他地方解决了...

摘自我们的WebApplication.php文件:

代码语言:javascript
复制
    /**
     * Log a message to the javascript console
     *
     * @param $msg
     */
    public function logToConsole($msg)
    {
        if (defined('CONSOLE_LOGGING_ENABLED') && CONSOLE_LOGGING_ENABLED)
        {
            static $last = null;
            static $first = null;
            static $inGroup = false;
            static $count = 0;

            $decimals = 5;

            if ($first == null)
            {
                $first          = microtime(true);
                $timeSinceFirst = str_repeat(' ', $decimals) . ' 0';
            }

            $timeSinceFirst = !isset($timeSinceFirst)
                ? number_format(microtime(true) - $first, $decimals, '.', ' ')
                : $timeSinceFirst;

            $timeSinceLast = $last === null
                ? str_repeat(' ', $decimals) . ' 0'
                : number_format(microtime(true) - $last, $decimals, '.', ' ');

            $args = func_get_args();
            if (count($args) > 1)
            {
                $msg = call_user_func_array('sprintf', $args);
            }
            $this->registerStartupScript(
                sprintf("console.log('%s');", 
                    sprintf('[%s][%s] ', $timeSinceFirst, $timeSinceLast) .
                    str_replace("\n", "'+String.fromCharCode(13)+'", addslashes($msg))));

            $last = microtime(true);
        }
    }

您感兴趣的部分是:

代码语言:javascript
复制
str_replace("\n", "'+String.fromCharCode(13)+'", addslashes($msg))

请注意,在您的问题的sprintf中,您忘记了str_replace...

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10935645

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档