PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am trying to rewrite this ASP/Jscript code to PHP so I can use it on my web site. It is part of a form generated by Top Producer. Here is the Jscript part:
<%@LANGUAGE="JScript" %>
<%
// Write a user entered value into the HTML page safely.
function writeSafeValue( sName ) {
var sValue;
sValue = "" + Request.QueryString( sName ) + "";
try {
if( typeof( sValue ) == "string" ) {
if( sValue != "undefined" ) {
// Do net let the user attempt to input HTML Code or
// SSI (Server Side Include) directives.
sValue = sValue.replace( /</g, "<" );
sValue = sValue.replace( />/g, ">" );
sValue = sValue.replace( /\"/g, """ );
}
else {
sValue = "";
}
}
} catch( e ) {
// Catch and display any errors that occur.
Response.Write( "writeValue( "" + sName + "" ) ERROR " + e.number + ": " + e.description + " - sValue: " + sValue );
sValue = "";
}
Response.Write( sValue );
}
%>
Anyone here good at converting ASP to PHP? I'm a bit of a noob and have been Googling operators and syntax for days to no avail. I COULD spend weeks learning PHP (which is a given) however I would really like to finish this project sooner than that. Any help is greatly appreciated!
writeSafeValue can be replaced with a single call to htmlspecialchars, though htmlentities is generally better (does the work of htmlspecialchars and more).
Very nice, McInfo! I was thinking that, about the return, but I thought there might be some reason that he wanted a printout, and not a returned value.
McInfo wrote:It should; but only if magic_quotes_gpc is enabled. Another improvement is to echo a return value rather than echo from within the function.
Thanks McInfo! That did the trick. So all that jscript code was reduced by about 70%... wow. And htmlentities seems extremely easy to use. Thanks everyone.