Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Tags 2 Array

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Brisbane, Australia
    Posts
    181
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Tags 2 Array

    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.

    Code:
    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:

    Code:
    $_ITEM = Array(
       "body => Array
          (
             "bgcolor" => #000000
             "text" => #FFFFFF
          )
    );
    -mR_eGo
    _______________________
    Programming since
    3 years old.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Brisbane, Australia
    Posts
    181
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This is the easiest i could get... though its not the most practical way...

    PHP Code:

    $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;

    -mR_eGo
    _______________________
    Programming since
    3 years old.

  • #3
    New Coder
    Join Date
    Mar 2003
    Location
    Somewhere far beyond
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?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));
    ?>
    WBR, Weirdan.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •