Jump to content

[SOLVED] Output string formatting


transparencia

Recommended Posts

I have this code:

 

$file['file_description'] = $kernel->format_input( $file['file_description'], T_STRIP );

 

Besides T_STRIP I want to use another formatting rule, but this doesn't work:

 

$file['file_description'] = $kernel->format_input( $file['file_description'], T_STRIP, T_HTML );

 

How would I do this?

Link to comment
https://forums.phpfreaks.com/topic/142263-solved-output-string-formatting/
Share on other sites

Sorry, I assumed that it was a default function.

 

Here it is:

 

function format_input( $variable_data, $variable_type )
{
	switch( $variable_type )
	{
		//HTML clean #$this->page->postit_code#
		case T_HTML: $variable_data = nl2br( $this->strip_html( $variable_data, $this->config['system_allowed_html_tags'], false, true ) ); break;

		//HTML remove
		case T_NOHTML: $variable_data = $this->strip_html( $variable_data, $this->config['system_allowed_html_tags'], false, true, true ); break;

		//HTML remove
		case T_PREVIEW: $variable_data = $this->strip_html( $variable_data, $this->config['system_allowed_html_tags'], false, false ); break;

		//HTML <form> element clean
		case T_FORM:
		{
			$original = array( '\\n', '\\r' );
			$replaced = array( chr( 10 ), chr( 13 ) );

			$variable_data = str_replace( $original, $replaced, $variable_data );

			break;
		}

		//Clean for database entry
		case T_DB: $variable_data = $this->xss_clean( $variable_data, false ); break;

		//Clean for database entry
		case T_STRIP: $variable_data = stripslashes( $variable_data ); break;

		//URL friendly
		case T_URL_ENC: $variable_data = urlencode( $variable_data ); break;

		//URL un-friendly
		case T_URL_DEC: $variable_data = urldecode( $variable_data ); break;

		//URL parsable for filesizes
		case T_DL_PARSE: $variable_data = str_replace( " ", "%20", $variable_data ); break;

		//Format based on locale
		case T_NUM: $variable_data = number_format( $variable_data ); break;

		//Nothin'
		default: break;
	}

	return $variable_data;
}

 

I guess to able to apply two or more of this rules, the whole function would have to be rewritten?

I agree with CV and heres an example

 

<?php
format_input( "test", T_STRIP); //pass 1
format_input( "test", array(T_STRIP, T_HTML)); //pass more than 1
format_input( "test", array(T_HTML)); //pass 1 (as an array will also work)

function format_input( $variable_data, $variable_type )
{
if(!is_array($variable_type))
{
	$variable_typeA = array();
	$variable_typeA[] = $variable_type;
}else{
	$variable_typeA = $variable_type;
}
foreach($variable_typeA as $variable_type)
{
	switch( $variable_type )
	{
		//HTML clean #$this->page->postit_code#
		case T_HTML:
			$variable_data = nl2br( $this->strip_html( $variable_data, $this->config['system_allowed_html_tags'], false, true ) );
		break;

		//HTML remove
		case T_NOHTML:
			$variable_data = $this->strip_html( $variable_data, $this->config['system_allowed_html_tags'], false, true, true );
		break;

		//HTML remove
		case T_PREVIEW:
			$variable_data = $this->strip_html( $variable_data, $this->config['system_allowed_html_tags'], false, false );
		break;

		//HTML <form> element clean
		case T_FORM:
			$original = array( '\\n', '\\r' );
			$replaced = array( chr( 10 ), chr( 13 ) );
			$variable_data = str_replace( $original, $replaced, $variable_data );
		break;

		//Clean for database entry
		case T_DB:
			$variable_data = $this->xss_clean( $variable_data, false );
		break;

		//Clean for database entry
		case T_STRIP:
			$variable_data = stripslashes( $variable_data );
		break;

		//URL friendly
		case T_URL_ENC:
			$variable_data = urlencode( $variable_data );
		break;

		//URL un-friendly
		case T_URL_DEC:
			$variable_data = urldecode( $variable_data );
		break;

		//URL parsable for filesizes
		case T_DL_PARSE:
			$variable_data = str_replace( " ", "%20", $variable_data );
		break;

		//Format based on locale
		case T_NUM:
			$variable_data = number_format( $variable_data );
		break;

		//Nothin'
		default: break;
	}
}
  
  return $variable_data;
}
   
?>

I see, that is a good solution. Thanks a lot!

 

Although now I found that the funtion stripslashes also removes the \n and so the spaces in a text!

 

I will try to explain the original problem. It is best to show an example, so consider this text:

 

O'reilly is a "King of Metal"!

 

Oh yes, he is.

 

When I print it with the T_HTML rule it shows like this:

 

O\'reilly is a \"King of Metal\"!

 

Oh yes, he is.

 

When I print it with the T_STRIP (stripslashes) it shows like this:

 

O'reilly is a "King of Metal"! Oh yes, he is.

 

Notice how the paragraphs are lost.

 

So what I want is to be able to print the exact same text that was originally input. Also, I don not want to change the input method, only the output.

 

I'm thinking about using the T_HTML rule and then add a preg_replace to replace \" for " and \' for '. Could you help me with the patterns bellow, I don't think these are the right ones.

 

$str = preg_replace('/\"/', '', $str);

 

$str = preg_replace('/\'/', '', $str);

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.