PDA

View Full Version : html 2 php converter function



nullified
06-23-2008, 01:58 AM
hi all,

im pretty sure i got this code from here originally but it was to long ago now :(

basically i have a javascript function that converts html 2 php for me.the trouble is i want it to use single quotes instead of double quotes.

ill give you an example, if input the following html into my form,



<a href="somelink.html">Click Here</a>


It outputs as follows.



<?php
echo "<a href=\"somelink.html\">Click Here</a>\n";
?>


which is fine and works perfect except i want it to output like so;



<?php
echo '<a href="somelink.html">Click Here</a>'."\n";
?>


here is the javascript;



<script type="text/javascript" language="javascript">
function brCheck(data)
{
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;

if(ns4) br = "%0A";
else if(ns6) br = "%0A";
else if(ie4) br = "%0D%0A";
else br = "%0D%0A";

data.value=data.value.replace(/"/gi,'\\"');
dataArr=escape(data.value).split(br);
data.value="";

}

function scriptPHP(data){
brCheck(data);
for (i=0; i<dataArr.length; i++){
data.value+= (i==0) ? "<?php\necho " : "echo "
data.value+= "\"" + unescape(dataArr[i]);
data.value+= (i!=dataArr.length-1) ? "\\n\"; \n" : "\\n\";\n"
}
data.value+="\?>"
}
</script>



i think it might fairly easy im just to dumb lol, any help would greatly appreciated.

Nile
06-23-2008, 02:04 AM
Please post a link to the page on your site that contains the problematic script so we can check it out.

nullified
06-23-2008, 03:31 AM
i only use this locally but i set up free hosting with a page

Click Here (http://dfwcomputer.x10hosting.com/converter.html)

jscheuer1
06-23-2008, 07:49 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>HTML to PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function scriptPHP(data){
data.value = '<\?php\n' + data.value.replace(/\x0d/g, '').replace(/'/g,
"\\'").replace(/^([^\x0a]*)$/gm, 'echo \'$1\' . "\\n";').replace(/echo '' \. "\\n";/g,
'echo "\\n";') + '\n\?>';
}

function copyF(txx){
with(txx){
focus();
select();
}
if(txx.createTextRange){
var txt = txx.createTextRange();
if(txt.execCommand)
txt.execCommand('Copy');
}
}
</script>

</head>

<body onload="document.forms['doc'].elements['tx'].focus();">
<div id="content">
<h2>HTML Converter</h2>
<div class="feature">
<h3><br>

<br>
</h3>
<form name="doc" action="">
<div>
<textarea name="tx" cols="65" rows="7" class="input1"></textarea>
<br>
<input name="button" type="button" class="input2" onclick="scriptPHP(this.form.elements['tx'])" value="HTML -> PHP">
<input name="reset" type="reset" class="input2" value="Reset" onclick="this.form.elements['tx'].focus()">

<input name="button2" type="button" class="input2" onclick="copyF(this.form.elements['tx'])" value="Select All">

</div>
</form>
</div>
</div>


</body>
</html>

Rather extensively tested in IE 7, FF 2, Safari 3 Win, and Opera 9.5.

nullified
06-23-2008, 09:08 AM
thankyou very much it is greatly appreciated, i thought it would be a simple change but im not even going to ask lol

Thanks Again

jscheuer1
06-23-2008, 02:44 PM
Well, I'll answer anyway. The script you had used deprecated methods, and was designed to accommodate browsers that, frankly you are unlikely to ever come across except in a lab for legacy browsers. So I just fixed up the deprecated, and about to be deprecated stuff (most of it at least), and changed the approach to be more efficient for today's browsers.

As a side note, I added the ability of this utility to deal with one level of nested single quotes ('), something that would have been irrelevant in the original, but that can be quite handy. The original did check for nested double quotes ("), but that resulted (if the inputted HTML was is in standard attribute quoting format) in PHP code that was much harder to read than this newer version's.

jscheuer1
06-24-2008, 06:57 AM
For anyone who might be interested, here is a version of this script which will work in the browsers already mentioned, as well as in 'version 4' browsers. And, although I haven't tested it, it should work on both Apple and 'nix systems (which each have a slightly different way of doing line breaks in text than do Windows systems):


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>HTML to PHP</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function scriptPHP(data){
if(!/\x0a/.test(data.value)) data.value = data.value.replace(/\x0d/g, '\x0a');
data.value = data.value.replace(/'/g, "\\'");
for (var a = data.value.split(/\x0a/), i = a.length - 1; i > -1; --i)
a[i] = a[i].replace(/^([^\x0a]*)$/,'echo \'$1\' . "\\n";').replace(/(\x0d|\x0a)/g, '').replace(/echo '' \. "\\n";/, 'echo "\\n";');
data.value = '<\?php\n' + a.join('\n') + '\n\?>';
}

function copyF(txx){
txx.focus();
txx.select();
if(txx.createTextRange){
var txt = txx.createTextRange();
if(txt.execCommand)
txt.execCommand('Copy');
}
}
</script>

</head>

<body onload="document.forms['doc'].elements['tx'].focus();">
<div id="content">
<h2>HTML Converter</h2>
<div class="feature">
<h3><br>

<br>
</h3>
<form name="doc" action="">
<div>
<textarea name="tx" cols="65" rows="7" class="input1"></textarea>
<br>
<input name="button" type="button" class="input2" onclick="scriptPHP(this.form.elements['tx'])" value="HTML -> PHP">
<input name="reset" type="reset" class="input2" value="Reset" onclick="this.form.elements['tx'].focus()">

<input name="button2" type="button" class="input2" onclick="copyF(this.form.elements['tx'])" value="Select All">

</div>
</form>
</div>
</div>


</body>
</html>

nullified
06-27-2008, 02:10 AM
thankyou for the indepth answer its greatly appreciated and very helpfull in learning.