static public regexp_match_all ( $pattern, $subject, &$matches ) : integer | boolean | ||
$pattern | string Regular expression | |
$subject | string String to apply regular expression to | |
$matches | array Reference to receive matches | |
return | integer | boolean | Returns number of full matches of given subject, or FALSE if an error occurred. |
/** * 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; }
/** * @see Filter::process() * @param $citationDescription MetadataDescription * @return string a DOI or null */ function &process(&$citationDescription) { $nullVar = null; // Get the search strings $searchTemplates =& $this->_getSearchTemplates(); $searchStrings = $this->constructSearchStrings($searchTemplates, $citationDescription); // Run the searches, in order, until we have a result $searchParams = array('qt' => 'worldcat_org_all'); foreach ($searchStrings as $searchString) { $searchParams['q'] = $searchString; // Worldcat Web search; results are (mal-formed) XHTML if (is_null($result = $this->callWebService(WORLDCAT_WEBSERVICE_SEARCH, $searchParams, XSL_TRANSFORMER_DOCTYPE_STRING))) { return $nullVar; } // parse the OCLC numbers from search results PKPString::regexp_match_all('/id="itemid_(\\d+)"/', $result, $matches); if (!empty($matches[1])) { break; } } // If we don't have an OCLC number, then we cannot get any metadata if (empty($matches[1])) { return $nullVar; } // use xISBN because it's free foreach ($matches[1] as $oclcId) { $isbns = $this->_oclcToIsbns($oclcId); if (is_array($isbns)) { break; } } if (is_null($isbns)) { return $nullVar; } $apiKey = $this->getApiKey(); if (empty($apiKey)) { // Use the first ISBN if we have multiple $citationDescription =& $this->_lookupXIsbn($isbns[0]); return $citationDescription; } elseif (!empty($isbns[0])) { // Worldcat lookup only works with an API key if (is_null($citationDescription =& $this->_lookupWorldcat($matches[1][0]))) { return $nullVar; } // Prefer ISBN from xISBN if possible if (!empty($isbns[0])) { $citationDescription->addStatement('ibsn', $isbns[0], null, true); } return $citationDescription; } // Nothing found return $nullVar; }
/** * Given a locale string, get the list of parameter references of the * form {$myParameterName}. * @param $source string * @return array */ static function getParameterNames($source) { $matches = null; PKPString::regexp_match_all('/({\\$[^}]+})/', $source, $matches); array_shift($matches); // Knock the top element off the array if (isset($matches[0])) { return $matches[0]; } return array(); }
/** * Parse SQL content into individual SQL statements. * @param $sql string * @return array */ function &parseStatements(&$sql) { $statements = array(); $statementsTmp = explode($this->statementDelim, $sql); $currentStatement = ''; $numSingleQuotes = $numEscapedSingleQuotes = 0; // This method for parsing the SQL statements was adapted from one used in phpBB (http://www.phpbb.com/) for ($i = 0, $count = count($statementsTmp); $i < $count; $i++) { // Get total number of single quotes in string $numSingleQuotes += PKPString::substr_count($statementsTmp[$i], "'"); // Get number of escaped single quotes $numEscapedSingleQuotes += PKPString::regexp_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $statementsTmp[$i], $matches); $currentStatement .= $statementsTmp[$i]; if (($numSingleQuotes - $numEscapedSingleQuotes) % 2 == 0) { // Even number of unescaped single quotes, so statement must be complete if (trim($currentStatement) !== '') { array_push($statements, trim($currentStatement)); } $currentStatement = ''; $numSingleQuotes = $numEscapedSingleQuotes = 0; } else { // The statement is not complete, the delimiter must be inside the statement $currentStatement .= $this->statementDelim; } } return $statements; }