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 9 of 9

Thread: split the class

  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    split the class

    Hi,

    <table class="tableclass"><tr height="50" class="test" ><td class="tdclass"></td></tr></table>

    This one how to split like this

    1.class="tableclass"
    2.class="test"
    3.class="tdclass"


    please help me.
    Last edited by anandselvaraj; 12-22-2010 at 01:17 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Is this supposed to be in the PHP forum? What are you wanting to do, read this html in and retrieve the classes into an array or something along that lines?
    I believe that the element tag only accepts 1 class attribute. You can use multiple classes within it by separating them with spaces.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Is this supposed to be in the PHP forum? What are you wanting to do, read this html in and retrieve the classes into an array or something along that lines?
    I believe that the element tag only accepts 1 class attribute. You can use multiple classes within it by separating them with spaces.
    Yes i read the file but i cant get class="test".

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    A simple preg_match_all will fetch the classes into an array. I don't know how you want to identify the array though; we can retrieve the classes, but are you wanting to create an associative array with the element name as an index or do you need to track it at all?
    I need to see example input and expected output in order to make this determination.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    Dec 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I want to print all directory files css classes.

  • #6
    New Coder
    Join Date
    Dec 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    A simple preg_match_all will fetch the classes into an array. I don't know how you want to identify the array though; we can retrieve the classes, but are you wanting to create an associative array with the element name as an index or do you need to track it at all?
    I need to see example input and expected output in order to make this determination.
    <?php

    error_reporting(E_NOTICE | E_ALL);

    set_time_limit(60);



    global $in_string, $out_string ;

    $in_string = "class=";
    //$out_string = "black_heading";
    $cnt = 0;

    process('./');

    function do_check($path)
    { global $in_string, $out_string ;

    // echo "in fn do_check in is $in_string out is $out_string <br>";

    if (filesize($path)>250000)
    {
    echo "<span style='background: blue'>FILE TOO LONG $path</span>";
    return;
    }

    // echo $path.'<br>';

    $lines = file ( $path );

    // Loop through our array, show HTML source as HTML source; and line numbers too.
    $content = "";

    $cnt = 1;
    foreach ( $lines as $line_num => $line ) {

    // echo "processing $cnt $line_num $line<br/>";
    if ( strpos($line, $in_string) !== false) {
    $line_num = $line_num + 1;
    //echo "string==>".stripslashes($in_string)."<br>\n";
    //echo "count==>".$cnt."<br>";
    //echo "path==>".$path."<br>";
    //echo "line number==>".$line_num."<br>";
    //echo htmlspecialchars($line, ENT_NOQUOTES, "UTF-8");
    $linesss = str_replace(array("&lt;", "&gt;"), array("<", ">"), htmlspecialchars($line, ENT_NOQUOTES, "UTF-8"));
    echo "<br><br><br>";
    // replace with out_string
    $cnt++;
    }

    }


    }


    function process($path)
    { global $in_string, $out_string ;
    // echo "in fn process in is $in_string out is $out_string <br>";

    //echo "<span class='cd'>-]</span> $path<br>";
    $array_ext_mask_lowercase = array('html', 'htm', 'js', 'php', 'tpl');

    if ($handle = opendir($path))
    {
    /* To jest poprawna metoda */
    while (false !== ($file = readdir($handle)))
    {
    if ($file=='.' || $file=='..')
    continue;

    $npath = str_replace('//', '/', $path.'/'.$file);
    if (strpos($file, '.') !== false)
    {
    $ext = strtolower(substr($file, strrpos($file, '.')+1));
    if (in_array($ext, $array_ext_mask_lowercase))
    do_check($npath);
    }
    if (is_dir($npath))
    process($npath);
    }
    }
    }

    function readdir_arr($path, $array_ext_mask_lowercase)
    { global $in_string, $out_string ;
    // echo "in fn readdir_arr in is $in_string out is $out_string <br>";

    if (!is_array($array_ext_mask_lowercase))
    $array_ext_mask_lowercase = array($array_ext_mask_lowercase);

    $ret = array();

    if ($handle = opendir($path))
    {
    /* To jest poprawna metoda */
    while (false !== ($file = readdir($handle)))
    {
    if (strpos($file, '.')===false)
    continue;

    $ext = strtolower(substr($file, strrpos($file, '.')+1));
    if (in_array($ext, $array_ext_mask_lowercase))
    $ret[]= str_replace('//', '/', $path.'/'.$file);
    }
    }

    return $ret;
    }

    ?>

  • #7
    New Coder
    Join Date
    Dec 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by anandselvaraj View Post
    <?php

    error_reporting(E_NOTICE | E_ALL);

    set_time_limit(60);



    global $in_string, $out_string ;

    $in_string = "class=";
    //$out_string = "black_heading";
    $cnt = 0;

    process('./');

    function do_check($path)
    { global $in_string, $out_string ;

    // echo "in fn do_check in is $in_string out is $out_string <br>";

    if (filesize($path)>250000)
    {
    echo "<span style='background: blue'>FILE TOO LONG $path</span>";
    return;
    }

    // echo $path.'<br>';

    $lines = file ( $path );

    // Loop through our array, show HTML source as HTML source; and line numbers too.
    $content = "";

    $cnt = 1;
    foreach ( $lines as $line_num => $line ) {

    // echo "processing $cnt $line_num $line<br/>";
    if ( strpos($line, $in_string) !== false) {
    $line_num = $line_num + 1;
    //echo "string==>".stripslashes($in_string)."<br>\n";
    //echo "count==>".$cnt."<br>";
    //echo "path==>".$path."<br>";
    //echo "line number==>".$line_num."<br>";
    //echo htmlspecialchars($line, ENT_NOQUOTES, "UTF-8");
    $linesss = str_replace(array("&lt;", "&gt;"), array("<", ">"), htmlspecialchars($line, ENT_NOQUOTES, "UTF-8")); //Here I am geeting html line by line. in that html line have classes i have to view.
    echo "<br><br><br>";
    // replace with out_string
    $cnt++;
    }

    }


    }


    function process($path)
    { global $in_string, $out_string ;
    // echo "in fn process in is $in_string out is $out_string <br>";

    //echo "<span class='cd'>-]</span> $path<br>";
    $array_ext_mask_lowercase = array('html', 'htm', 'js', 'php', 'tpl');

    if ($handle = opendir($path))
    {
    /* To jest poprawna metoda */
    while (false !== ($file = readdir($handle)))
    {
    if ($file=='.' || $file=='..')
    continue;

    $npath = str_replace('//', '/', $path.'/'.$file);
    if (strpos($file, '.') !== false)
    {
    $ext = strtolower(substr($file, strrpos($file, '.')+1));
    if (in_array($ext, $array_ext_mask_lowercase))
    do_check($npath);
    }
    if (is_dir($npath))
    process($npath);
    }
    }
    }

    function readdir_arr($path, $array_ext_mask_lowercase)
    { global $in_string, $out_string ;
    // echo "in fn readdir_arr in is $in_string out is $out_string <br>";

    if (!is_array($array_ext_mask_lowercase))
    $array_ext_mask_lowercase = array($array_ext_mask_lowercase);

    $ret = array();

    if ($handle = opendir($path))
    {
    /* To jest poprawna metoda */
    while (false !== ($file = readdir($handle)))
    {
    if (strpos($file, '.')===false)
    continue;

    $ext = strtolower(substr($file, strrpos($file, '.')+1));
    if (in_array($ext, $array_ext_mask_lowercase))
    $ret[]= str_replace('//', '/', $path.'/'.$file);
    }
    }

    return $ret;
    }

    ?>
    help me soon.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    In the future please wrap code in [code][/code] or [php][/php] tags to preserve the formatting.

    So you are just wanting to print out all used CSS classes within a file?
    A simple preg is the easiest way to do this, and will happen in the do_check function. I'd read this as a string since you can match once with the preg instead of each line, plus most of do_check does nothing. I'd strongly recommend the removal of your global variables as well and opt to pass in any parameters you need or use constants if its for read only.
    PHP Code:
    function do_check($path)
    { global 
    $in_string$out_string ;

    // echo "in fn do_check in is $in_string out is $out_string <br>";

    if (filesize($path)>250000)
    {
    echo 
    "<span style='background: blue'>FILE TOO LONG $path</span>";
    return;
    }

    // echo $path.'<br>';
        
    if (false !== ($sFile = @file_get_contents($path)))
        {
            
    $sPattern '#class=("|\')([^{\1}]+)\1#miU';
            
    $aMatches = array();
            if (
    preg_match_all($sPattern$sFile$aMatches))
            {
                
    $aClasses = array();
                foreach (
    $aMatches[2] AS $class)
                {
                    
    $parts explode(" "$class);
                    
    $aClasses array_merge($parts$aClasses);
                }
            }
        }

    $aClasses will contain all classes. Use array_unique to remove duplicates.
    $aMatches itself will contain the full block (the class=...) and the quotations in use. Those will be offsets 0 and 1.
    If you need this line by line then I wouldn't match it as that will be slow. You would need to use strpos with substr to essentially cut out sections of the string to assign the part to a variable. Not sure why you'd want to do that though.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    New Coder
    Join Date
    Dec 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you very much....its working fine.


  •  

    Tags for this Thread

    Posting Permissions

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