I have this data in my database that i fetched:
As you can see i store an html template in my database.
This is the code i used to output that html:
$php = Blade::compileString($template->content);
dd($php) //i used laravel framework, btw.
Then this is the output:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exmaple.com</title>
<style>
</style>
</head>
<body>
<div class="account-details">
<b>Account Name</b>: <?php echo e(company.name); ?><br>
<b>Plan</b>: <?php echo e(plan.name); ?><br>
<b>Source</b>: <?php echo e(source.name); ?><br>
<b>Source Type</b>: <?php echo e(source.source_type.name); ?><br>
<br>
</div>
<div class="welcome" style="font-family:Arial;font-size:small">
Hi <?php echo e(user.first_name); ?>,<br>
<br>
There seems to be a problem blah blah blah<br>
<br>
Details of the problem are:<br>
<?php echo e(sourceMessage); ?><br>
<br>
You can check and update the source here:<br>
https://example.com<br>
<br>
<br>
<span style="font-size:12.8px">
Kind regards,<br>
<br>
Test Team<br>
<br>
Email: example.com<br>
Website: example.com<br>
</span>
</div>
</body>
</html>
I want to change the .(dot) into -> so in this example, some of text will output like:
from <?php echo e(company.name); ?> to <?php echo e(company->name); ?>
from <?php echo e(source.name); ?> to <?php echo e(source->name); ?>
So i think if it's in a <?php echo e(whatever); ?> that's the time we check and replace the . with -> ?
I think this can be done by RegEx but I'm not expert on that.
The reason why i wanted to replace it is because i am getting a template from an email service then returns ., so i wanted to replace that with -> because I know PHP reads -> in accessing objects rather than ..
You can use preg_replace to look for pieces of code matching <?php ... e(f) and replace the .s in f with ->:
$html = preg_replace_callback('/(<\?php\s+.*?\be\()([^)]+\))/',
function ($m) {
return "{$m[1]}$" . str_replace('.', '->', $m[2]);
},
$html);
Note we use a callback as it makes it easier to deal with replacing a.b.c with a->b->c. Also, to really look like PHP, you need to add a $ at the beginning of the variable name, which this code does. If you don't want it, just change {$m[1]}$ to {$m[1]}
Demo on 3v4l.org
If you're doing that on a editor like VS code or sublime, you just need to replace the function e usage parts.
CTRL + H
(In sublime or VS code) to open up replace dialog.
Make sure the Regex option is clicked.
This would do the trick;
Search (e\(.+)\.(.+\))
Replace $1->$3
Related
I have added a line break in this piece of PHP code, but it's getting a syntax error.
Does PHP not allow breaks in its code?
<!DOCTYPE>
<html>
<head>
</head>
<body>
</body>
<?php
define("Value_of_Pi", 3.14);
define ("Gravity_Value", 9.8);
echo "Pi value is".Value_of_Pi;
<br/>
echo "Gravity Value is".Gravity_Value;
?>
</html>
You can use \r\n if you want to break line within php.
<?php
define("Value_of_Pi", 3.14);
define ("Gravity_Value", 9.8);
echo "Pi value is".Value_of_Pi. "\r\n";
echo "Gravity Value is".Gravity_Value;
?>
<br/> is not valid PHP syntax. You could either have the <br/> outside the PHP block:
<?php
define("Value_of_Pi", 3.14);
define ("Gravity_Value", 9.8);
echo "Pi value is".Value_of_Pi;
?>
<!-- PHP block terminated -->
<br/>
<!-- New PHP block opened: -->
<?PHP
echo "Gravity Value is".Gravity_Value;
?>
Or just echo it from PHP:
<?php
define("Value_of_Pi", 3.14);
define ("Gravity_Value", 9.8);
echo "Pi value is".Value_of_Pi;
echo "<br/>"; # Here!
echo "Gravity Value is".Gravity_Value;
?>
You need to write echo '<br/>';.
This is because you can either write HTML code or PHP within the specific sections.
You are currently in a PHP code section so you can only write PHP code there that will be valid.
<br/> is not a valid PHP code but HTML. However, because you are within <?php ?> it is not valid.
You can use the code below instead:
echo '<br/>';
<br/> is an HTML tag hence the syntax error you're getting.
<br> is html and must be echoed within php, like so:
<?php
define("Value_of_Pi", 3.14);
define ("Gravity_Value", 9.8);
echo "Pi value is".Value_of_Pi;
echo "<br/>";
echo "Gravity Value is".Gravity_Value;
?>
You can also use \r\n in php like so:
echo "Pi value is".Value_of_Pi. "\r\n";
I'm using this line of php in my main page
echo generateRadioButtons("fbresponse.php", "moRating1", 6);
Which when posting the following on the response file
echo $_POST['moRating1']
It works fine and displays the correct result, but! my question is how would i add text to that so..
Blah blah blah, you rated x question: 'moRating1'
I've tried doing
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $moRating1 ?></p>
</body>
</html>
inside the response file but that just doesnt load anything..
Any help please!
It's probably because this function uses eval() to execute its content (I guess it from lack of PHP tags in your first example).
If it's true, then you should be able to close PHP tag, print HTML and open it again.
?>
<html>
<head>
<title>Questions</title>
</head>
<body>
<h1>Survey responses</h1>
<p>How well did you rate it : <?php print $_POST['moRating1'] ?></p>
</body>
</html>
try doing:
$mRating1 = $_POST['moRating1'];
...
?>
...
<p>How well did you rate it: <?php echo $mRating1?></p>
I'm trying to call an HTML/PHP content that it's inside my database using:
<?php echo $row_content['conteudo']; ?>
When the row is called the HTML appears correctly but the PHP doesn't.
I belieave it's cause of the echo inside the main echo.
<?php echo "
<h3>Hello</h3>
<?php do { ?>
<div class=\"indios\">
<a href=\"indio.php?id=<?php echo $row_indiosct['id']; ?>\">
<img src=\"galeria/indios/<?php echo $row_indiosct['foto']; ?>\" alt=\"<?php echo $row_indiosct['nome']; ?>\" />
<br /><?php echo $row_indiosct['nome']; ?></a></div>
<?php } while ($row_indiosct = mysql_fetch_assoc($indiosct)); ?> "
?>
The line one of this code is the same echo as the first code field, it's not repeating, it's there just for help and to understand where is the problem.
I already fixed some quotation marks but it gives an error in the line of the 1st echo.
That is some of the ugliest code I have ever seen...
<?php
echo '
<h3>Hello</h3>';
while ($row_indiosct = mysql_fetch_assoc($indiosct))
{
echo '
<div class="indios">
<a href="indio.php?id='.$row_indiosct['id'].'">
<img src="galeria/indios/'. $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
}
?>
You could also use the HEREDOC syntax.
Don't do this. Multi-line echoes, especially when you've got embedded quotes, quickly become a pain. Use a HEREDOC instead.
<?php
echo <<<EOL
<h3>Hello</h3>
...
<div class"indios">
...
EOL;
and yes, the PHP inside your echo will NOT execute. PHP is not a "recursively executable" language. If you're outputting a string, any php code embedded in that string is not executed - it'll be treated as part of the output, e.g.
echo "<?php echo 'foo' ?>"
is NOT going to output just foo. You'll actually get as output
<?php echo 'foo' ?>
You have misunderstood how PHP works. PHP is processed by the server. When it encounters your script, it sees the following:
<?php echo "some long piece of text that you have told PHP not to look at" ?>
What is the reasoning behind trying to nest PHP calls inside strings?
evaluate code php in string using the function eval(): this post Execute PHP code in a string
<?php
$motto = 'Hello';
$str = '<h1>Welcome</h1><?php echo $motto?><br/>';
eval("?> $str <?php ");
http://codepad.org/ao2PPHN7
also if your need the code buffer output in a string also you can using the ob_start() method:
<?php ob_start(); ?>
<h3>Hello</h3>;
<?php
while ($row_indiosct = mysql_fetch_assoc($indiosct)){ ?>
<div class="indios">
<a href="indio.php?id='<?php echo $row_indiosct['id']'">
<img src="galeria/indios/'<?php echo $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
<?php } ?>
I have a scenario like this: Simply a *.php page with a PHP content on top followed with a HTML content. Now with an application of PHP_SELF, I need to echo the message within HTML without loosing HTML interface once the message is echoed within the 'div1' tag. Is this possible?
I tried replacing echo with $msg['div1'] = 'Some message' and inside div1 tag <?= $msgxx['display_log'] ?> but doesn't seems to be a valid code. Please share your views. Code below just to explain my approach. Thanks.
<?php
echo 'some message';
?>
<html>
<form .....>
<div>
<div id="div1"></div>
<div><input type="submit" id="submit1"></div>
</div>
</html>
In php you $message='<b>Hello, world!</b>'. In html
<div id="div1"><?php echo $message ?></div>
You can include inline PHP in your html, perhaps that is where the confusion is coming from? It doesn't always have to be at the top of the page. just wrap your PHP with the <?php ... ?> tags.
In your case, if you wanted to echo some message inside of div1:
<div id="div1"><?php echo 'some message'; ?></div>
Are you trying to echo PHP data within HTML? If that is the case, keep in mind that you can just write your PHP and HTML code just mixed like this. Otherwise, please explain more what you are trying to do and consider posting more detailed code as it's not very clear to me.
<html>
<head>
...
</head>
<body>
<form>
<div id="div1"><?php echo 'some message'; ?></div>
<input type="submit" id="submit1">
</form>
</body>
</html>
You can also use <?= 'some message' ?> which is equivalent to <?php echo 'some message' =>.
I wanted to know, is there any way to insert an HTML page into PHP without using the include function? I do not want to use an external html file, I want to write the html coding directly into the php coding.
Thanks for your help!
Interleave it:
<?php
// Some php code.
?>
<html>
<head>
<title>Hello!</title>
</head>
<body>
<h1>Header</h1>
<?php /* More php code. */ ?>
<p>Blah!</a>
</body>
</html>
<?php /* Even more php. */ ?>
From a best practices point of view, though, avoid doing this - having business logic (PHP) and presentation (HTML) in the same place makes maintaining harder.
EDIT: To address your comment. You can either do it the same way, or use echo:
<?php if (x == 5) { ?>
<p>Blah!</a>
<?php } else {
echo '<p>Bleh</p>';
} ?>
If you need to include snippets of HTML based on conditions, you can interleave code like this. In this case it's convenient to use the alternative syntax for loop controls
<?php if ( $var ): ?>
<html>
<title>YAY</title>
</html>
<?php endif; ?>
so the code is clearer to read and you retain HTML syntax coloring (if your editor supports it).
It is very bad habit to mix HTML and PHP (for more than just output control), but here you go:
$html = "<div>This is HTML</div>"
echo $html;
or Heredoc syntax:
$html = <<<EOF
<div>
<p>
Some longer HTML
</p>
</div>
EOF;
echo $html;
or using alternative syntax for control statements if the output depends on some condition (or if you loop through an array etc.)(which is far better than building HTML with strings):
<?php if($foo): ?>
<div> Some HTML output </div>
<?php else: ?>
<div> Some other HTML </div>
<?php endif; ?>
or just
<?php //PHP here ?>
<div>HTML</div>
<?php //more PHP ?>
<div>more HTML</div>
<?php //even more PHP ?>