Show code in textarea

Asked

Viewed 496 times

0

What I intend is to show on my web site within a text area code php html and etc but whenever I try to do it runs the code and I want it to show within a text area and other system any accuracy as here in the stack overflow

<section class="bg-light" id="links">
  <div class="container">

    <h1 class="text-center text-info">Teste PHP</h1>

      <?php
          echo"Olá, Mundo!";
          echo("<h2>Olá, Mundo</h2>");
          ?>

    </div>
  </div>

</section>
<section class="bg-light">
  <hr></hr>
  <div class="container">
    <div class="row">
      <div class="col-md-12">
        <label for="comment" class="text-info">Código feito:</label>
        <textarea class="form-control" rows="15" id="codigo">Conteudo a apresentar</textarea>
    </div>
    </div>
  </div>
</section>
  • 1

    Can you add the code you tried to ask the question?

  • Yes!! My 1s mistake

  • The Content to present is the place where I want to put the content to show the site

  • Does not need to be in a text area but if possible mehor

1 answer

0


You need to find all html entities. Most languages already have some automatic way to escape code.

In Java, using JSTL in JSP, you can do so:

<c:out value="${valorAqui}" escapeXml="true"/>

In PHP, there is the htmlentities function that you use:

<?php echo htmlentities($texto); ?>

I didn’t get to this, but I believe that in a simple way you could also print php code as follows within HTML:

<?php echo '<?php //aqui você insere seu código... ?>' ?>

Behold:

<section class="bg-light" id="links">
  <div class="container">

    <h1 class="text-center text-info">Teste PHP</h1>

      <?php
          echo"Olá, Mundo!";
          echo("<h2>Olá, Mundo</h2>");
          ?>

    </div>
  </div>

</section>
<section class="bg-light">
  <hr></hr>
  <div class="container">
    <div class="row">
      <div class="col-md-12">
        <label for="comment" class="text-info">Código feito:</label>
        <textarea class="form-control" rows="15" id="codigo"><?php echo htmlentities($conteudoParaApresentar); ?></textarea>
    </div>
    </div>
  </div>
</section>
  • <?php echo htmlentities($text); ? >And I can put this inside the text area??

  • Thank you You saved my life Ahah

  • can yes, I posted an example using your code. In this case, simply replace the $text variable with the variable containing the html code.

  • then and in case I don’t want the variable but I write the code myself I have to match that same variable before? ?

  • In this case, if you do not want to use variable just do so: <?php echo htmlentities('<p>aqui vem todo o código!</p><hr><br>Tudo funciona!'); ?>

  • Thanks for everything!! helped a lot!! and sorry for anything

  • I tested without the htmlentities and it worked just the same.

  • It also works, but htmlentitles does an encoding, according to official documentation: http://php.net/manual/en/function.htmlentities.php, already omitting this function you are not escaping anything, only printing, in some cases it may not work.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.