Jump to content

preg_split not working


Altec

Recommended Posts

I'm having some trouble with this code:

function linkcloud_execute() {
    global $mybb, $templates, $link_cloud;
    
    $link_cloud = '';
    $links = preg_split("# | | #",$mybb->settings['linkcloudlinks']);
    
    $link_cloud = $links;
}

 

$mybb->settings['linkcloudlinks'] holds the value of a text area. The value of said text area is:

 

Google,http://www.google.com
Google,http://www.google.com

Bascially I need to split that by lines into an array. $links returns the word "Array" and nothing else.

 

Anyone know what is wrong?

 

Link to comment
https://forums.phpfreaks.com/topic/160596-preg_split-not-working/
Share on other sites

Here is what I have now:

 

function linkcloud_execute() {
   global $mybb, $templates, $link_cloud;
   
   $link_cloud = '';
   $links = preg_split("# | | #",$mybb->settings['linkcloudlinks']);
   
   $cloud = new wordCloud();
   
   foreach($links as $links => $info)
   {
      static $i = 0;
         $links[$i] = explode(',',$info);
         $cloud->addWord(array('word' => $links[$i][0],'url' => $links[$i][1], 'colour' => '#000000'));
      $i++;
   }
   
   $link_cloud = $cloud->showCloud();
}

 

I get "Warning [2] Cannot use a scalar value as an array - Line: 109 - File: inc/plugins/linkcloud.php PHP 5.2.8 (Linux)" three times (each of the loop iterations). I Googled around and it seems like that error is from adding to a zero. This wordCloud class is:

 

class wordCloud
{
   var $version = '2.0';
   var $wordsArray = array();
   
