使用 PHP 用标签包装一段文本

发布于 2024-08-06 10:57:12 字数 1844 浏览 7 评论 0原文

我试图允许用户(使用 Wordpress)插入 jquery 幻灯片画廊(http://www.queness.com/resources/html/slideshow/jquery-slideshow.html)基于虚假标签。例如:

[slideshow]
    <img src="url" /><br />
    <img src="url" />
[!slideshow]

会产生类似于

<div id="gallery">
     <a href="#"><img src="url" /></a><br />
     <a href="#"><img src="url" /></a><br />
</div>

我认为我遇到问题的地方是 jquery 代码要求将 img 包含在锚标记中。这是我所拥有的,但感谢 WordPress,代码上方或下方的任何内容都没有正确格式化。我使用了 Wordpress 格式化功能,但它将每一行都包装在段落标记中,因此它只会破坏所有内容。

function make_slideshow($string) {

 $patterns[0] = '/(\[\[slideshow\]\])/';
 $patterns[1] = '/(\[\[\!slideshow\]\])/';
 $replacements[0] = '<div id="gallery">';
 $replacements[1] = '<div class="caption"><div class="content"></div></div></div>';
 $replace = preg_replace($patterns, $replacements, $string);

 $new_line = explode("\n", $replace);

 foreach($new_line as $key => $value) {
  if($value == "" || $value == " " || is_null($value)) {
   unset($new_line[$key]);
  }
 }

 $sorted_lines = array_values($new_line);

 foreach($sorted_lines as $key => $value){
  if( (stristr($value, 'href') === FALSE) && (stristr($value, 'img') !== FALSE) ){
   $sorted_lines[$key] = '<a href="#">' . $value . '</a>';
  }

  if( (stristr($value, 'show') === FALSE) && ($key === 1) ){
   $value = explode(" ", $value);
   $value[0] .= ' class="show"';
   $sorted_lines[$key] = implode(" ", $value);
  }

 }

返回$sorted_lines;

};

通常我会在 SO 上找到所有其他答案,所以这只是我的第一个问题。我不知道对于其他人来说这是否是一个太大的问题而无法帮助我解决,但我被困住了所以我想我应该尝试一下。

I'm trying to allow a user (using Wordpress) to insert a jquery slideshow gallery (http://www.queness.com/resources/html/slideshow/jquery-slideshow.html) based on a faux tag. For example:

[slideshow]
    <img src="url" /><br />
    <img src="url" />
[!slideshow]

Would produce something similar to

<div id="gallery">
     <a href="#"><img src="url" /></a><br />
     <a href="#"><img src="url" /></a><br />
</div>

I think where I'm having problems is the jquery code requires the img to be enclosed with anchor tags. Here's what I have, but thanks to Wordpress, anything above or below the code isn't formatted correctly. I've used the Wordpress formatting function, but it wraps EVERY line in a paragraph tag, so it just breaks everything.

function make_slideshow($string) {

 $patterns[0] = '/(\[\[slideshow\]\])/';
 $patterns[1] = '/(\[\[\!slideshow\]\])/';
 $replacements[0] = '<div id="gallery">';
 $replacements[1] = '<div class="caption"><div class="content"></div></div></div>';
 $replace = preg_replace($patterns, $replacements, $string);

 $new_line = explode("\n", $replace);

 foreach($new_line as $key => $value) {
  if($value == "" || $value == " " || is_null($value)) {
   unset($new_line[$key]);
  }
 }

 $sorted_lines = array_values($new_line);

 foreach($sorted_lines as $key => $value){
  if( (stristr($value, 'href') === FALSE) && (stristr($value, 'img') !== FALSE) ){
   $sorted_lines[$key] = '<a href="#">' . $value . '</a>';
  }

  if( (stristr($value, 'show') === FALSE) && ($key === 1) ){
   $value = explode(" ", $value);
   $value[0] .= ' class="show"';
   $sorted_lines[$key] = implode(" ", $value);
  }

 }

return $sorted_lines;

};

Normally I find all my other answers on SO, so this is only my first question. I don't know if it's way too big of a problem for someone else to try to help me out with, but I am stuck so I figured I'd give it a shot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

病毒体 2024-08-13 10:57:12

WordPress 在编辑器中自动添加 p 标签;你需要阻止 WP 这样做。根据我的经验,您可以删除编辑器中的所有换行符 - 整理代码 - 或使用一些插件来阻止 WP 添加格式: TinyMCE Advanced 和/或 禁用 wpautop

Wordpress adds p tags automatically in the editor; you need to stop WP from doing that. In my experience, you either remove all line breaks in the editor - scrunch the code up - or use a few plugins, to stop WP from adding the formatting: TinyMCE Advanced and/or Disable wpautop.

℉服软 2024-08-13 10:57:12

该代码中没有任何内容添加 ap 标记。如果这就是问题所在,您需要找到将 p 添加为包装器的真正代码。

There's nothing in that code that adds a p tag. If that's the problem, you need to find the real code that adds p as a wrapper.

情泪▽动烟 2024-08-13 10:57:12

哪个 WordPress 函数正在调用您的例程?

我遇到了类似的问题:

<?php wp_list_pages( $args ); ?> 

正在向我想要抑制的所有内容添加

    links..

检查 WP 开发文档中的例程,该例程位于您的例程的上一层,我打赌那里会是一个 $arg 值,用于抑制

标签。

http://codex.wordpress.org/Function_Reference

What WordPress function is calling your routine?

I had a similar problem with:

<?php wp_list_pages( $args ); ?> 

Was adding <ul> links to everything I wanted to surpress..

Check the WP dev docs for the routine that is up one hierarchy from your routine and I bet there will be an $arg value to suppress <p> tags.

http://codex.wordpress.org/Function_Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文