Replace for html [closed]

0

Then, look at my code:

<--php-->
$nomeusuario = print($_SESSION['nome']);
$html = str_replace('#NOMEUSUARIO#', $nomeusuario, $html);

<--html-->
Olá, <strong>#NOMEUSUARIO#</strong> seja bem vindo

The user name is at the top of the page, not the tag you like. How do I replace the #NAME # to get the string 'name' of the print ($ _ SESSION ['name']? I can not leave the html file in php, as I use MVC.

See below for how it looks.

Thank you.

    
asked by anonymous 10.02.2016 / 20:19

2 answers

2

I will list two existing errors:

1. Print ():

First of all, the print is not correct, just remove it.

<--php-->
$nomeusuario = $_SESSION['nome'];
$html = str_replace('#NOMEUSUARIO#', $nomeusuario, $html);

<--html-->
Olá, <strong>#NOMEUSUARIO#</strong> seja bem vindo

The reason for this is simple. When you use print you will be literally displaying content.

Do not you understand? Here's an example:

$qualquer_coisa = print('meu_nome');

This will display meu_nome in the file, usually at the beginning since the html is after this function. But, the result will be displayed where it is called.

And the variable $qualquer_coisa ? This will have the value 1 , CASE would be working perfectly, #NOMEUSUARIO# would be replaced by 1 .

2. $ html?!

There is a detail, I do not know if it was created by you when posting the question, but I will be considering an error.

What does $html have? Nothing.

To solve, I suggest you make two files, for example:

A file to save pure HTML:

  

/public_html/html_index.html ~ Example, but leave before public_html!

Olá, <strong>#NOMEUSUARIO#</strong> seja bem vindo
  

/public_html/index.php

$nomeusuario = $_SESSION['nome'];

$arquivo = 'html_index.html'; 
// Caminho de exemplo!    

$html = file_get_contents( $arquivo );
// Carrega o HTML, do outro arquivo.

$html = str_replace('#NOMEUSUARIO#', $nomeusuario, $html);

echo $html;
// Exibe o HTML, já alterado.

Why this?

The file_get_contents() will get the HTML of the other file, which is pure HTML, so str_replace() will just replace the content that was previously loaded.

    
10.02.2016 / 20:34
-1

Hello. You can do to display the user name with php itself, like this:

  

php

$nomeusuario = $_SESSION['nome'];
  

html

Olá, <strong><?php echo $nomeusuario; ?></strong> seja bem vindo
    
10.02.2016 / 23:52