Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: [ADDON] HTML Entity Conversion Utility (sNews All Versions)  (Read 7005 times)

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
[ADDON] HTML Entity Conversion Utility (sNews All Versions)
« on: August 09, 2006, 03:25:44 PM »

If you want to display a block of code by pasting it into a normal HTML page, you will run into a problem when everything following the first HTML entity disappears and won't display. To display it, you need to convert all HTML entities to their character equivalents. You can do it by hand-coding each replacement, but this is a tedious exercise when there are many entities in a large block of code.

I came across this script about a week ago that automatically converts all the entities for you. You simply paste the code you want converted into the textarea, choose the conversion option for quotes conversion, and hit the Convert button. It then displays a new block with the converted code in it. You copy the converted code from there and paste it where you want it. After that, you need it insert line-returns on the end of each line, and spacing at the start of each line, so that it will display line-by-line. Otherwise, it displays as one long line in one paragraph without line-breaks.

Step 1
Note - this utility is totally independent of sNews files - runs alone.
Open a new PHP page in your code editor and paste the code below into it, then save the file as... htmlentities.php (or whatever you want to call it) into a new folder within your sNews root folder. Call this new folder utilities. Doing this keeps it separate from your sNews files.

Step 2
Call the file in your browser using the normal URL: http://localhost/snews/utilities/htmlentities.php for an offline (home computer) installation... or http://www.yoursite.com/utilities/htmlentities.php if its on a remote server (online).

Code: [Select]
<?php
/* ---------------------------------------------------------
   HTML Entities Conversion Tool for sNews Users
   - original script from - http://www.w3clubs.com/htmlentities.php
   - Re-worked to function as an add-on for sNews 1.4 - August 1/06
   - By: Keyrocks - http://www.snews.ca
   - Requires absolutely no mods or changes to any sNews file.
   - Just install it in the root folder and call it.
   - This copy is wrapped in a split template - temptop.php & tempbott.php
------------------------------------------------------------------------------------------*/

include ("../temptop.php");

echo 
'<h2>HTML entities converter</h2>';
        if (!empty(
$_POST['text'])){
            
$_POST['text'] = stripslashes($_POST['text']);
            echo 
'<div style="border: solid 1px orange; padding: 20px; margin: 20px">';
            switch (@
$_POST['quote_style']) {
                case 
'quotes':
                    
$quote_style ENT_QUOTES;
                    break;
                case 
'noquotes':
                    
$quote_style ENT_NOQUOTES;
                    break;
                case 
'compat':
                default:
                    
$quote_style ENT_COMPAT;
                    break;
            }
            echo 
'<pre>';
            echo 
htmlentities(htmlentities($_POST['text'], $quote_style), ENT_QUOTES);
            echo 
'</pre>';
            echo 
'</div>';
        }
        if (!isset(
$_POST['text'])) {
            
$_POST['text'] = "(replace this text with code)";
        }
        
?>

        <form action="htmlentities.php" method="post">
            <textarea onclick="this.select()" name="text" style="width: 400px; height: 200px"><?php echo @$_POST['text']; ?></textarea>
            <fieldset title="Conversion options" style="width: 400px; border: solid 1px #dddddd">
                <legend>Conversion options</legend>
                <input type="radio" name="quote_style" value="compat" id="radio_compat" <?php echo

(@
$_POST['quote_style']=='compat' || !isset($_POST['quote_style'])) ? 'checked="checked" ' ''?>
/>
                <label for="radio_compat">Convert double quotes, but NOT single quotes (default)</label>
                <input type="radio" name="quote_style" value="quotes" id="radio_quotes" <?php echo

(@
$_POST['quote_style']=='quotes') ? 'checked="checked" ' ''?>
/>
                <label for="radio_quotes">Convert double quotes AND single quotes</label>
                <input type="radio" name="quote_style" value="noquotes" id="radio_noquotes" <?php echo

(@
$_POST['quote_style']=='noquotes') ? 'checked="checked" ' ''?>
/>
                <label for="radio_noquotes">Don't convert any quotes</label>
            </fieldset>
            <input type="submit" value="Convert Now" />
        </form>
        <?php
        
include (
"../tempbott.php");
?>

Step 3 - adding your template
You'll notice that the file opens with include ("../temptop.php"); and closes with include ("../tempbott.php");. I use these "include" statements to wrap the function in my stie's template. If you just want a simple template, you can replace these with the HTML you want to use. If your site's template is more complex and you want to use it... and you still have the original HTML template kicking around, just split a copy into top and bottom parts, save them wherever you like as .php files, and use the "include" statements as shown. If you don't need any template - just disable or remove the two "include" statements in the file.

You can test-run this utility at HTML Entity Converter Demo.

« Last Edit: January 31, 2012, 02:44:18 AM by Keyrocks »
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

bryn

  • Hero Member
  • *****
  • Karma: 2
  • Posts: 934
    • http://www.cssugly.com
[ADDON] HTML Entity Conversion Utility (sNews All Versions)
« Reply #1 on: August 09, 2006, 11:03:16 PM »

excellent keyrocks..thanks very much for sharing the code with us..as always it's much appreciated :D
Logged
Over 1,000 posts of joy, sNews is not only brilliant, but fun too! thanks guys :D

klaus

  • Newbie
  • *
  • Karma: 0
  • Posts: 24
[ADDON] HTML Entity Conversion Utility (sNews All Versions)
« Reply #2 on: November 21, 2006, 02:52:03 AM »

This has been the reason for many coders's headaches. All the above code does is call the PHP native function htmlentities. However, that fuction is very limited and does not allways delivers the best results. The debate has been vast and the best solution I have seen so far is this:
http://www.prolifique.com/entities.php.txt
Glad to be of some help.
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
[ADDON] HTML Entity Conversion Utility (sNews All Versions)
« Reply #3 on: November 21, 2006, 03:17:23 AM »

Your input is always welcomed. It would be nice if you could share this as a ready-made, functional addon for sNews users. :)
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

klaus

  • Newbie
  • *
  • Karma: 0
  • Posts: 24
[ADDON] HTML Entity Conversion Utility (sNews All Versions)
« Reply #4 on: November 21, 2006, 07:27:00 AM »

Thanks. I'll do it and post here. The best advice to put the code above to work is

1. If u're converting php/sql or any other code besides clean text, comment the line
// $_POST['text'] = stripslashes($_POST['text']);

2. The amp problem on several languages! After converting, search replace "&#" for "&#"
Logged

Patric Ahlqvist

  • Nobodys perfect, but Im pretty effing close
  • ULTIMATE member
  • ******
  • Karma: 65
  • Posts: 4867
  • I'm a self-made man and worships my creator.
    • p-ahlqvist.com
Re: [ADDON] HTML Entity Conversion Utility (sNews All Versions)
« Reply #5 on: January 21, 2008, 03:26:26 PM »

As demo seems down, I have it here:

http://p-ahlqvist.com/coding/blaj.php
Logged
"It's only dead fish that goes with the flow... "
Updated

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: [ADDON] HTML Entity Conversion Utility (sNews All Versions)
« Reply #6 on: January 21, 2008, 03:42:15 PM »

Thanks Patric. Actually the demo is not down... just not where it used to be. I had relocated the Entity Converter on snews.ca some time ago and forgot to change the link... it's fixed now.  :)

I see you've modified the Jet 30 template from Template World. Great minds think alike... I'm working on a modified version of it as well.
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU