Results 1 to 8 of 8

Thread: html 2 php converter function

  1. #1
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default html 2 php converter function

    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,

    Code:
    <a href="somelink.html">Click Here</a>
    It outputs as follows.

    PHP Code:
    <?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 Code:
    <?php
    echo '<a href="somelink.html">Click Here</a>'."\n";
    ?>
    here is the javascript;

    Code:
    <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.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Please post a link to the page on your site that contains the problematic script so we can check it out.
    Jeremy | jfein.net

  3. #3
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    i only use this locally but i set up free hosting with a page

    Click Here

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,004
    Thanks
    44
    Thanked 3,203 Times in 3,165 Posts
    Blog Entries
    12

    Default

    Code:
    <!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.
    Last edited by jscheuer1; 06-24-2008 at 06:45 AM. Reason: minor code efficiency
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    nullified (06-23-2008)

  6. #5
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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

  7. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,004
    Thanks
    44
    Thanked 3,203 Times in 3,165 Posts
    Blog Entries
    12

    Default

    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,004
    Thanks
    44
    Thanked 3,203 Times in 3,165 Posts
    Blog Entries
    12

    Default

    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):

    Code:
    <!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>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #8
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    thankyou for the indepth answer its greatly appreciated and very helpfull in learning.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •