Php重现js脚本(转换为php脚本)

时间:2011-10-12 13:56:03

标签: php javascript jquery replace

我想将这个js脚本重现/转换为php脚本,有没有人可以帮助我? 我想我需要在php中使用preg_replace()并且我真的不喜欢它:/

这是我的简单脚本:

wysiwyg_val = $('.wysiwyg textarea').val()
.replace(/\n/g, "<br>")
.replace(/<br>/g,'[br]')
.replace(/\</g,'&lt;')
.replace(/\>/g,'&gt;')
.replace(/\{code\}/g, '<pre><code>')
.replace(/\{\/code\}/g, '</code></pre>')
.replace(/\{strong\}/g, '<strong>')
.replace(/\{\/strong\}/g, '</strong>')
.replace(/\{italic\}/g, '<i>')
.replace(/\{\/italic\}/g, '</i>')
.replace(/\{title\}/g, '<h2>')
.replace(/\{\/title\}/g, '</h2>')
.replace(/\{subtitle\}/g, '<h3>')
.replace(/\{\/subtitle\}/g, '</h3>')
.replace(/\[br\]/g,'<br>');

wysiwyg_val,在转义之后,通过Ajax传递到一个php页面,可以将其作为$_POST['text']

2 个答案:

答案 0 :(得分:3)

$wysiwyg_val = 'xyz'; // your WYSIWYG input

$replacements = array(
    array('/\n/g', "<br>"),
    array('/<br>/g', '[br]'),
    array('/\</g','&lt;'),
    // and so forth.
);

foreach ($replacements as $replacement) {
    $wysiwyg_val = preg_replace($replacement[0], $replacement[1], $wysiwyg_val);
}

答案 1 :(得分:2)

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

所以就这样做

$subject = $POST['text'];

$subject = preg_replace($pattern, $replacement, $subject);
$subject = preg_replace($pattern, $replacement, $subject);
...