function smd_random_text($atts, $thing=NULL) { global $txpcfg, $variable, $pretext, $thisarticle, $thisfile, $thislink, $thisimage; extract(lAtts(array( 'type' => 'string', 'source' => 'hello world', 'delim' => '|', 'file_delim' => '\n', 'item_num' => '', 'shuffle' => '0', 'trim' => '1', 'escape' => 'html', 'form' => '', 'limit' => '1', 'count' => 'up', 'wraptag' => '', 'break' => '', 'class' => '', 'debug' => '0', ), $atts)); $srcpt = explode($delim, $source); $limit = ($limit < 1) ? 1 : $limit; $from = array(); $items = array(); switch ($type) { case "field": if (isset($thisfile[$srcpt[0]]) && $thisfile[$srcpt[0]] != "") { $from = $thisfile[$srcpt[0]]; } else if (isset($thislink[$srcpt[0]]) && $thislink[$srcpt[0]] != "") { $from = $thislink[$srcpt[0]]; } else if (isset($thisimage[$srcpt[0]]) && $thisimage[$srcpt[0]] != "") { $from = $thisimage[$srcpt[0]]; } else if (isset($pretext[$srcpt[0]]) && $pretext[$srcpt[0]] != "") { $from = $pretext[$srcpt[0]]; } else if (isset($thisarticle[$srcpt[0]]) && $thisarticle[$srcpt[0]] != "") { $from = $thisarticle[$srcpt[0]]; } if ($from) { $from = explode($delim, $from); $limit = (count($from) < $limit) ? count($from) : $limit; $items = ($item_num != '') ? ((count($from) >= $item_num) ? $item_num : 0) : array_rand($from, $limit); } break; case "txpvar": $from = explode($delim, $variable[$srcpt[0]]); $limit = (count($from) < $limit) ? count($from) : $limit; $items = ($item_num != '') ? ((count($from) >= $item_num) ? $item_num : 0) : array_rand($from, $limit); break; case "urlvar": $from = explode($delim, gps($srcpt[0])); $limit = (count($from) < $limit) ? count($from) : $limit; $items = ($item_num != '') ? ((count($from) >= $item_num) ? $item_num : 0) : array_rand($from, $limit); break; case "database": $where = "1=1 ORDER BY RAND() LIMIT ".$limit; $from = safe_column($srcpt[1], $srcpt[0], $where, $debug); $limit = count($from); $items = ($item_num != '') ? ((count($from) >= $item_num) ? $item_num : 0) : array_keys($from); break; case "file": $path = $txpcfg['doc_root'].((substr($srcpt[0], 0, 1)=='/') ? '' : '/').$srcpt[0]; if ($debug) { echo "++ FILE PATH ++"; dmp ($path); } if (file_exists($path)) { $from = doArray(file($path), 'rtrim'); if ($file_delim != '\n') { $from = explode($file_delim, $from[0]); } $limit = (count($from) < $limit) ? count($from) : $limit; $items = ($item_num != '') ? ((count($from) >= $item_num) ? $item_num : 0) : array_rand($from, $limit); } break; case "string": default: $from = $srcpt; $limit = (count($from) < $limit) ? count($from) : $limit; $items = ($item_num != '') ? ((count($from) >= $item_num) ? $item_num : 0) : array_rand($from, $limit); break; } // Force array if it isn't already (some PHP functions return single values) if (!is_array($items)) { $items = array($items); } if ($shuffle) { shuffle($items); } $from = ($trim) ? doArray($from, 'trim') : $from; if ($debug) { echo "++ ITEM POOL ++"; dmp($from); echo "++ CHOSEN ITEMS ++"; dmp($items); } if ($debug > 2) { echo "++ PRETEXT ++"; dmp($pretext); echo "++ THIS FILE ++"; dmp($thisfile); echo "++ THIS LINK ++"; dmp($thislink); echo "++ THIS IMAGE ++"; dmp($thisimage); echo "++ THIS ARTICLE ++"; dmp($thisarticle); } // Find out how to plonk the output $what = ($form) ? fetch_form($form) : (($thing) ? $thing : '{smd_rnd_txt}'); // Pick the given $items keys out of the $from array and display them $out = array(); $ctr = ($count=="up") ? 0 : $limit-1; foreach ($items as $item) { $replacement = array( '{smd_rnd_txt}' => ( ($escape == 'html') ? htmlspecialchars($from[$item]) : $from[$item] ), '{smd_rnd_txt_rows}' => $limit, '{smd_rnd_txt_thisindex}' => $ctr, '{smd_rnd_txt_thisrow}' => $ctr + 1, '{smd_rnd_txt_chosen_item}' => $item, ); if ($debug > 1) { echo "++ REPLACEMENTS ++"; dmp($replacement); } $out[] = parse(strtr($what, $replacement)); $ctr = ($count=="up") ? $ctr+1 : $ctr-1; } return doWrap($out, $wraptag, $break, $class); }