...

View Full Version : Outputting metacharacters from regex array



Ahlahn
05-29-2011, 06:24 PM
Hey guys,

I'm trying to get this regex match to output an array with all the characters that were successfully matched, but it's excluding the <h1></h1> characters since those are interpreted as HTML. I know htmlentities could be useful, but I'm not sure how I would implemented within this script ->



$regexMatch = preg_match_all($expression, $string, $matches,PREG_OFFSET_CAPTURE);

if($array > 0){
echo "Sucess";
echo "<pre>";
print_r($matches);
echo "</pre>";
}
else
echo "No Match";


$matches represents the array containing the matched items- how do I get it to display meta-characters as well?

Here are the outputs


Expression = "/Hello/"
String = "Hello"

(
[0] => Array
(
[0] => Array
(
[0] => Hello
[1] => 0
)

)

)


but....


$expression = "/\<h1\>hello\<\/h1\>/"
$string = "<h1>hello</h1>"

Array
(
[0] => Array
(
[0] => Array
(
[0] =>
Hello]

[1] => 0
)

)

)

Fou-Lu
05-29-2011, 07:06 PM
I fail to see this as an actual problem, but if you want to display this in a visible format, you can walk the array against the htmlspecialchars with a custom function:


function htmlspecialcharsWalk(&$item)
{
if (is_string($item))
{
$item = htmlspecialchars($item);
}
}

$visible = $array;
array_walk_recursive($visible, 'htmlspecialcharsWalk');
printf("<pre>%s</pre>", print_r($visible, true));

Ahlahn
05-30-2011, 02:39 PM
That worked beautifully- thank you so much. I've never heard of the array_walk_recursive method... very cool.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum