...

View Full Version : Tags 2 Array



mr_ego
11-14-2003, 07:12 AM
I'm trying to write a regular expression (doesn't have to be a reg-ex, but its probably the easiest way), that when you pass a body tag through to it, it gives you the colour of the background and the colour of text on that page in an Array.



So when you pass:

<body bgcolor="#FFFFFF" text="#000000">

You Get (after running print_r):

Array
(
[background] => #FFFFFF
[textcolour] => #000000
)


Any Ideas?

OR

Is there a function in PHP that changes html text into an Array? EG:



$_ITEM = Array(
"body => Array
(
"bgcolor" => #000000
"text" => #FFFFFF
)
);

mr_ego
11-14-2003, 07:28 AM
This is the easiest i could get... though its not the most practical way...




$body = "bgcolor=\"#000000\" text=\"#FFFFFF\"";
print_r(page_splittag($body));

function page_splittag($body) {
$_TAG = Array();
$_PROP = preg_split("/\ /", $body);
foreach ($_PROP AS $value) {
$_VALUES = preg_split("/\=/", $value);
$_TAG[trim($_VALUES[0])] =
substr(trim($_VALUES[1]), 1, (strlen($_VALUES[1]) - 2));
}
return $_TAG;
}

Weirdan
11-14-2003, 09:12 PM
<?php
$tst="<body bgcolor=\"#FFFFFF\" text=\"#000000\" selected>";
function split_tag($tag){
preg_match_all("/\s(\w+)\s*(=?\s*\"([^\"]*)\")?/",$tag,$result);
foreach($result[1] as $key=>$val)
$ret[$val]=empty($result[3][$key])?true:$result[3][$key];
return @$ret;
}
print_r(split_tag($tst));
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum