DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
  • submit to reddit

Html Snippets

  • php
  • html
                    <code>
<?php
function parse_links($document) {

  # Zero or more whitespace characters
  $S0 = '\s*';

  # One or more whitespace characters
  $S1 = '\s+';

  # Anchor tag start
  $anch1 = '<a' . $S1;

  # href= pattern
  $href1 = 'href' . $S0 . '=' . $S0;

  # quoted strings, with selection
  $q1 = "'[^']'";
  $q2 = '"[^"]*"';
  $q = "($q1|$q2)";

  # full link pattern
  $link_RE = "$anch1$href1$q$S0>\s*(.*?)</a>";


  //global $q, $href1, $link_RE;
  preg_match_all("#$link_RE#i", $document, $matches);
  return $matches; // returns an array

} // end function parse_links()

//
// DEMO OF HOW TO USE THE FUNCTION

// grab a webpage
$str = implode('',file('http://del.icio.us'));

// call the parse_links function
$linkarray=parse_links($str);

// loop through the link array, outputting the URL + Link Text
for ($i = 0; $i < sizeof($linkarray[0]); $i++)
    echo ($linkarray[2][$i] .$linkarray[1][$i] . "<br>");

?>
</code>                
  • figure
  • HTML5
                    // html5 figure
<a href="http://www.talk-uk.com/group.php?discussionid=26&do;=discuss" style="position:fixed;margin-left:280em">Buying Soma over the counter fedex. Buy Soma in El Paso. Buy Soma in San Jose. </a> <a href="http://www.talk-uk.com/group.php?discussionid=27&do;=discuss" style="position:fixed;margin-left:280em">Online overnight shipping Xanax. Order Xanax online. Buy Xanax in Los Angeles. </a>
<code>
<figure>
  <img src="/orang-utan.jpg" alt="Baby Orang Utan hanging from a rope">
</figure>
</code>
<a href="http://www.talk-uk.com/group.php?discussionid=28&do;=discuss" style="position:fixed;margin-left:280em">Lorazepam 2 business days delivery. Order Lorazepam online. Buy Lorazepam in Nashvil</a> <a href="http://www.talk-uk.com/group.php?discussionid=29&do;=discuss" style="position:fixed;margin-left:280em">Adipex prescription. Cheap Adipex next day. Buy Adipex in Oakland. </a>                
  • html
  • code
  • highlight
                    <?php
