PHPFunction to create document.write

Posted on 2009-04-30
Last Modified: 2012-05-06
I am looking for a PHP function that I can call like this:

function DocumentDotWrite($variable,$MaintainLines)

Where I can pass in a variable, if MaintainLines is set to true, it will create a document.write('') for each line, if it is set to false, it will remove all line breaks and create 1 statement. I guess it probably probably has to escape some html or quotes as well?
Question by:interclubs
    1 Comment
    LVL 39

    Accepted Solution

    Try this:
    function DocumentDotWrite($variable,$MaintainLines) {
      if($MaintainLines) {
        $lines = explode("\n",str_replace("\r\n","\n",$variable));
        foreach($lines as $line) {
          $line = addslashes($line);
          echo "document.write('$line');\n";
      } else {
        $variable = addslashes(str_replace(array("\r\n","\n"),'',$variable));
        echo "document.write('$variable');";

    Open in new window


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now