jessicakennedy1028

String to Array

Sep 9th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.95 KB | None | 0 0
  1.     function stringtoArray($string = null, $keySep = null, $valSep = null) {
  2.         $keySep == '' ? $keySep = '==' : $keySep;
  3.         $valSep == '' ? $valSep = '&&' : $valSep;
  4.        
  5.         $chunks = array_chunk(preg_split('/('.$keySep.'|'.$valSep.')/', $string), 2);
  6.         $defined = array_combine(array_column($chunks, 0), array_column($chunks, 1));
  7.  
  8.         if (is_array($defined)) {
  9.             $output = $defined;
  10.         }
  11.         else {
  12.             $output = '<p>Your string is malformed, here is a use of a define example:</p><p>$string="example'.$keySep.'a thing characteristic of its kind or illustrating a general rule.'.$valSep.'test'.$keySep.'a procedure intended to establish the quality, performance, or reliability of something, especially before it is taken into widespread use.'.$valSep.'demonstrate'.$keySep.'clearly show the existence or truth of (something) by giving proof or evidence."</p>';
  13.         }
  14.         return $output;
  15.     }
Add Comment
Please, Sign In to add comment