Archive

Posts Tagged ‘html’

Preparing HTML And PHP Code For Pubilishing On Websites

April 1st, 2009 No comments

I talked a while ago about Adding Code To WordPress Blogs And Comments, but I decided that it needed a bit of code to do this automatically.

So here it is, prepared by the text processor.

<form method="post" action="http://talkincode.com/examples/text-process/text.php">
    <textarea name="text" rows="10" cols="80" wrap="off"></textarea>
    <input type="submit" value="Process" />
</form>
 
<?php
if ( isset($_POST["text"]) ) {
    $text   = $_POST["text"];
    $text   = stripslashes( $text );
    $input  = array ( "/&/", "/'/", "/"/", "/</", "/>/", "/t/", "/(?<=s)x20|x20(?=s)/", "/^\s$/m", "/&/", "/rn/" );
    $output = array ( "&amp;", "&#39;", "&quot;", "&lt;", "&gt;", "&nbsp;&nbsp;&nbsp;&nbsp;", "&nbsp;", "&nbsp;<br />", "&amp;", "<br />" );
    $temp = preg_replace($input, $output, $text);
    echo '<div style="border:1px solid grey;">'.$temp.'</div>';
}
?>

There seems to be rather a lot going on here, but the process is quite simple. The preg_replace() function can take an array as an argument for the input and output parameters. When you do this the arrays will be matched up so that the second item in the input array will be replaced by the second item in the output array.

So here is a list of the things I am matching for and what they are replaced with.

  • /&/ This matches for any ampersand, we replace this with the encoded variant of &amp;.
  • /&#39;/ Find single quotes and encode them with &#39;.
  • /\"/ Find double quotes and encode them with &quot;.
  • /
  • />/ Same as above but the other way around, in this case the equivalent is &gt;.
  • /\t/ Next we start matching for white space, the first is to find all tab characters and replace them with four &nbsp; characters, like this &nbsp;&nbsp;&nbsp;&nbsp;
  • /(?<=\s)\x20|\x20(?=\s)/ Next we look for any space character that has white space characters before and after it and replace with a single white space character &nbsp;.
  • /^\s$/m This matches for any line with nothing on it. These must be replaced with a single &nbsp; character, but in order to keep the code as it was posted we add a <br /> tag, the final output would be &nbsp;<br />.
  • /&/ Now that we have all of our tags encoded we need to re encode all of the & characters so that when the script prints out the content to a HTML page with all & translated to &amp;.
  • /\r\n/ Finally, we find all of the new line characters and convert them to <br /> tags. You might want to change this to just \n if you are using a Linux format.

Before we do any of this we pass the text through the stripslashes() function. This is because sending the text over POST might add slashes to the " and ' characters. This call just removes them.

You can try out the processor if you want by copying some code into the following text box.

This will output to the text process example page. You can also visit this page directly and play around with the tool.

Install PHPDocumentor

January 21st, 2009 No comments

PHPDocumentor is a fast and convent way of creating API documentation for your PHP programs and classes. If you are familiar with the world of Java, it works in much the same way as the JavaDoc program, indeed, it is based on this program.

PHPDocumentor can be run in a number of different ways, but I have found that the easiest way is to, again, use PEAR to install everything you need. To install PHPDocumentor using PEAR use the following command.

phpdoc install phpdocumentor

To run PHPDocumentor and see a list of commands just type in the following:

phpdoc -h

To run PHPDocumentor you need to provide a couple of options, these are:

  • t The documentation directory (ie. where your documentation files will go).
  • o The output format (e. HTML, PDF etc), which is defined as output:converter:template directory.
  • d The directory (ie. where your source code files are).

Use the following syntax to run PHPDocumentor, using the above parameters.

C:\projectDir>phpdoc -t "c:\projectDir" -o HTML:default:default -d "c:\projectDir\docs"

The output format defined here is HTML, with the default style and format. This is as a document with frames and in the default style that the program uses. To see what sort of formats are available go to the PHPDocumentor install directory at <your PHP directory>\data\PhpDocumentor\phpDocumentor\Converters.

To get a different style on your PHPDocs use the following value for the -o parameter.

HTML:default:l0l33t

To create a PDF of your documentation use the following.

PDF:default:default

To product output that looks like the PHP home page use the following:

HTML:smarty:php

You might be asking how you get your PHP code to generate this documentation. Although this is not complicated, there is rather a lot to go over and so this will be the subject of a different blog post.

Categories: PHP Tags: , , , , , , ,

Convert A sitemap.xml File To A HTML Sitemap With PHP

August 13th, 2008 No comments

I have already talked about converting a sitemap.xml file into a urllist.txt file, but what if you want to create a HTML sitemap? If you have a sitemap.xml file then you can use this to spider your site, scrape the contents of each page and populate the HTML file with this information.

The following code does this. For every page it looks for the <title> tag, the description meta tag and the first <h2> tag on the page. These items are then used to construct a segment of HTML for that page.

<?php
$header = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>HTML Sitemap</title>
</head>
<body>';
 
set_time_limit(400);
 
$currentElement = '';
$currentLoc = '';
 
$map = "<h1>HTML Sitemap</h1>"."\n";
 
function parsePage($data){
 global $map;
 /*
 if you want to trap a certain file extention then use the syntax below...
 stripos($data,".php")>0
 stripos($data,".htm")>0
 stripos($data,".asp")>0
 */
 if(stripos($data,".pdf")>0){
  // if the url is a pdf document.
  $map .= '<p><a href="'.$data.'">PDF document.</a></p>'."\n";
  $map .= '<p>A pdf document.</p>'."\n";
 }elseif(stripos($data,".txt")>0){
  // if the url is a text document
  $map .= '<p><a href="'.$data.'">Text document.</a></p>'."\n";
  $map .= '<p>A text document.</p>'."\n";
 }else{
  // try to open it anyway...
  // make sure that you can read the file
  if($urlh = @fopen($data, 'rb')){
   $contents = '';
   //check php version
   if(phpversion()>5){
    $contents = stream_get_contents($urlh);
   }else{
    while(!feof($urlh)){
     $contents .= fread($urlh, 8192);
    };
   };
 
   // find the title
   preg_match('/(?<=\<[Tt][Ii][Tt][Ll][Ee]\>)\s*?(.*?)\s*?(?=\<\/[Tt][Ii][Tt][Ll][Ee]\>)/U',$contents,$title);
   $title = $title[0];
 
   // find the first h1 tag
   $header = array();
   preg_match('/(?<=\<[Hh]2\>)(.*?)(?=\<\/[Hh]2\>)/U',$contents,$header);
   $header = strip_tags($header[0]);
 
   if(strlen($title)>0 && strlen($header)>0){
    // print the title and h1 tag in combo
    $map .= '<p class="link"><a href="'.str_replace('&','&amp;',$data).'" title="'.(strlen($header)>0?trim($header):trim($title)).'">'.trim($title).(strlen($header)>0?" - ".trim($header):'').'</a></p>'."\n";
   }elseif(strlen($title)>0){
    $map .= '<p class="link"><a href="'.str_replace('&','&amp;',$data).'" title="'.trim($title).'">'.trim($title).'</a></p>'."\n";
   }elseif(strlen($header)>0){
    $map .= '<p class="link"><a href="'.str_replace('&','&amp;',$data).'" title="'.trim($header).'">'.trim($header).'</a></p>'."\n";
   };
 
   // find description
   preg_match('/(?<=\<[Mm][Ee][Tt][Aa]\s[Nn][Aa][Mm][Ee]\=\"[Dd]escription\" content\=\")(.*?)(?="\s*?\/?\>)/U',$contents,$description);
   $description = $description[0];
 
   // print description
   if(strlen($description)>0){
    $map .= '<p class="desc">'.trim($description).'</p>'."\n";
   };
   // close the file
   fclose($urlh);
  };
 };
};
 
/////////// XML PARSE FUNCTIONS HERE /////////////
// the start element function
function startElement($xmlParser,$name,$attribs){
 global $currentElement;
 $currentElement = $name;
};
 
// the end element function
function endElement($parser,$name){
 global $currentElement,$currentLoc;
 if($currentElement == 'loc'){
  parsePage($currentLoc);
  $currentLoc = '';
 };
 $currentElement = '';
};
 
// the character data function
function characterData($parser,$data){
 global $currentElement,$currentLoc;
 // if the current element is loc then it will be a url
 if($currentElement == 'loc'){
  $currentLoc .= $data;
 };
};
 
// create parse object
$xml_parser = xml_parser_create();
// turn off case folding!
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING, false);
// set start and end element functions
xml_set_element_handler($xml_parser,"startElement","endElement");
// set character data function
xml_set_character_data_handler($xml_parser,"characterData");
 
