当前位置: 首页 > 工具软件 > JSV > 使用案例 >

php v8js 执行外部js,php运行jsv8引擎

闾丘炫明
2023-12-01

这两天在写markdown2html的功能,现有的ph扩展转换质量都很差,发现有一个to-markdown.js转换效果还算能满足需求,于是折腾了一个在php里运行v8引擎的小工具,写api来处理。

看到红薯有个翻译了一个demo:http://www.oschina.net/question/12_62525

Ubuntu 12.04下安装,部署到centos上的话,应该一样,但还没有测试

[shell]

sudo apt-get install php5-dev php-pear libv8-dev build-essential

sudo pecl install v8js

sudo pecl install channel://pecl.php.net/v8js-0.1.3

sudo echo extension=v8js.so >>/etc/php5/cli/php.ini

sudo echo extension=v8js.so >>/etc/php5/apache2/php.ini

php -m | grep v8

[/shell]

[php]

$markdown = 'to-markdown.js';

$markdown = file_get_contents($markdown);

$html = 'test.html';

$html = file_get_contents($html);

$html = str_replace("\r", '_+-r-+_', $html);

$html = str_replace("\n", '_+-n-+_', $html);

$html = addslashes($html);

$v8 = new V8Js();

$JS = <<< EOT

{$markdown}

var html= '{$html}';

toMarkdown(html);

EOT;

file_put_contents('my.js', $html);

$return = $v8->executeString($JS);

$return = str_replace('_+-r-+_', "\r", $return);

$return = str_replace("_+-n-+_", "\n", $return);

file_put_contents('result.txt', $return);

[/php]

 类似资料: