The Easiest Way to Save and Share Code Snippets on the web

Anchor Helper

php

posted: Jul, 5th 2010 | jump to bottom

<?php
 
function get_domain() {
	return 'http://demo.com/';
}
 
function anchor($text, $data = null) {
 
	$attributes = ' ';
	$data = (empty($data)) ? array() : $data;
 
	// Check for a title
	if( array_key_exists('title', $data) == FALSE )
		$attributes .= 'title="' . $text . '" ';
	else
		$attributes .= 'title="' . $data['title'] . '" ';
 
	// Check for a link
	if( array_key_exists('href', $data) == FALSE )
		$href = '#no_link';
	else
		$href = $data['href'];
 
	// Check for attributes
	if( array_key_exists('attr', $data) == TRUE ) {
		// Make sure it's an array
		if( is_array($data['attr']) ) {
 
			foreach( $data['attr'] as $prop => $value ) :
				$attributes .= $prop . '="' . $value . '" ';
			endforeach;
 
		}
	}
 
	// Build the link
	$link = '<a' . $attributes . 'href="' . get_domain() . $href . '">' . $text . '</a>';
	echo $link;
}
 
anchor('Hello World', array(
		'title' => 'My Custom Link',
		'href' 	=> 'index.php',
		'attr' 	=> array(
			'class' => 'link',
			'id' => 'special',
			'style' => 'color: blue; text-decoration: none;'
			)
	));
?>
242 views