Jump to content

Streamlining HTMLENTITIES


doubledee

Recommended Posts

$testString = "<b>This is a bold string value</b>";
$testArray = array(array("<b>This is a bold mutidimensional array value</b>"));

echo "<br>String before encoding: " . $testString;
echo "<br>String after encoding: " . entities($testString);
echo "<br>Array before encoding: <pre>" . print_r($testArray, true) . "</pre>";
echo "<br>Array after encoding: <pre>" . print_r(entities($testArray), true) . "</pre>";

 

Expected Output (using my function):

String before encoding: This is a bold string value

String after encoding: <b>This is a bold string value</b>

 

Array before encoding:

Array (

    [ 0 ] => Array (

        [ 0 ] => This is a bold mutidimensional array value

    )

)

Array after encoding:

Array (

    [ 0 ] => Array (

        [ 0 ] => <b>This is a bold mutidimensional array value</b>

    )

)

 

That was very helpful.

 

Thank you!!

 

 

Debbie

 

 

Link to comment
Share on other sites

BTW, a few questions about the parameters...

 

	function entities($input, $type=ENT_QUOTES, $char='UTF-8'){

 

1.) Why does UTF-8 wrapped in single quotes and ENT_QUOTES is not?

 

2.) When I document my Function, what is the Data-Type for...

 

$type  ??

 

$char ??

 

 

I guess they are both using Constants, right? 

 

But what Data-Types are those? 

 

Are they Integers?

 

 

Debbie

 

 

Link to comment
Share on other sites

Thanks for the help everyone!!

 

 

For your consumption, here are the two Function that I am adding to my Code Library including the Test Cases...

 

<?php

//****************************************************************************
function str2htmlentities($input, $type=ENT_QUOTES, $char='UTF-8'){
	/**
	 * Convert all applicable characters to HTML entities using PHP Loop.
	 *
	 * To safely display reserved characers (e.g. < >), use this function to
	 * convert text to the appropriate HTML Entities before outputting.
	 *
	 * This will help to prevent against Cross-Site Scripting (XSS) attacks.
	 *
	 * Returns either a scalar variable or an array.
	 *
	 * Written On: 2012-05-31
	 *
	 * @param		{String, Array, Multi-Dimensional Array}	$input
	 * @param		Integer (Constant)												$type
	 * @param		String																		$char
	 * @return	String
	 */
	if (is_array($input)){
		foreach ($input as $key => $value){
			$input[$key] = str2htmlentities($value, $type, $char);
		}

		return $input;
	}else{
		return htmlentities($input, $type, $char);
	}
}
//****************************************************************************



//****************************************************************************
function str2htmlentities_map($input, $type=ENT_QUOTES, $char='UTF-8'){
	/**
	 * Convert all applicable characters to HTML entities using ArrayMap.
	 *
	 * To safely display reserved characers (e.g. < >), use this function to
	 * convert text to the appropriate HTML Entities before outputting.
	 *
	 * This will help to prevent against Cross-Site Scripting (XSS) attacks.
	 *
	 * Returns either a scalar variable or an array.
	 *
	 * Written On: 2012-05-31
	 *
	 * @param		{String, Array, Multi-Dimensional Array}	$input
	 * @param		Integer (Constant)												$type
	 * @param		String																		$char
	 * @return	String
	 */
    if (is_array($input)){
        return array_map('str2htmlentities_map', $input);
    }
	return htmlentities($input, $type, $char);
}
//****************************************************************************



// **NOTE: Rename Function references below for "str2htmlentities_map"

// **************
// Test Data 1.	*
// **************
$testString = "<b>This is a bold string value</b>";
$testArray = array(array("<b>This is a bold mutidimensional array value</b>"));

echo "<br>String before encoding: " . $testString;
echo "<br>String after encoding: " . str2htmlentities($testString);
echo "<br>Array before encoding: <pre>" . print_r($testArray, true) . "</pre>";
echo "<br>Array after encoding: <pre>" . print_r(str2htmlentities($testArray), true) . "</pre>";


// **************
// Test Data 2.	*
// **************
$username = "<b>DoubleDee</b>";
$htmlTags=array("<b>Bold</b>", "<i>Italics</i>", "<h1>Heading1</h1>");

echo '$username before encoding: ' . $username . '<br />';
echo '$username after encoding: ' . str2htmlentities($username) . '<br /><br />';
echo '$favoriteTags Array before encoding: ' . "<pre>" . print_r($htmlTags, true) . "</pre><br />";
echo '$favoriteTags Array after encoding: ' . "<pre>" . print_r(str2htmlentities($htmlTags), true) . "</pre><br /><br />";


// **************
// Test Data 3.	*
// **************
$music[] = array('GROUP' => '<b>Led Zeppelin</b>', 'ALBUM' => '<u>Led Zeppelin III</u>', 'SONGS' => array("<i>Friends</i>", "<i>Gallows Pole</i>", "<i>That's the Way</i>"));
$music[] = array('GROUP' => '<b>Led Zeppelin</b>', 'ALBUM' => '<u>Houses of the Holy</u>', 'SONGS' => array("<i>Over the Hills and Far Away</i>", "<i>D'yer Mak'er</i>", "<i>The Ocean</i>"));
$music[] = array('GROUP' => '<b>Cream</b>', 'ALBUM' => '<u>Disraeli Gears</u>', 'SONGS' => array("<i>Strange Brew</i>", "<i>SWLABR</i>"));

echo "<br>Music Array before encoding: <pre>" . print_r($music, true) . "</pre><br />";
echo "<br>Music Array after encoding: <pre>" . print_r(str2htmlentities($music), true) . "</pre>";


?>

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.