...

View Full Version : split the class



anandselvaraj
12-22-2010, 10:04 AM
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.

Fou-Lu
12-22-2010, 01:11 PM
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.

anandselvaraj
12-22-2010, 01:20 PM
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".

Fou-Lu
12-22-2010, 01:40 PM
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.

anandselvaraj
12-22-2010, 01:49 PM
I want to print all directory files css classes.

anandselvaraj
12-22-2010, 01:51 PM
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;
}

?>

anandselvaraj
12-22-2010, 01:52 PM
<?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.

Fou-Lu
12-22-2010, 03:11 PM
In the future please wrap code in
or
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.


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.

anandselvaraj
12-23-2010, 12:21 PM
Thank you very much....its working fine.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum