Php 是否从变量中删除空格以创建有效的html元素id?

Php 是否从变量中删除空格以创建有效的html元素id?,php,regex,fancybox-2,Php,Regex,Fancybox 2,我正在将fancybox添加到Wordpress上的产品库中。我使用产品标题作为缩略图链接的隐藏div和href的id名称。产品名称中有空格,这打破了fancybox 我在网上找到的解决方案只涉及html元素之间的空白,而我的javascript/regex知识还不足以从中导出解决方案 以下是我用来生成产品缩略图和图像的代码: <div class="entry-post-thumbnail"> <a class="size-<?php echo

我正在将fancybox添加到Wordpress上的产品库中。我使用产品标题作为缩略图链接的隐藏div和href的id名称。产品名称中有空格,这打破了fancybox

我在网上找到的解决方案只涉及html元素之间的空白,而我的javascript/regex知识还不足以从中导出解决方案

以下是我用来生成产品缩略图和图像的代码:

<div class="entry-post-thumbnail">
            <a class="size-<?php echo $image_size;?> fancybox" href="#image-<?php the_title(); ?>"><?php the_post_thumbnail($image_size); ?></a>
            <div id="image-<?php the_title(); ?>" style="display: none;">
                <?php the_post_thumbnail($post->ID,'full'); ?>
            </div>
</div><!-- .entry-post-thumbnail -->


使用
preg\u replace

<div id="image-<?php preg_replace('/(\s/', '', the_title()); ?>" style="display: none;">

使用jquery的javascript中的一个解决方案是:$('.size product').each(function(){var id=$(this.attr();id=id.split(“”).join(“”;$(this.attr('id',id);});您的问题标题提到了javascript,但在您的示例中没有,因此在php部分修复id似乎是合乎逻辑的。你能具体说明你需要修复的地方吗?@DanielGimenez在生成标题时,会输出空格,因为输入的标题通常都有空格。我需要一种方法来删除标题中的空格,一旦它们生成。谢谢
<div id="image-<?php preg_replace('/(\s/', '', the_title()); ?>" style="display: none;">