Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created October 2, 2019 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobus1998/460e608f1d510d4d25a12f1d73bcd745 to your computer and use it in GitHub Desktop.
Save kobus1998/460e608f1d510d4d25a12f1d73bcd745 to your computer and use it in GitHub Desktop.
regex matching
<?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