过滤HTML输出的javascript

时间:2013-07-04 16:58:46

标签: php javascript prettyphoto

我正在使用prettyPhoto API手动打开灯箱,使用以下代码:

api_images  ['images/fullscreen/image1.jpg','images/fullscreen/image2.jpg','images/fullscreen/image3.jpg'];
api_titles = ['Title 1','Title 2','Title 3'];
api_descriptions = ['Description 1','Description 2','Description 3']
$.prettyPhoto.open(api_images,api_titles,api_descriptions);

我面临的问题是描述值是从Wordpress的WYSIWYG中提取的,代码很容易被随机的html标签,标点符号等打破

  <script type="text/javascript">
    $(document).ready(function() {
        $('#menu-item-1006').on('click', function(e) {
            e.preventDefault();
            var images = new Array();
            var descriptions = new Array();
            var titles = new Array();
<?php
$i = 0;
$images = new WP_Query(array('post_type' => 'clearance', 'showposts' => -1, 'order' => 'menu_order', 'orderby' => 'ASC'));
if ($images->have_posts()) : while ($images->have_posts()) : $images->the_post();

        $featured = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
        ?>
                            images[<?php echo $i; ?>] = '<?php echo $featured[0]; ?>';
                            titles[<?php echo $i; ?>] =  '<?php the_title(); ?>'
                            descriptions[<?php echo $i; ?>] = '<?php echo get_the_content(); ?>';

        <?php
        $i++;
    endwhile;
else:
    ?>
<?php endif; ?>

            $.prettyPhoto.open(images, titles, descriptions);
        }) 
    });    
</script>

如何过滤get_the_content()函数以便输出w / o错误?谢谢!

3 个答案:

答案 0 :(得分:1)

一个简单的解决方案可能是:

替换

<?php echo get_the_content(); ?>

<?php echo preg_replace('/\<[^\>]+\>/s', '', get_the_content()); ?>

答案 1 :(得分:0)

json_encode(get_the_content())有效!

答案 2 :(得分:0)

$content = get_the_content();
echo strip_tags($content, '<a><img>');

仅会留下aimg个标签。我认为这就是你需要的所有画廊