PKPString::iterativeExplode PHP Method

iterativeExplode() static public method

Iterate over an array of delimiters and see whether it exists in the given input string. If so, then use it to explode the string into an array.
static public iterativeExplode ( $delimiters, $input ) : array
$delimiters array
$input string
return array
    static function iterativeExplode($delimiters, $input)
    {
        // Run through the delimiters and try them out
        // one by one.
        foreach ($delimiters as $delimiter) {
            if (strstr($input, $delimiter) !== false) {
                return explode($delimiter, $input);
            }
        }
        // If none of the delimiters works then return
        // the original string as an array.
        return array($input);
    }

Usage Example

 /**
  * Converts a string with multiple persons
  * to an array of NLM name descriptions.
  *
  * @param $personsString string
  * @param $title boolean true to parse for title
  * @param $degrees boolean true to parse for degrees
  * @return array an array of NLM name descriptions or null
  *  if the string could not be converted plus optionally a
  *  single 'et-al' string.
  */
 function &_parsePersonsString($personsString, $title, $degrees)
 {
     // Check for 'et al'.
     $personsStringBeforeEtal = PKPString::strlen($personsString);
     $personsString = PKPString::regexp_replace('/et ?al$/', '', $personsString);
     $etAl = $personsStringBeforeEtal == PKPString::strlen($personsString) ? false : true;
     // Remove punctuation.
     $personsString = trim($personsString, ':;, ');
     // Cut the authors string into pieces.
     $personStrings = PKPString::iterativeExplode(array(':', ';'), $personsString);
     // If we did not have success with simple patterns then try more complex
     // patterns to tokenize multiple-person strings.
     if (count($personStrings) == 1) {
         // The first pattern must match the whole string, the second is used
         // to extract names.
         $complexPersonsPatterns = array(array('/^((([^ \\t\\n\\r\\f\\v,.&]{2,}\\s*)+,\\s*([A-Z]\\.\\s*)+),\\s*)+(\\&|\\.\\s\\.\\s\\.)\\s*([^ \\t\\n\\r\\f\\v,.&]{2,}\\s*,\\s*([A-Z]\\.\\s*)+)$/i', '/(?:[^ \\t\\n\\r\\f\\v,.&]{2,}\\s*)+,\\s*(?:[A-Z]\\.\\s*)+/i'), array('/^((([^ \\t\\n\\r\\f\\v,&]+\\s+)+[^ \\t\\n\\r\\f\\v,&]+\\s*)[,&]\\s*)+(([^ \\t\\n\\r\\f\\v,&]+\\s+)+[^ \\t\\n\\r\\f\\v,&]+)/i', '/(?:(?:[^ \\t\\n\\r\\f\\v,&.]+|[^ \\t\\n\\r\\f\\v,&]{2,})\\s+)+(?:[^ \\t\\n\\r\\f\\v,&.]+|[^ \\t\\n\\r\\f\\v,&]{2,})/i'));
         $matched = false;
         foreach ($complexPersonsPatterns as $complexPersonsPattern) {
             // Break at the first pattern that matches.
             if ($matched = PKPString::regexp_match($complexPersonsPattern[0], $personsString)) {
                 // Retrieve names.
                 $success = PKPString::regexp_match_all($complexPersonsPattern[1], $personsString, $personStrings);
                 assert($success && count($personStrings) == 1);
                 $personStrings = $personStrings[0];
                 break;
             }
         }
         if (!$matched) {
             // If nothing matches then try to parse as a single person.
             $personStrings = array($personsString);
         }
     }
     // Parse persons.
     $persons = array();
     foreach ($personStrings as $personString) {
         $persons[] =& $this->_parsePersonString($personString, $title, $degrees);
     }
     // Add et-al string.
     if ($etAl) {
         $persons[] = PERSON_STRING_FILTER_ETAL;
     }
     return $persons;
 }
All Usage Examples Of PKPString::iterativeExplode