开发者

regular expression to convert WordPress tags to HTML

I have this HTML that I am reading from wordpress into a regular PHP file. This is not valid HTML but wordpress is supposed to strip the [caption] out take the img tag, and put it into a div with the caption as a <p> tag. But I want to do this in PHP, with no wordpress.

[captio开发者_开发技巧n id="" align="alignnone" width="190" caption="my caption"]html image tag would got here but got stripped[/caption]

How would you do this in a regular expression. I am thinking of somehow using preg_match_callback here somehow.


It seems in order to let the wordpress code do the work all you need are the two files plugin.php and shortcodes.php from the wp-includes directory.

require 'wordpress/wp-includes/plugin.php';
require 'wordpress/wp-includes/shortcodes.php';
add_shortcode('caption', 'handleCaption');
$content = '[caption id="foo" style="bar"]Marry had a little lamb[/caption]whose fleece was white as snow';
do_shortcode($content);

function handleCaption($attributes, $content='') {
    var_dump($attributes, $content);
}

prints

array(2) {
  ["id"]=>
  string(3) "foo"
  ["style"]=>
  string(3) "bar"
}
string(23) "Marry had a little lamb"


Here's a possible reg exp.

$res = preg_match_all('/\[caption(.*?)\](.*?)\[\/caption\]/iU',
'[caption id="" align="alignnone" width="190" caption="my caption"]html image tag would got here but got stripped[/caption]',
$matches);


var_dump($res, $matches);

The output

int(1)
array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(122) "[caption id="" align="alignnone" width="190" caption="my caption"]html image tag would got here but got stripped[/caption]"
  }
  [1]=>
  array(1) {
    [0]=>
    string(57) " id="" align="alignnone" width="190" caption="my caption""
  }
  [2]=>
  array(1) {
    [0]=>
    string(46) "html image tag would got here but got stripped"
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