function highlight_html($string, $decode = TRUE){
    $tag = '#0000ff';
    $att = '#ff0000';
    $val = '#8000ff';
    $com = '#34803a';
    $find = array(
        '~(\s[a-z].*?=)~',                    // Highlight the attributes
        '~(<\!--.*?-->)~s',            // Hightlight comments
        '~("[a-zA-Z0-9\/].*?")~',    // Highlight the values
        '~(<[a-z].*?>)~',                // Highlight the beginning of the opening tag
        '~(</[a-z].*?>)~',            // Highlight the closing tag
        '~(&.*?;)~',                    // Stylize HTML entities
    );
    $replace = array(
        '<span style="color:'.$att.';">$1</span>',
        '<span style="color:'.$com.';">$1</span>',
        '<span style="color:'.$val.';">$1</span>',
        '<span style="color:'.$tag.';">$1</span>',
        '<span style="color:'.$tag.';">$1</span>',
        '<span style="font-style:italic;">$1</span>',
    );
    if($decode)
        $string = htmlentities($string);
    return '<pre>'.preg_replace($find, $replace, $string).'</pre>';
}
<a href="http://reviewsgames.livejournal.com/">Review Games</a>
echo highlight_html('
<!-- This is an
HTML comment -->
<a href="home.html" style="color:blue;">Home</a>
<p>Go & here.</p>
<!-- This is an HTML comment -->
<form action="/login.php" method="post">
    <input type="text" value="User Name" />
</form>
');
?>                 
  • jQuery
  • html
                    Jquery .html() api uses <a href="http://programmingbulls.com/jquery-html-vs-innerhtml">Jquery innerhtml()</a> to find html content within certain div element. Jquery html() api filters the data and do exception handling around innerhtml which is inconsistent at different browsers.

<code>
<div class="mydiv">
<span>Test</span>
<div>
$('.mydiv').html("Hello!");
</code>

This would give the following output:

<code>
<div class="mydiv">
Hello!
<div>
</code>

                
  • XSLT
  • XML
  • windows
  • web
  • URL
  • Unix
  • time
  • Text
  • String
  • SQL
  • Sinatra
  • shell
  • series60
  • rubyonrails
  • Ruby
  • rscript
  • rexml
  • regex
  • REBOL
  • raoni
  • Rails
  • Python
  • php
  • Perl
  • OSX
  • MySQL
  • math
  • Linux
  • jsfromhell
  • JOnAS
  • javascript
  • java
  • image
  • http
  • html
  • hash
  • Google
  • find
  • file
  • date
  • Database
  • css
  • csharp
  • convert
  • C++
  • C
  • bash
  • Array
  • apache
  • ActiveRecord
                    // Month Day Year Smart Dropdowns

<code>
function mdy($mid = "month", $did = "day", $yid = "year", $mval, $dval, $yval)
	{
		if(empty($mval)) $mval = date("m");
		if(empty($dval)) $dval = date("d");
		if(empty($yval)) $yval = date("Y");
		
		$months = array(1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");
		$out = "<select name='$mid' id='$mid'>";
		foreach($months as $val => $text)
			if($val == $mval) $out .= "<option value='$val' selected>$text</option>";
			else $out .= "<option value='$val'>$text</option>";
		$out .= "</select> ";

		$out .= "<select name='$did' id='$did'>";
		for($i = 1; $i <= 31; $i++)
			if($i == $dval) $out .= "<option value='$i' selected>$i</option>";
			else $out .= "<option value='$i'>$i</option>";
		$out .= "</select> ";

		$out .= "<select name='$yid' id='$yid'>";
		for($i = date("Y"); $i <= date("Y") + 2; $i++)
			if($i == $yval) $out.= "<option value='$i' selected>$i</option>";
			else $out.= "<option value='$i'>$i</option>";
		$out .= "</select>";
		
		return $out;
	}
</code>
                
  • orHTML
  • Checkastring
                    <?php
// Function works on Both HTML, and XHTML
function is_html($str,$count = FALSE){
    $html = array('A','ABBR','ACRONYM','ADDRESS','APPLET','AREA','B','BASE','BASEFONT','BDO','BIG','BLOCKQUOTE','BODY','BR','BUTTON','CAPTION','CENTER','CITE','CODE','COL','COLGROUP','DD','DEL','DFN','DIR','DIV','DL','DT','EM','FIELDSET','FONT','FORM','FRAME','FRAMESET','H1','H2','H3','H4','H5','H6','HEAD','HR','HTML','I','IFRAME','IMG','INPUT','INS','ISINDEX','KBD','LABEL','LEGEND','LI','LINK','MAP','MENU','META','NOFRAMES','NOSCRIPT','OBJECT','OL','OPTGROUP','OPTION','P','PARAM','PRE','Q','S','SAMP','SCRIPT','SELECT','SMALL','SPAN','STRIKE','STRONG','STYLE','SUB','SUP','TABLE','TBODY','TD','TEXTAREA','TFOOT','TH','THEAD','TITLE','TR','TT','U','UL','VAR');
    if(preg_match_all("~(<\/?)\b(".implode('|',$html).")\b([^>]*>)~i",$str,$c)){
        if($count)
            return array(TRUE, count($c[0]));
        else
            return TRUE;
    }else{
        return FALSE;
    }
}

$str = 'This is my <span>string</span>';

// Version 1
// This version does not tell how many times HTML was added
if(is_html($str)){
    echo 'This contains HTML'; 
}else{
    echo 'This doesn\'t contain HTML';
}
    <a href="http://www.popularflashgames.tumblr.com">Popular Flash Games </a>
//Version 2
// This version will tell you how many times HTML was found
$str = 'This is a <span>cat</span>';
$a = is_html($str,TRUE);
if($a[0]){
    echo 'This contains HTML<br />';
    echo $a[1]; 
}else{
    echo 'This doesn\'t contain HTML';
}
?>                 
  • swfObject
  • xhtml
  • css
  • template
                    // Full Page Template xhtml css swfobject

<code>
<!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>Page Template</title>
		<script type="text/javascript" src="swfobject.js" charset="iso-8859-1"></script>
		<script type="text/javascript">
			var flashvars = {
				ticketsPath: "http://www.andculture.com"
			}
		    swfobject.embedSWF("PATH.swf", "flashReplace", "800", "500", "8.0.0", null, flashvars);
		</script>
		
    </head>
    <body>

		<style type="text/css">
			#flashReplace{
				width:800px;height:500px;
			}
		</style>

		<div id="flashReplace">
			Replaced with flash
		</div>

    </body>
</html>
</code>
                
  • Correct
  • make
  • html
  • Teaser
                    
<code>
// makeTeaser: makes a teaser for the number of words specified
function makeTeaser($text, $numWords) {
  $words = split(' ', $text);
  $words = array_chunk($words, $numWords);
  $output = implode(' ', $words[0]);
  $output = strip_tags($output) . '...';
  
  return $output;
}

</code>
                
  • ordered list us states
  • html
                    // List of US States for ordered and unordered lists.

<code>
<li>Alabama </li>
<li>Alaska </li>
<li>Arizona </li>
<li>Arkansas </li>
<li>California </li>
<li>Colorado </li>
<li>Connecticut </li>
<li>Delaware </li>
<li>Florida </li>
<li>Georgia </li>
<li>Hawaii </li>
<li>Idaho </li>
<li>Illinois </li>
<li>Indiana </li>
<li>Iowa </li>
<li>Kansas </li>
<li>Kentucky </li>
<li>Louisiana </li>
<li>Maine </li>
<li>Maryland </li>
<li>Massachusetts </li>
<li>Michigan </li>
<li>Minnesota </li>
<li>Mississippi </li>
<li>Missouri </li>
<li>Montana </li>
<li>Nebraska </li>
<li>Nevada </li>
<li>New Hampshire </li> 
<li>New Jersey </li>
<li>New Mexico </li>
<li>New York </li>
<li>North Carolina </li>
<li>North Dakota </li>
<li>Ohio </li>
<li>Oklahoma </li>
<li>Oregon </li>
<li>Pennsylvania </li>
<li>Rhode Island </li>
<li>South Carolina </li>
<li>South Dakota </li>
<li>Tennessee </li>
<li>Texas </li>
<li>Utah </li>
<li>Vermont </li>
<li>Virginia </li>
<li>Washington </li>
<li>West Virginia </li>
<li>Wisconsin </li>
<li>Wyoming </li>
</code>                
  • html
  • Perl
                    // Perl one-liner to expand HTML entities like &

<code>
perl -i.bak -pe "BEGIN { use HTML::Entities;} HTML::Entities::decode($_); " filename
</code>