Created
October 2, 2019 08:40
-
-
Save kobus1998/460e608f1d510d4d25a12f1d73bcd745 to your computer and use it in GitHub Desktop.
regex matching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Match | |
{ | |
public function __construct($pattern, $data, $match) | |
{ | |
$this->pattern = $pattern; | |
$this->data = $data; | |
$this->match = $match; | |
} | |
public function hasMatch() | |
{ | |
return $this->match != null; | |
} | |
public function getResult() | |
{ | |
return $this->match; | |
} | |
public function getData() | |
{ | |
return $this->data; | |
} | |
public function replace($data) | |
{ | |
$this->data = preg_replace($this->pattern, $data, $this->data); | |
return $this; | |
} | |
} | |
class Regex | |
{ | |
public static function match($pattern, $data) | |
{ | |
preg_match($pattern, $data, $match); | |
return new Match($pattern, $data, $match); | |
} | |
} | |
$match = Regex::match("/abc/", "abcdefg"); | |
echo $match->hasMatch() ? "yes\n" : "no\n"; | |
$match->replace(""); | |
echo $match->getData(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment