Skip to content

Instantly share code, notes, and snippets.

@stephaneIBANEZ
Created February 23, 2017 10:44
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 stephaneIBANEZ/77789f2e26ac30dae7863db0a02c6cc0 to your computer and use it in GitHub Desktop.
Save stephaneIBANEZ/77789f2e26ac30dae7863db0a02c6cc0 to your computer and use it in GitHub Desktop.
String transformations
<?php
namespace Inserm_RadicoBundle\Entity;
class Grep {
protected $_code = null;
protected $_itemNumber = 0;
protected $_replaceList = array();
protected $_search = array();
protected $_replace = array();
protected $_renderList = array();
protected $_regex = '';
public function __construct() {
return $this;
}
private function _trace($label, $array, $return = false) {
$result = "<br><b>$label: </b> count: " . count($array) . "<pre>";
$result .= print_r($array, true);
$result .= "</pre>";
if (!$return) {
echo $result;
} else
return $result;
}
private function _check($list, $listName = 'list') {
$error = false;
$count = count($list);
if ($listName == '_renderList') {
$expected = ($this->_itemNumber * 2) + 1;
if ($count != $expected) {
$error = true;
}
} else {
$expected = $this->_itemNumber;
if ($count != $this->_itemNumber) {
$error = true;
}
}
if ($error) {
$excep = "<br><font-color='red'>Count of items don't match with the $listName ! Expected $expected, found: $count items</font>";
$excep .= $this->_trace($listName, $list, true);
throw new Exception($excep);
}
}
private function _explodeList($list) {
$regex = "#(\(.+?\))#";
preg_match_all($regex, $list, $matches);
for ($i = 1; $i < count($matches); $i++) {
$result = str_replace(array('(', ')'), array('', ''), $matches[$i]);
}
return $result;
}
public function setRegex($regex) {
$this->_regex = $regex;
}
public function setReplaceList($list='') {
if ($list == '') {
$this->_replaceList = array();
$this->_search = array();
$this->_replace = array();
return;
}
$tmp = $this->_explodeList($list);
foreach ($tmp as $block => $param) {
$var[$block + 1] = explode(";", $param);
}
unset($tmp);
$this->_replaceList = $var;
unset($var);
for ($i = 1; $i <= count($this->_replaceList); $i++) {
$searchTxt = $this->_replaceList[$i][0];
if ($searchTxt != '') {
$this->_search[$i] = str_replace("'", "", explode(',', $searchTxt));
} else {
$this->_search[$i] = array('');
}
$replaceTxt = $this->_replaceList[$i][1];
if ($replaceTxt != '') {
$this->_replace[$i] = str_replace("'", "", explode(',', $replaceTxt));
;
} else {
$this->_replace[$i] = array('');
}
}
}
public function setRenderList($list) {
if ($list == '')
return;
$this->_renderList = $this->_explodeList($list);
}
public function setJonParameters($json) {
$o = json_decode($json);
$params = get_object_vars($o);
$this->setRegex($params['regex']);
if (array_key_exists('replaceList', $params)) {
$this->setReplaceList($params['replaceList']);
} else $this->setReplaceList();
$this->setRenderList($params['renderList']);
}
public function render($code) {
preg_match_all($this->_regex, $code, $matchesTest);
$expected = count($matchesTest) - 1;
$this->_itemNumber = $expected;
if ($expected) {
$this->_itemNumber = $expected;
} else {
$excep = "<br><font-color='red'>There is a problem in the regular expression !!</font>";
$excep .= $this->_trace($listName, $list, true);
throw new Exception($excep);
}
if (count($this->_replaceList) > 0)
$this->_check($this->_replaceList, $listName = '_replaceList');
$this->_check($this->_renderList, $listName = '_renderList');
return preg_replace_callback($this->_regex, function($matches) {
$replaced = array();
$ordered = array();
$result = '';
for ($i = 1; $i < count($matches); $i++) {
if (count($this->_replaceList) > 0) {
$replaced[$i] = str_replace($this->_search[$i], $this->_replace[$i], $matches[$i]);
} else {
$replaced[$i] = $matches[$i];
}
}
for ($i = 0; $i < count($this->_renderList); $i++) {
if ($i % 2 == 0) {
// even = separator
$result .= str_replace("'", '', $this->_renderList[$i]);
} else {
//odd = block
if ($this->_renderList[$i][0] != "!")
$result .= $replaced[$this->_renderList[$i]];
}
}
$matches = $result;
return $result;
}, $code
);
}
}
/* EXAMPLE OF USE */
/*
echo "<h1>MASKAGE</h1>";
$idmr = "01234567890123456789";
print "<br>IDMR: $idmr<br>";
$maskIdmr = new Grep();
$idmrParams = array(
'regex' => "#(.{4})(.{4})(.{4})(.{4})(.{4})#",
'renderList' => "('')(1)(' ')(2)(' ')(3)(' ')(4)(' ')(5)('')",
);
$idmrJson = json_encode($idmrParams);
echo "<br> IDMR JSON: $idmrJson<br>";
//echo "<br> decode: ".print_r(json_decode($idmrJson));
$maskIdmr->setJonParameters($idmrJson);
echo "<font color='red'>";
$idmrGreped = $maskIdmr->render($idmr);
echo $idmrGreped;
echo "</font>";
echo "<hr>";
echo "<h1>DE-MASKAGE</h1>";
echo "<br> IDMR Masqué: $idmrGreped<br>";
$idmrUnmask = new Grep();
$idmrUnmaskParams = array(
'regex' => "#(.{4}) (.{4}) (.{4}) (.{4}) (.{4})#",
'replaceList' => "(' ';'')(' ';'')(' ';'')(' ';'')(' ';'')",
'renderList' => "('')(1)('')(2)('')(3)('')(4)('')(5)('')"
);
$idmrUnmaskJson = json_encode($idmrUnmaskParams);
echo "<br>UNMASK IDMR JSON: $idmrUnmaskJson<br>";
$idmrUnmask->setJonParameters($idmrUnmaskJson);
echo "<font color='red'>";
echo $idmrUnmask->render($idmrGreped);
echo "</font>";
////////////////////////////// RADICO //////////////////////////////
echo "<hr>";
echo "<h1>MASKAGE</h1>";
$radico = "xxFRxPAR000010001848";
print "<br>Radico: (" . mb_strlen($radico) . "): $radico<br>";
$maskRadico = new Grep();
$radicoParams = array(
'regex' => "#(.{4})(.{4})(.{5})(.{7})#",
'replaceList' => "('x';'')('x';'')('';'')('';'')",
'renderList' => "('')(1)('')(!2)('')(3)('-')(4)('')"
);
$radicoJson = json_encode($radicoParams);
echo "<br> RADICO JSON: $radicoJson<br>";
//echo "<br> decode: ".print_r(json_decode($radicoJson));
$maskRadico->setJonParameters($radicoJson);
echo "<font color='red'>";
$radicoGreped = $maskRadico->render($radico);
echo $radicoGreped;
echo "</font>";
echo "<h1>DE-MASKAGE</h1>";
echo "<br> RADICO Masqué: $radicoGreped<br>";
$radicoUnmask = new Grep();
$radicoUnmaskParams = array(
'regex' => "#(.*)(.{5})-(.{7})#",
'renderList' => "('*')(1)('*')(2)('')(3)('*')"
);
$radicoUnmaskJson = json_encode($radicoUnmaskParams);
echo "<br>UNMASK RADICO JSON: $radicoUnmaskJson<br>";
$radicoUnmask->setJonParameters($radicoUnmaskJson);
echo "<font color='red'>";
echo $radicoUnmask->render($radicoGreped);
echo "</font>";
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment