Wrapping a chunk of text with tags using PHP

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

Read More
<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.

Related posts

Leave a Reply

3 comments