OK, so if you remove everything within the php tags in the first post, and insert this instead:
error_reporting(E_ALL | E_STRICT); // keep this here to check that you code doesn't cough up warnings and / or errors
class regex_match {
public $string;
public $pattern;
public $mode;
public static $fetchArr = array();
public $arrGhost = array();
public $matches;
public function __construct($string = 'NULL', $pattern = '#.*#', $mode = 'false') {
$this->mode = $mode;
$this->pattern = $pattern;
$this->string = $string;
echo '<div class="div_preg_match">' . "\n";
if($this->mode){ // preg_match_all
echo '<p><span style="color: #C3CBD4;">preg_match_all (\' </span>' . $pattern . '<span style="color: #C3CBD4;"> \', $str, $matches)</span></p>';
preg_match_all($this->pattern, $this->string, $this->matches, PREG_OFFSET_CAPTURE);
} else { // preg_match
echo '<p><span style="color: #C3CBD4;">preg_match (\' </span>' . $pattern . '<span style="color: #C3CBD4;"> \', $str, $matches)</span></p>';
preg_match($this->pattern, $this->string, $this->matches, PREG_OFFSET_CAPTURE);
}
array_walk_recursive($this->matches, 'fetchArrayValues'); // fetch root array values
$this->setArrayFlags();
}
public function setArrayFlags(){
// flag matched or captured characters
$total = count(self::$fetchArr);
for ($a = 0, $b = 1; $a < $total ; $a+=2, $b+=2) {
$strlen = strlen(self::$fetchArr[$a]);
for ($x = 0 ; $x < $strlen ; $x++) {
if(!isset( $this->arrGhost[$x+(self::$fetchArr[$b])] )){
$this->arrGhost[$x+(self::$fetchArr[$b])] = '0'; // set to matched
} else {
$this->arrGhost[$x+(self::$fetchArr[$b])] += 1; // already set.. advance to next setting (captured, sub-captured, etc...);
}
}
}
self::$fetchArr = array();
$this->display();
}
public function display(){
// output the string onscreen
echo "<br />\n";
$count = strlen($this->string);
for ($i = 0 ; $i < $count ; $i++) {
if(isset($this->arrGhost[$i])){
echo '<p class="element capture'. $this->arrGhost[$i] . '">' . $this->string[$i] . '</p>';
} else {
echo '<p class="element">' . $this->string[$i] . '</p>'; // non matching nor capturing element
}
}
echo "\n" . '</div>' . "\n\n"; // closes div_preg_match
}
}
// fetch the array values from preg_match(_all)? regex array $matches
function fetchArrayValues($item, $key){
return regex_match::$fetchArr[] = $item;
}
// Let's see some examples!
$example01 = new regex_match('I am a string! Therefore I am still alive!', '#s(tring)!#', true); // ($string, $pattern, $mode)
$example02 = new regex_match('I can has cheezburger?', '#[^u]+#', false); // ($string, $pattern, $mode)
It works! I re-wrote it as a class... and it validates.
BUT...
It's not quite where I want it to be.. you'll notice that one of the variables is static.
public static $fetchArr = array();
Why you ask? Because I cannot seem to get the array_walk_recursive function to properly call a method within the class.. For example, suppose I inserted a method within the class like so:
public function test(){
echo 'I am a test';
}
And suppose I wanted array_walk_recursive to point to this method as its callback:
array_walk_recursive($this->matches, $this->test()); // fetch root array values
It will execute what is inside the test method, but not without a warning:
'Warning: array_walk_recursive() [function.array-walk-recursive]: Wrong syntax for function name...'
I have tried surrounding $this->test() in quotes (which now generates an error - function does not exist), also tried using self::test() (same initial warning as before).
So to make a long story short, I left the callback function within array_walk_recursive as a function outside of the class.. the final caveat as a result, after I am done setting the flags (keys / values) within $arrGhost, I must reset the fetchArr array as such:
self::$fetchArr = array();
Otherwise, the results from the previous example are included into the results of the following example (which messes the results from here on out).
If anyone can show me how to successfully tap into a method within the class as a callback from array_walk_recursive (without producing any warnings or errors), I can convert the $fetchArr array from static to simply public, and do everything inside class.
Cheers
EDIT - as a default, I just made everything public.. I'll end up privatising in the end.