   function __construct($words = false)
   {
      if ($words !== false) {
         // If we have a string
         if (is_string($words)) {
            $this->addString($words);
         } elseif (count($words)) {
            foreach ($words as $key => $value) {
               $this->addWord($value);
            }
         }
      }
      return;
   }
   function wordCloud($words = false)
   {
      return $this->__construct($words);
   }
   function addString($string)
   {
      $string = preg_replace('[^a-z]', '', strip_tags(strtolower($string)));
      $words = array();
      $words = explode(' ', $string);
      if (count($words)) {
         foreach ($words as $key => $value) {
            $this->addWord($value);
         }
      }
   }
   function notify($string, $value)
   {
      echo '<pre>';
      print_r($string);
      print_r($value);
      echo '</pre>';
      return false;
   }
   function addWord($wordAttributes = array())
   {
      if (is_string($wordAttributes)) {
         $wordAttributes = array('word' => $wordAttributes);
      }
      if (!array_key_exists('size', $wordAttributes)) {
         $wordAttributes = array_merge($wordAttributes, array('size' => 1));
      }
      if (!array_key_exists('word', $wordAttributes)) {
         return $this->notify('no word attribute', print_r($wordAttributes, true));
      }
      $word = strtolower($wordAttributes['word']);
      if (empty($this->wordsArray[$word])) {
         $this->wordsArray[$word] = array();
      }
      if (!empty($this->wordsArray[$word]['size']) && !empty($wordAttributes['size'])) {
         $wordAttributes['size'] = ($this->wordsArray[$word]['size'] + $wordAttributes['size']);
      } elseif (!empty($this->wordsArray[$word]['size'])) {
         $wordAttributes['size'] = $this->wordsArray[$word]['size'];
      }
      $this->wordsArray[$word] = $wordAttributes;
      return $this->wordsArray[$word];
   }
   function shuffleCloud()
   {
      $keys = array_keys($this->wordsArray);
      shuffle($keys);
      if (count($keys) && is_array($keys)) {
         $tmpArray = $this->wordsArray;
         $this->wordsArray = array();
         foreach ($keys as $key => $value)
            $this->wordsArray[$value] = $tmpArray[$value];
      }
      return $this->wordsArray;
   }
   function getClassFromPercent($percent)
   {
      if($percent >= 99)
      {
         $class = 9;
      }
      elseif($percent >= 70)
      {
         $class = 8;
      }
      elseif($percent >= 60)
      {
         $class = 7;   
      }
      elseif($percent >= 50)
      {
         $class = 6;   
      }
      elseif($percent >= 40)
      {
         $class = 5;   
      }
      elseif($percent >= 30)
      {
         $class = 4;   
      }
      elseif($percent >= 20)
      {
         $class = 3;   
      }
      elseif($percent >= 10)
      {
         $class = 2;   
      }
      elseif($percent >= 5)
      {
         $class = 1;   
      }
      else
      {
         $class = 0;   
      }
      return $class;
   }
   function setLimit($limit)
   {
      if (!empty($limit)) {
         $this->limitAmount = $limit;
      }
      return $this->limitAmount;
   }
   function limitCloud()
   {
      $i = 1;
      foreach ($this->wordsArray as $key => $value) {
         if ($this->limitAmount < $i) {
            $wordsArray[$value['word']] = $value;
         }
         $i++;
      }
      $this->wordsArray = array();
      $this->wordsArray = $wordsArray;
      return $this->wordsArray;
   }
   function removeWord($word)
   {
      $this->removeWords[] = strtolower($word);
   }
   function removeWords()
   {
      foreach ($this->wordsArray as $key => $value) {
         if (!in_array($value['word'], $this->removeWords)) {
            $wordsArray[$value['word']] = $value;
         }
      }
      $this->wordsArray = array();
      $this->wordsArray = $wordsArray;
      return $this->wordsArray;
   }
   function orderBy($field, $direction = 'ASC')
   {
       return $this->orderBy = array('field' => $field, 'direction' => $direction);
   }
   function orderCloud($unsortedArray, $sortField, $sortWay = 'SORT_ASC')
   {
     $sortedArray = array();
      foreach ($unsortedArray as $uniqid => $row) {
         foreach ($row as $key => $value) {
            $sortedArray[$key][$uniqid] = strtolower($value);
         }
      }
      if ($sortWay) {
         array_multisort($sortedArray[$sortField], constant($sortWay), $unsortedArray);
      }
      return $unsortedArray;
   }
   function getMax()
   {
      $max = 0;
      if (!empty($this->wordsArray)) {
         $p_size = 0;
         foreach ($this->wordsArray as $cKey => $cVal) {
            $c_size = $cVal['size'];
            if ($c_size > $p_size) {
        $max = $c_size; /* @Thanks Morticus */
            $p_size = $c_size;
      }
         }
      }
      return $max;
   }
   function showCloud($returnType = 'html')
   {
      if (empty($this->orderBy)) {
         $this->shuffleCloud();
      } else {
         $orderDirection = strtolower($this->orderBy['direction']) == 'desc' ? 'SORT_DESC' : 'SORT_ASC';
    $this->wordsArray = $this->orderCloud($this->wordsArray, $this->orderBy['field'], $orderDirection);
      }
      if (!empty($this->limitAmount)) {
         $this->limitCloud();
      }
      if (!empty($this->removeWords)) {
         $this->removeWords();
      }
      $this->max = $this->getMax();
      if (is_array($this->wordsArray)) {
         $return = ($returnType == 'html' ? '' : ($returnType == 'array' ? array() : ''));
         foreach ($this->wordsArray as $word => $arrayInfo) {
            $sizeRange = $this->getClassFromPercent(($arrayInfo['size'] / $this->max) * 100);
            $arrayInfo['range'] = $sizeRange;
            if ($returnType == 'array') {
               $return [$word] = $arrayInfo;
            } elseif ($returnType == 'html') {
               $return .= "<span class='word size{$sizeRange}'>   {$arrayInfo['word']}   </span>";
            }
         }
         return $return;
      }
   }
}

$link_cloud is simply echo'd on to the page.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.