Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F9569843
D107.id256.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D107.id256.diff
View Options
diff --git a/lists/RegexpFactory.php b/lists/RegexpFactory.php
old mode 100755
new mode 100644
--- a/lists/RegexpFactory.php
+++ b/lists/RegexpFactory.php
@@ -5,10 +5,16 @@
*/
class RegexpFactory {
/**
- * The regular expression
- * @var string
+ * The regular expressions
+ * @var string[]
+ */
+ public $expressions;
+
+ /**
+ * The replacement expressions
+ * @var string[]
*/
- public $expression;
+ public $replaceExpressions;
/**
* The last error
@@ -17,49 +23,91 @@
public $lastError;
/**
+ * Expression delimiter
+ */
+ const DELIMITER = "\n";
+
+ /**
* Initializes a new instance of the RegexpFactory object.
*
- * @param string The regular expression
+ * @param string $rexpressions The regular expression
+ * @param string $replaceExpressions The format of the result string
*/
- function __construct ($expression) {
- $this->expression = $expression;
+ function __construct ($expressions, $replaceExpressions) {
+ $this->expressions = explode(static::DELIMITER, $expressions);
+ $this->replaceExpressions = explode(static::DELIMITER, $replaceExpressions);
+
+ if (count($this->expressions) != count($this->replaceExpressions)) {
+ throw new Exception("The number of expressions and replacements should match.");
+ }
}
/**
* Replaces an expression using regexp, a similar way Apache mod_rewrite and Nginx wreplace do.
*
* @param string $haystack The expression to perform a replacement on
- * @param string $replaceExpression The format of the result string
* @return string The replaced string
*/
- function replace ($haystack, $replaceExpression) {
- return preg_replace($this->expression, $replaceExpression, $haystack);
+ function replace ($haystack) {
+ $text = $haystack;
+ for ($i = 0 ; $i < count($this->expressions) ; $i++) {
+ $expression = $this->expressions[$i];
+ $replaceExpression = $this->replaceExpressions[$i];
+ $text = preg_replace($expression, $replaceExpression, $haystack);
+ }
+ return $text;
+ }
+
+ /**
+ * Adds delimiters to each regular expression.
+ */
+ public function addDelimiters () {
+ array_walk($this->expressions, function (&$item) {
+ $item = RegexpFactory::addDelimitersToExpression($item);
+ });
}
/**
- * Encloses the regular expression with delimiters.
+ * Encloses the specified regular expression with delimiters.
+ *
+ * @param string $expression The expression to delimit
+ * @return string The expression with delimiters
*/
- function addDelimiters () {
+ public static function addDelimitersToExpression ($expression) {
$delimiters = ['/', '@', '#', '~', '+', '%', '♦', 'µ', '±', '☞'];
//TODO: check if it's okay to use UTF-8 characters as delimiters
foreach ($delimiters as $delimiter) {
- if (strpos($this->expression, $delimiter) === false) {
- $this->expression = $delimiter . $this->expression . $delimiter;
- return;
+ if (strpos($expression, $delimiter) === false) {
+ return $delimiter . $expression . $delimiter;
+ }
+ }
+ throw new Exception("Can't delimite regexp $expression");
+ }
+
+ /**
+ * Determines if the specified expression block is valid.
+ *
+ * @return bool true if each expression is valid; otherwise, false.
+ */
+ public function isValid () {
+ foreach ($this->expressions as $expression) {
+ if (!$this->isValidExpression($expression)) {
+ return false;
}
}
- throw new Exception("Can't delimite regexp $this->expression");
+ return true;
}
/**
* Determines if the specified expression is valid.
*
+ * @param string the regexp to test
* @return bool true if the expression is valid; otherwise, false.
*/
- public function isValid () {
+ public function isValidExpression ($expression) {
$this->lastError = '';
set_error_handler('self::handleErrors');
- $result = preg_match($this->expression, null);
+ $result = preg_match($expression, null);
restore_error_handler();
if ($this->lastError === '' && $result === false) {
$this->lastError = self::getPCREError();
@@ -83,9 +131,9 @@
return true;
}
message_die(
- GENERAL_ERROR,
- $errstr . "<p>Please report this bug. This error should be handled by the regexp factory.</p>",
- 'Regexp factory error'
+ GENERAL_ERROR,
+ $errstr . "<p>Please report this bug. This error should be handled by the regexp factory.</p>",
+ 'Regexp factory error'
);
}
diff --git a/lists/replace.php b/lists/replace.php
--- a/lists/replace.php
+++ b/lists/replace.php
@@ -19,14 +19,14 @@
$requestSerialized = base64_encode(serialize($_REQUEST));
if (array_key_exists('lists', $_REQUEST) && array_key_exists(0, $_REQUEST['lists']) && array_key_exists('replacement', $_REQUEST)) {
- $regexp = new RegexpFactory($_REQUEST['expression']);
+ $regexp = new RegexpFactory($_REQUEST['expression'], $_REQUEST['replacement']);
$regexp->addDelimiters();
if ($regexp->isValid()) {
$items = explode("\n", $_REQUEST['lists'][0]);
- $replace_callback = function (&$item, $key, $replaceExpression) use ($regexp) {
- $item = $regexp->replace(trim($item), $replaceExpression);
+ $replace_callback = function (&$item, $key) use ($regexp) {
+ $item = $regexp->replace(trim($item));
};
- array_walk($items, $replace_callback, $_REQUEST['replacement']);
+ array_walk($items, $replace_callback);
$result = join("\n", $items);
if ($enable_join) {
$result = join($_REQUEST["joinglue"], $items);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Jun 9, 03:59 (5 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2715155
Default Alt Text
D107.id256.diff (6 KB)
Attached To
Mode
D107: list/replace: multi regexp support
Attached
Detach File
Event Timeline
Log In to Comment