// open xml file
if(!($fp = fopen('sitemap.xml',"r"))){
 die("could not open XML input");
};
 
// read the file - print error if something went wrong.
while($data = fread($fp,4096)){
 if(!xml_parse($xml_parser,$data,feof($fp))){
  die(sprintf("XML error: %s at line %d",xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser)));
 };
};
 
// close file
fclose($fp);
 
$footer = '</body>
</html>';
 
// write output to a file
$fp = fopen('sitemap.html',"w+");
fwrite($fp,$header.$map.$footer);
fclose($fp);
 
// print output
echo $header.$map.$footer;
?>

This script prints out the sitemap and also saves the sitemap to a file for later use. This is essential as the script can take a long time to run due to all of the page accessing that it has to do.

This script is failry complicated and has gone through several versions since I first created it so if you find any improvements or bugs then let me know and I will incorporate them.

Categories: PHP Tags: , , , , , , ,

Using JavaScript To Select Text

July 1st, 2008 No comments

This is a simple trick that will allow users to select the contents of a text area. First we need a text area.

<form><textarea name="textarea1" id="textarea1" rows="5" cols="40" wrap="off">This is some
long content.
This is some long content.
This is some long content.
This is some long content.
</textarea>
<br />
<input type="button" value="Select text" onclick="selectText('textarea1')">
</form>

