Replace for html

Asked

Viewed 456 times

0

So, guys, check my code:

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

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

The username is at the top of the page, not the tag you’d like. As I replace #USERNAME# to get the 'name' string from print($_SESSION['name'] I cannot leave the html file in php, because MVC is used.

See below how it looks.

Thank you.

  • I don’t understand. What’s in $html before str_replace? And the correct way to assign the value to the $username variable is: $username = $_SESSION['name'];

2 answers

2

I will list two existing errors:

1. Print():

First in this case the print is not correct, just remove.

<--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 using the print will be literally displaying the content.

Don’t you get it? Take this example:

$qualquer_coisa = print('meu_nome');

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

And the variable $qualquer_coisa? This will hold the value 1, If it was working perfectly, the #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 a mistake.

What the variable $html does it have? Nothing.

To solve I advise making two files, for example:

A file to save purely 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 is that?

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

  • I didn’t understand why to do this. I already have the html files, I just need to replace a php variable and play it in html. Type like this. &#xA;&#xA;No php --------------------------------------------------------------------------------&#xA;&#xA;$nomeusuario = $_SESSION['nome'];&#xA;&#xA;$html = str_replace('#NOMEUSUARIO#', $nomeusuario);&#xA;&#xA;No html---------------------------------------------------------------------------------&#xA;<strong>#NOMEUSUARIO#</strong>

  • You may have HTML, but it is not mentioned. PHP can only manipulate what is set. There is no substitution without first loading the text. For example: "I speak cookie", if you want to exchange "cookie" for "cookie" you will have to FIRST define some variable to have the phrase "I speak cookie". Otherwise there is no way to define. Logico, if you have minimum notion you can make a file_get_contents in the file itself and then "find" the html (even make a explode basic in the <--html-->) and then a variable will get the HTML of the file. But it will depend on you.

  • Thanks for the help friend. I’m starting in php. I managed to do otherwise, with the menu being done in php direct, in the Menu class. No need to upload an html file. Thank you.

-1

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

php

$nomeusuario = $_SESSION['nome'];

html

Olá, <strong><?php echo $nomeusuario; ?></strong> seja bem vindo
  • It doesn’t work in MVC mode, I can’t change the html structure.

Browser other questions tagged

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