Php重现一个js脚本(转换为Php脚本)


Php reproduce a js script (convert to php script)

我想复制/转换这个js脚本到PHP脚本,有没有人可以帮助我?我想我需要preg_replace()在PHP中,我真的不擅长它:/

这是我的js简单脚本:

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']

$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);
}
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);
...