Jump to content

benphp

Members
  • Posts

    336
  • Joined

  • Last visited

Posts posted by benphp

  1. When I put Darkwater's line into the larger function it doesn't work. Here's the full script with demo HTML:

     

    <html>
    <head></head>
    <body>
    <?php
    $text1 = "<html>
    <body>
    <h2><a name=\"_Toc479567961\"></a><a name=\"_Toc473534443\"></a><a
    name=\"_Toc473530327\"></a><a name=\"_Toc471122987\"></a><a name=\"_Toc470952538\"><span
    style='font-variant:small-caps !msorm;text-transform:none !msorm'><span
    style='font-variant:normal !important;text-transform:uppercase'>Overview</span></span></a></h2>
    <p>
    <a name=\"_Toc471122987\"></a>Test
    </body>
    </html>";
    
    
    /*   MS Word HTML cleaner,
    */
    function lego_clean($text) {
    // normalize white space
    $text = eregi_replace("[[:space:]]+", " ", $text);
    $text = str_replace("> <",">\r\r<",$text);
    $text = str_replace("<br>","<br>\r",$text);
    ///mine
    $text = str_replace("Symbol\'>w</span>","Symbol\'><code id=\"symb\">ω</code></span>",$text);
    $text = str_replace("Symbol\'>p</span>","Symbol\'><code id=\"symb\">π</code></span>",$text);
    $text = str_replace("Symbol\'>Ð</span>","Symbol\'><code id=\"symb\">∠</code></span>",$text);
    $text = str_replace("Symbol\'>q</span>","Symbol\'><code id=\"symb\">θ</code></span>",$text);
    $text = str_replace("Symbol\'>d</span>","Symbol\'><code id=\"symb\">δ</code></span>",$text);
    $text = str_replace("uppercase\'>d</span>","uppercase\'><code id=\"symb\">Δ</code></span>",$text);
    $text = str_replace("Symbol\'>°</span>","Symbol\'><code id=\"symb\">°</code></span>",$text);
    $text = str_replace("Symbol\'>W</span>","Symbol\'><code id=\"symb\">Ω</code></span>",$text);
    $text = str_replace("&#38;#952;","<code id=\"symb\">θ</code>",$text);
    $text = str_replace("&#38;#948;","<code id=\"symb\">δ</code>",$text);
    $text = str_replace("º","<code id=\"symb\">°</code>",$text);
    $text = str_replace("°","<code id=\"symb\">°</code>",$text);
    
    $text = preg_replace('!<a(.+?)name="[^"]+"[^>]*></a>!is', '', $text);
    //reference
    /// http://tlt.its.psu.edu/suggestions/international/bylanguage/mathchart.html
    
    // remove everything before <body>
    $text = strstr($text,"<body");
    
    // keep tags, strip attributes
    $text = ereg_replace("<p [^>]*BodyTextIndent[^>]*>([^\n|\n\015|\015\n]*)</p>","<p>\\1</p>",$text);
    $text = eregi_replace("<p [^>]*margin-left[^>]*>([^\n|\n\015|\015\n]*)</p>","<blockquote>\\1</blockquote>",$text);
    $text = str_replace(" ","",$text);
    
    //clean up whatever is left inside <p> and <li>
    $text = eregi_replace("<p [^>]*>","<p>",$text);
    $text = eregi_replace("<li [^>]*>","<li>",$text);
    
    // kill unwanted tags
    $text = eregi_replace("</?span[^>]*>","",$text);
    $text = eregi_replace("</?body[^>]*>","",$text);
    $text = eregi_replace("</?div[^>]*>","",$text);
    $text = eregi_replace("<\![^>]*>","",$text);
    $text = eregi_replace("</?[a-z]\:[^>]*>","",$text);
    
    // kill style and on mouse* tags
    $text = eregi_replace("([ \f\r\t\n\'\"])style=[^>]+", "\\1", $text);
    $text = eregi_replace("([ \f\r\t\n\'\"])on[a-z]+=[^>]+", "\\1", $text);
    
    //remove empty paragraphs
    $text = str_replace("<p></p>","",$text);
    
    //remove closing </html>
    $text = str_replace("</html>","",$text);
    
    //clean up white space again
    $text = eregi_replace("[[:space:]]+", " ", $text);
    $text = str_replace("> <",">\r\r<",$text);
    $text = str_replace("<br>","<br>\r",$text);
    
    return $text;
    }
    
    
    
    
    print "\n<form action=\"cleaner.php\" method=\"post\">";
    print "\n<textarea name=\"text1\" cols=\"66\" rows=\"25\">$text1</textarea>";
    print "\n<input type=\"submit\" name=\"btnClean\" value=\"Clean\">";
    print "\n</form>";
    
    if(isset($_POST['text1'])) {
    $text1 = $_POST['text1'];
    $text1 = lego_clean($text1);
    print "$text1";
    }
    
    ?>
    </body>
    </html>
    

     

    Expected result is stripping the <a name tags - including the closing tags, but keeping the <a href tags.

  2. Here's the entire script:

     

    <?php
    /*	MS Word HTML cleaner,
    */
    function lego_clean($text) {
    
        // normalize white space
        $text = eregi_replace("[[:space:]]+", " ", $text);
        $text = str_replace("> <",">\r\r<",$text);
        $text = str_replace("<br>","<br>\r",$text);
    ///mine
        $text = str_replace("Symbol\'>w</span>","Symbol\'><code id=\"symb\">ω</code></span>",$text);
        $text = str_replace("Symbol\'>p</span>","Symbol\'><code id=\"symb\">π</code></span>",$text);
        $text = str_replace("Symbol\'>Ð</span>","Symbol\'><code id=\"symb\">∠</code></span>",$text);
        $text = str_replace("Symbol\'>q</span>","Symbol\'><code id=\"symb\">θ</code></span>",$text);
        $text = str_replace("Symbol\'>d</span>","Symbol\'><code id=\"symb\">δ</code></span>",$text);
        $text = str_replace("uppercase\'>d</span>","uppercase\'><code id=\"symb\">Δ</code></span>",$text);
        $text = str_replace("Symbol\'>°</span>","Symbol\'><code id=\"symb\">°</code></span>",$text);
        $text = str_replace("Symbol\'>W</span>","Symbol\'><code id=\"symb\">Ω</code></span>",$text);
        $text = str_replace("&#952;","<code id=\"symb\">θ</code>",$text);
        $text = str_replace("&#948;","<code id=\"symb\">δ</code>",$text);
        $text = str_replace("º","<code id=\"symb\">°</code>",$text);
        $text = str_replace("°","<code id=\"symb\">°</code>",$text);
    
    $text = preg_replace('!<a(.+?)name="[^"]+"[^>]*></a>!is', '', $text);
    //reference
    /// http://tlt.its.psu.edu/suggestions/international/bylanguage/mathchart.html
    
        // remove everything before <body>
        $text = strstr($text,"<body");
    
        // keep tags, strip attributes
        $text = ereg_replace("<p [^>]*BodyTextIndent[^>]*>([^\n|\n\015|\015\n]*)</p>","<p>\\1</p>",$text);
        $text = eregi_replace("<p [^>]*margin-left[^>]*>([^\n|\n\015|\015\n]*)</p>","<blockquote>\\1</blockquote>",$text);
        $text = str_replace(" ","",$text);
    
        //clean up whatever is left inside <p> and <li>
        $text = eregi_replace("<p [^>]*>","<p>",$text);
        $text = eregi_replace("<li [^>]*>","<li>",$text);
    
        // kill unwanted tags
        $text = eregi_replace("</?span[^>]*>","",$text);
        $text = eregi_replace("</?body[^>]*>","",$text);
        $text = eregi_replace("</?div[^>]*>","",$text);
        $text = eregi_replace("<\![^>]*>","",$text);
        $text = eregi_replace("</?[a-z]\:[^>]*>","",$text);
    
        // kill style and on mouse* tags
        $text = eregi_replace("([ \f\r\t\n\'\"])style=[^>]+", "\\1", $text);
        $text = eregi_replace("([ \f\r\t\n\'\"])on[a-z]+=[^>]+", "\\1", $text);
    
        //remove empty paragraphs
    $text = str_replace("<p></p>","",$text);
    
        //remove closing </html>
    $text = str_replace("</html>","",$text);
    
        //clean up white space again
    $text = eregi_replace("[[:space:]]+", " ", $text);
    $text = str_replace("> <",">\r\r<",$text);
    $text = str_replace("<br>","<br>\r",$text);
    
    return $text;
    }
    
    
    
    
    print "<form action=cleaner.php method=post>";
    print "<textarea name=text cols=66 rows=25></textarea>";
    print "<input type=submit name=btnClean>";
    print "</form>";
    
    if (isset($_POST['btnClean'])) {
    $text = $_POST['text'];
    $text = lego_clean($text);
    print $text;
    }
    
    ?>
    

  3. $text = the HTML I posted, for example. In reality it would be a much larger HTML page.

     

    <html>
    <body>
    <h2><a name="_Toc479567961"></a><a name="_Toc473534443"></a><a
    name="_Toc473530327"></a><a name="_Toc471122987"></a><a name="_Toc470952538"><span
    style='font-variant:small-caps !msorm;text-transform:none !msorm'><span
    style='font-variant:normal !important;text-transform:uppercase'>Overview</span></span></a></h2>
    <p>
    <a name="_Toc471122987"></a>Test
    <p>
    <a href="test.php">Test2</a>
    </body>
    </html>
    

  4. That works - for that HTML - but I want to put it into a larger function, such as:

     

    <?php
        $text = str_replace("θ","<code id=\"symb\">θ</code>",$text);
        $text = str_replace("δ","<code id=\"symb\">δ</code>",$text);
        $text = str_replace("º","<code id=\"symb\">°</code>",$text);
        $text = str_replace("°","<code id=\"symb\">°</code>",$text);
    
        $text = preg_replace('!<a(.+?)name="[^"]+"[^>]*></a>!is', '', $text);
    ?>
    

     

    and it doesn't work...?

  5. Trying to strip a name tags:

     

    <html>
    <body>
    <h2><a name="_Toc479567961"></a><a name="_Toc473534443"></a><a
    name="_Toc473530327"></a><a name="_Toc471122987"></a><a name="_Toc470952538"><span
    style='font-variant:small-caps !msorm;text-transform:none !msorm'><span
    style='font-variant:normal !important;text-transform:uppercase'>Overview</span></span></a></h2>
    <p>
    <a name="_Toc471122987"></a>Test
    <p>
    <a href="test.php">Test2</a>
    </body>
    </html>
    

     

    But keep a href tags.

  6. Here's a post on the MySQL forum that says that so long as you're using it and not distributing it, you can use it for free:

     

    http://forums.mysql.com/read.php?4,14006,14006#msg-14006

     

    It has been my understanding that the GPL license means that it is free to use in just about any capacity. But if you develop an application and want to sell it with a distributed copy of MySQL, you need to pay. At least, that's how I read the legalese.

  7. I'm pulling my hair out.

     

    Server: 4.0.26-nt

    Desktop: 5.0.51b-community-nt

     

    Tables are identical on both servers:

    +---------+-------------+------+-----+---------+----------------+
    | Field   | Type        | Null | Key | Default | Extra          |
    +---------+-------------+------+-----+---------+----------------+
    | rid     | int(11)     | NO   | PRI | NULL    | auto_increment |
    | pid     | int(11)     | YES  |     | NULL    |                |
    | qid     | int(11)     | YES  |     | NULL    |                |
    | uid     | int(11)     | YES  |     | NULL    |                |
    | score   | varchar(2)  | YES  |     | NULL    |                |
    | ractive | varchar(1)  | YES  |     | NULL    |                |
    | shift   | varchar(2)  | YES  |     | NULL    |                |
    | rcount  | varchar(4)  | YES  |     | NULL    |                |
    | moddate | varchar(20) | YES  |     | NULL    |                |
    +---------+-------------+------+-----+---------+----------------+
    

     

    Code is identical:

    <?php
    $selectR = "
    select r1.score, r1.moddate, r1.rcount, r1.uid
    from resp r1
    where r1.rcount = (
        select r2.rcount
        from resp r2
        where r2.uid=r1.uid
    ORDER BY r2.rcount DESC LIMIT 1
        )
    ";
    ?>
    

     

    One works, and the other gives me "Fatal error: SQL in ..."

     

    ?????

     

  8. I did it the hard way:

     

    <?php
    include('common/functions.php');
    $dbConn = fnOpenDbConn();
    
    print "<table>";
    $selectR = "
    select rid, score, uid, moddate, rcount
    from resp r1
    where rcount = (
        select rcount
        from resp r2
        where r2.uid=r1.uid
    ORDER BY r2.rcount DESC LIMIT 1
        )
    AND uid = '204'
    ";
    //print $selectR;
    $resultR = mysql_query($selectR) or trigger_error("SQL", E_USER_ERROR);
    $numrowsR = mysql_num_rows($resultR);
    while($rowR = mysql_fetch_row($resultR)) {
    $rid	= $rowR[0];
    $score	= $rowR[1];
    $uid	= $rowR[2];
    $moddate	= $rowR[3];
    $rcount	= $rowR[4];
    print "<tr><td>$rid</td><td>$score</td><td>$moddate</td><td>$uid</td><td>$rcount</td></tr>";
    }
    print "</table>";
    
    mysql_close($dbConn);
    ?>
    

     

    For anyone who comes along later and wants to see how it's done - here's the table:

     

    +------+------+------+------+-------+---------+-------+--------+------------+
    | rid  | pid  | qid  | uid  | score | ractive | shift | rcount | moddate    |
    +------+------+------+------+-------+---------+-------+--------+------------+
    |  479 |    1 |  162 |  204 | 1     | NULL    | 1     | 1      | 2008.09.22 |
    |  480 |    1 |  163 |  204 | 1     | NULL    | 1     | 1      | 2008.09.22 |
    |  481 |    1 |    1 |  204 | 1     | NULL    | 1     | 1      | 2008.09.22 |
    |  482 |    1 |    2 |  204 | 3     | NULL    | 1     | 2      | 2008.09.23 |
    |  483 |    1 |    3 |  204 | 3     | NULL    | 1     | 2      | 2008.09.23 |
    |  484 |    1 |    4 |  204 | 3     | NULL    | 1     | 3      | 2008.09.24 |
    |  485 |    1 |   31 |  204 | 3     | NULL    | 1     | 4      | 2008.09.25 |
    

  9. Yes, that limits the result to one record.

     

    I think I need to use MAX somehow, but I can't get it to work. Here's the actual code that I've been trying with no success:

     

    <?php
    include('common/functions.php');
    $dbConn = fnOpenDbConn();
    
    print "<table>";
    $selectR = "
    SELECT DISTINCT rid, score, uid, MAX(moddate) as Moddate 
    FROM resp 
    WHERE uid = '204'
    GROUP BY resp.rid, resp.score, resp.moddate 
    ORDER BY resp.rid
    ";
    
    $resultR = mysql_query($selectR) or trigger_error("SQL", E_USER_ERROR);
    $numrowsR = mysql_num_rows($resultR);
    print "$numrowsR";
    while($rowR = mysql_fetch_row($resultR)) {
    $rid	= $rowR[0];
    $score	= $rowR[1];
    $uid	= $rowR[2];
    $moddate	= $rowR[3];
    print "<tr><td>$rid</td><td>$score</td><td>$moddate</td><td>$uid</td></tr>";
    }
    print "</table>";
    
    mysql_close($dbConn);
    ?>
    

  10. The issue is a bit more complicated - I simplified it for the question, but here is the actual code. Maybe one of you wizards can figure this one out. The trick is that $colcount may be greater than the elements in the $rows array, because the elements in the $rows array varies:

     

    <?php
    for ($x=0;$x<=$colcount-1;$x++) {  
    print "<td>";
    $sum = 0;
    for ($i=0;$i<=$rowcount-1;$i++) { //cycle through the row array
    	//$rows [row] [column] 
    	$sum = $sum + $rows[$i][$x];
    	$avg = round($sum / $rowcount, 2);
    }
    print $avg;
    print "</td>";
    }
    ?>
    

     

     

  11. I have something that looks like this:

    <?php
    for ($i=0;$i<=$rowcount;$i++) { 
    $sum = $rows[$i];
    }
    ?>
    

     

    But the $rowcount isn't always the same, therefore I get the Undefined offset error. Is there a function that can determine if the offset will be undefined before it attempts the loop? Otherwise, I'll need to write more lines to an already large page.

     

    Thanks!

  12. You can use modulus for this:

     

    <?php
    $string = "";
    $cols = "";
    if (!empty($_POST['words'])) {
    $string = $_POST['words'];			//declare the $string variable passed from the "words" textarea
    }
    if (!empty($_POST['cols'])) {
    $cols = $_POST['cols'];				//declare the $cols string passed from the "cols" textbox - defines the number of table columns to draw
    }
    $string = str_replace("'", '&#38;#39;', $string); 	//replace apostrophes with the equivalent SGML decimal character reference code (Standard Generalized Markup Language)
    $string = stripslashes($string);		//remove the annoying automatic magic_quotes_gpc slashes set in php.ini 
    ?>
    
    <html>
    <head>
    <title> </title>
    <LINK REL="stylesheet" TYPE="text/css" HREF="../style.css" />
    
    </head>
    <body onLoad="document.maketable.cols.focus();">
    Finishes the table when the array isn't perfectly divisible by the number of columns. 
    <form action="" method="post" name="maketable">
    Enter an array: 
    <?php
    if($string == '')
    {
    print "<textarea cols='60' rows='5' name='words'>For who would bear the whips and scorns of time, th' oppressor's wrong, the proud man's contumely, the pangs of despised love, the law's delay, the insolence of office, and the spurns that patient merit of th' unworthy takes, when he himself might his quietus make with a bare bodkin? Who would fardels bear, to grunt and sweat under a weary life, but that the dread of something after death, the undiscover'd country, from whose bourn no traveller returns, puzzles the will and makes us rather bear those ills we have than fly to others that we know not of?</textarea>";
    print "<br />Number of columns: <input type='text' size='1' maxlength='1' name='cols' value='6' onMouseDown=\"window.document.maketable.cols.value='';\">";
    }
    else
    {
    print "<textarea cols='60' rows='5' name='words'>$string</textarea>";
    print "<br />Number of columns: <input type='text' size='1' maxlength='1' name='cols' value='$cols' onMouseDown=\"window.document.maketable.cols.value='';\">";
    }
    ?>
    <br />
    
    <input type="submit" value="Write Table">  <input type="button" value="Clear" onclick="window.document.maketable.words.value='';document.maketable.words.focus();">  <input type="button" value="Reload Hamlet" onclick="window.document.maketable.words.value='For who would bear the whips and scorns of time, th\' oppressor\'s wrong, the proud man\'s contumely, the pangs of despised love, the law\'s delay, the insolence of office, and the spurns that patient merit of th\' unworthy takes, when he himself might his quietus make with a bare bodkin? Who would fardels bear, to grunt and sweat under a weary life, but that the dread of something after death, the undiscover\'d country, from whose bourn no traveller returns, puzzles the will and makes us rather bear those ills we have than fly to others that we know not of?';document.maketable.cols.focus();">
    </form>
    
    <table border="1">
    <tr>
    <?php
    if($string !="") {				//if $string is not empty, then write the table - so the table isn't written when first drawing the page
    
    $myarray = split(' ', $string);			//split the string on [space]  -isn't there a StrToArray function?
    array_unshift($myarray, ' ');			//adds a blank element to the beginning of the array - necessary, due to modulus use - if zero is used, then it would bollix the first quotient
    $num = count($myarray) -1;			//use minus 1 to offset the empty value at the beginning of the array 
    
    for($i = 1; $i<= $num; $i++)			//loop while the array is less than the array count	
    {
    if($i%$cols != 0) 			//if the array count divided by cols has a remainder then write ONLY a table cell - 'cause if it doesn't have a remainder, then it's reached the end of the row
    {
    	if($i != $num)			//if this is not the last cell then print a cell
    	{ 
    		print "\n\t<td>$myarray[$i]</td>";  		//each individual cell
    	}  //end not last cell
    	else				//this IS the last cell
    	{
    		print "\n\t<td>$myarray[$i]</td>";  		//print the last cell
    		$myremain = ($i%$cols);		  		//get the remaining number of items in the array to print
    		$emptycels = $cols - $myremain;	  		//find the remaining table cells to print
    			while($emptycels != 0)	  		//loop through the empty table cells until then number of cells in the row = $cols 
    			{
    				print "\n\t<td> </td>";  	//print remaining empty table cells
    				$emptycels -= 1;		//negatively increment the empty cell count
    			}  
    	}  //end else last cell  		
    }  //end if modulus not zero
    else					//if the array can be divided by cols with no remainder, then write end of row 
    {
    	if ($i < $num)							//if the counter hasn't yet reached the total number of records
    	{
    		print "\n\t<td>$myarray[$i]</td></tr>\n<tr>"; 		//print the last cell in the row with a new row 
    	}
    	else								//else if the counter is equal to the total number of records
    	{
    		print "\n\t<td>$myarray[$i] </td>\n"; 			//print the last cell in the row without a new row 
    	}
    }  //end else modulus IS zero
    }  //end main loop
    
    
    
    } //end if $string is not empty
    ?>
    
    </tr>    
    </table>
    
    </body>
    </html>
    

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.