Error: Could not fetch http://thedevnet.com/wp-content/plugins/gd-star-rating/css/gdsr.css.php?t=0&s=a10i10m20k20c05r05%23121620243046%23121620243240%23s1pchristmas%23s1pcrystal%23s1pdarkness%23s1poxygen%23s1goxygen_gif%23s1pplain%23s1ppumpkin%23s1psoft%23s1pstarrating%23s1pstarscape%23t1pclassical%23t1pstarrating%23t1gstarrating_gif&ver=1.7.2 for caching. You might need to exclude this file in WP Minify options. The Developer Network – Get Data Between Two Elements

Get Data Between Two Elements

Ever had a bunch of text where you wanted some pieces of it? This snippet will get all between to elements. eg) [bob] where [ would be first element and ] would be second element.

Snippet:

  1. <?
  2.  // returns an array from a string split by variable elements
  3.  // nongeek: gets something from between two other things
  4.  // http://thedevnet.com/php/snippets/get-data-between-two-elements/
  5.  
  6.  function data_from_elements($needle_start, $needle_end, $haystack, $tags=false)
  7.  {
  8.   $pos_start = 0;
  9.  
  10.   while(($pos_start = strpos($haystack,$needle_start,$pos_start)) !== false)
  11.   {
  12.    $pos_end = strpos($haystack,$needle_end,$pos_start);
  13.  
  14.    if($tags)
  15.     $array[] = substr($haystack,$pos_start,$pos_end-$pos_start+strlen($needle_end));
  16.    else
  17.     $array[] = substr($haystack,$pos_start + strlen($needle_start),
  18.      $pos_end - $pos_start - strlen($needle_start));
  19.  
  20.    $pos_start++;
  21.   }
  22.  
  23.   return @$array;
  24.  }
  25. ?>

Example:

  1. <?
  2.  $test = "[bob][jim][sue]";
  3.  
  4.  print_r(data_from_elements("[","]",$test, true));
  5.  // Array ( [0] => [bob] [1] => [jim] [2] => [sue] )
  6.  
  7.  print_r(data_from_elements("[","]",$test));
  8.  // Array ( [0] => bob [1] => jim [2] => sue )
  9. ?>
GD Star Rating
loading...

Originally posted 2009-10-17 00:03:00.

Popularity: 6%

Posted by irbobo   @   6 August 2010
Tags : , ,

Related Posts

Like this post? Share it!

RSS Digg Twitter StumbleUpon Delicious Technorati Facebook

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

You must be logged in to post a comment.

Previous Post
« PHP Simple Script Security
Next Post
Speed Limit Download of File »
Powered by Wordpress   |   Lunated designed by ZenVerse