transparencia Posted January 24, 2009 Share Posted January 24, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/142263-solved-output-string-formatting/ Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 how should we know? $kernel->format_input is some arbitrary method in some arbitrary class. You can start by posting that method. Quote Link to comment https://forums.phpfreaks.com/topic/142263-solved-output-string-formatting/#findComment-745288 Share on other sites More sharing options...
transparencia Posted January 24, 2009 Author Share Posted January 24, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/142263-solved-output-string-formatting/#findComment-745294 Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 yep. I think the easiest method will be to pass your 'formatting rules' as an array of the rules you want, and wrap most of that function inside a foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/142263-solved-output-string-formatting/#findComment-745298 Share on other sites More sharing options...
MadTechie Posted January 24, 2009 Share Posted January 24, 2009 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142263-solved-output-string-formatting/#findComment-745300 Share on other sites More sharing options...
transparencia Posted January 24, 2009 Author Share Posted January 24, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/142263-solved-output-string-formatting/#findComment-745325 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.