This form also includes a button with an on click event that runs a function. This function takes a single parameter as the name of the element. It then sets the focus to this element and then selects all of the text therein.

function selectText(id){
 var id = document.getElementById(id);
 id.focus();
 id.select();
}

Why is this useful? Well lets say you had a form or a quiz that produced and answer, and you wanted people to post their answer on their blogs, which then linked back to your quiz. This would allow users to select the contents of a text area without having to select it themselves.

Categories: PHP Tags: , , , ,

HTML And XHTML Doctypes

June 23rd, 2008 No comments

In order to validate any page of HTML or XHTML you will need a doctype. This is a string of text that sits at the top of the document and tells the browser exactly what markup standard has been used to create the page.

XHTML Strict

This doctype is used in an XHTML document when you are not using any framset or depreciated tags.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

XHTML Transitional

This doctype is used if your XHTML document contains depreciated tags like <b>.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

XHTML Frameset

Use this XHTML doctype if your document contains either frameset tags or depreciated tags, or both.
&ltl!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

HTML Strict

Use this HTML doctype if your document contains no depreciated tags.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

HTML Transitional

Also called loose, this doctype is to be used if your HTML document contains depreciated tags.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

HTML Frameset

This doctype is used in a HTML document that contains frameset tags or depreciated tags or both.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">

Without these doctypes in place the browser will default to what is called “quirks mode” where certain CSS styles are looked at a little differently. If you are finding trouble with your stylesheet then try adding a doctype that fits your markup.

Alternatively, if you find that you want your page to validate and adding the doctype tag actually destroys the page then you can add what is called a "broken doctype". This is the same as any normal doctype, but with the URL pointing to the DTD missing. Here are two examples.

XHTML Strict Broken Doctype

This doctype is used in an XHTML document when you are not using any framset or depreciated tags, but still want the page to display in quirks mode and validate properly.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">

HTML Strict Broken Doctype

This doctype is used in an HTML document when you are not using any framset or depreciated tags, but still want the page to display in quirks mode and validate properly.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">

Categories: (X)HTML Tags: , , ,