Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Making Code Copyable on Your Page

Most of the articles in the Willmaster Library and Blog contain source code you can copy and paste. The code may be JavaScript, PHP, Perl, HTML, or other.

Generally, source code is put into a pre tag or into a textarea form field.

If that's all there was to it, then this article would be done.

But angle brackets and ampersands need special handling. Quotes, too. Let's use this sentence for an illustration.

Jack & Jill — up the hill.

The sentence is within paragraph tags and it publishes an em dash.

To provide the source code for a site owner to publish that sentence, you would need to provide this for copying and pasting.

<p>Jack & Jill &mdash; up the hill.</p>

And how would you get that copyable source code published on your page, so the HTML markup and the HTML entity show up as source code?

In other words, how do you publish source code that shows up like source code?

Use this table to convert every occurrence of the listed characters into their equivalent HTML entities.

Change
This
To
This
<&lt;
>&gt;
&&amp;
"&quot;

When that is done for the "Jack & Jill" sentence source code, you end up with this.

&lt;p&gt;Jack &amp; Jill &amp;mdash; up the hill.&lt;/p&gt;

You put that source code into your web page so your web page publishes source code that's copyable by others so they can publish the sentence on their web pages.

Here is how it works.

  • You put this into the source code of your web page.

    &lt;p&gt;Jack &amp; Jill &amp;mdash; up the hill.&lt;/p&gt;
  • When you do that, this is what publishes on your web page that other people can copy.

    <p>Jack & Jill &mdash; up the hill.</p>
  • When other people copy the source code from your web page and paste it into the source code of their own web page, it will appear like this:

    Jack & Jill — up the hill.

Here is a generator you can use to convert source code into characters you can publish on your web page so the source code is available for others. (Try <p>Jack & Jill &mdash; up the hill.</p> for testing.)

You are welcome to use the generator here. You are also welcome to put the generator on your own web page.

Here is the generator source code.

<?php
/*
Convert Source Code for Publishing Copyable Source Code
Version 1.0
January 1, 2019

Will Bontrager Software LLC
https://www.willmaster.com/
*/
$homecode = $publishcode = $result = '';
if( isset($_POST['thecode']) and strlen($_POST['thecode']) > 0 )
{
    $result = $_POST['thecode'];
    $publishcode = htmlentities( $result );
    $homecode = htmlspecialchars( $publishcode );
}
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Convert Source Code for Publishing Copyable Source Code</title>
<style type="text/css">
* { box-sizing:border-box; }
html, body { font-size:100%; font-family:sans-serif; margin:0; }
pre { overflow:auto; border:1px solid #ccc; border-radius:.5em; padding:.75em; margin-top:0; }
</style>
</head>
<body>
<h3 style="margin-top:0;">
Convert Source Code for Publishing Copyable Source Code
</h3>
<form method="post" enctype="multipart/form-data" accept-charset="utf-8" action="<?php echo(htmlspecialchars($_SERVER['PHP_SELF'])); ?>">
<p style="margin-bottom:.15em;">
Type or paste the source code to encode.
</p>
<textarea name="thecode" style="width:100%; height:1in;"><?php echo($publishcode) ?></textarea>
<p>
<input type="submit" value="Encode the code" style="width:100%;">
</p>
</form>
<?php if( strlen($result) ): ?>
<p style="margin-bottom:.15em;">
Put this into the source code of your web page.
</p>
<pre>
<?php echo($homecode) ?>
</pre>
<p style="margin-bottom:.15em;">
Your site visitors will see this on your page to copy and put on their pages.
</p>
<pre>
<?php echo($publishcode) ?>
</pre>
<p style="margin-bottom:.15em;">
Their site visitors will see this.
</p>
<pre style="white-space:normal;">
<?php echo($result) ?>
</pre>
<?php endif; ?>
<p style="margin-bottom:0;">
Copyright 2019 <a href="https://www.willmaster.com/">Will Bontrager Software LLC</a>
</p>
</body>
</html>

The generator is PHP software and may be saved with any file name ending with the .php file name extension.

The generator can be used by putting its URL into the web browser.

With the generator, it is now easier to create source code that publishes as source code for people to copy.

(This article first appeared with an issue of the Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC