I do have that code:
ob_start();
include($this->testTpl);
$html = ob_get_clean();
$pattern = '/{{{\s*(.+?)\s*}}}(\r?\n)?/s';
echo preg_replace_callback($pattern, function($matches) {
return "<?php htmlspecialchars({$matches[1]}); ?>";
}, $html);
and the testTpl file is a php file having this inside:
<div class="something">
<ul>
<li>{{{ $data->something }}}</li>
</ul>
the text is replaced but when I echo it what's returned is:
<li><!--?php htmlspecialchars($data--->something); ?></li>
I don't have the smallest clue why that's happening... anyone any thoughts? any help is appreciated
It's already PHP no need for another set of tags <?php ?>
ob_start();
include($this->testTpl);
$html = ob_get_clean();
$pattern = '/{{{\s*(.+?)\s*}}}(\r?\n)?/s';
echo preg_replace_callback($pattern, function($matches) {
return htmlspecialchars({$matches[1]});
}, $html);
I ended by doing that:
$html = file_get_contents($this->testTpl);
$pattern = '/{{{\s*(.+?)\s*}}}(\r?\n)?/s';
$output = preg_replace_callback($pattern, function($matches) {
return "<?php echo htmlspecialchars($matches[1]); ?>";
}, $html);
file_put_contents(sys_get_temp_dir().'/temp.php', $output);
include(sys_get_temp_dir().'/temp.php');
in this way, the newly created file will be rendered correctly. If anyone has any other idea how to do it without writing the file physically on the /tmp folder...
Related
I'm trying to convert [quote="author"]text[/quote] into a formatted div block.
function bbCode($p_text){
// capture author
$pattern = '/\[quote="(.+)"\]/';
preg_match($pattern, $p_text, $match);
// replace bbcode with formatted block
$patternA = '/\[quote=".+"\]/'; // captures [quote="..."]
$replacementA = '</p><div class="quote"><strong class="quote-author"><?php echo $match; ?> wrote:</strong><br>';
$p_text = preg_replace($patternA, $replacementA, $p_text);
$patternB = '/\[\/quote\]/'; // captures [/quote]
$replacementB = '</div><p class="thread-p">';
$p_text = preg_replace($patternB, $replacementB, $p_text);
return $p_text;
}
In the main file:
<?php
// $p_text is defined before this
$p_text = bbCode($p_text);
?>
<p class="thread-p"><?php echo $p_text; ?></p>
It seems to be just coming up with [quote="author"]text then the div closes some other section of the html below. Not sure what I've done wrong.
I've tried to end the <p> because divs can't go inside <p>'s iirc, maybe that's messing something up?
I understand your question to be that you would like to take
[quote="author"]text[/quote]
and create HTML which will display the author and text.
This is one possible implementation:
<?php
function bbCode($p_text) {
$pattern = '#\[quote="([^\"]+)"\]([^\[]+)\[/quote\]#';
preg_match_all($pattern,$p_text,$matches);
$html = <<< HTML
<div class="quote">
<span class="quote-author">__AUTHOR__ wrote:</span>
<p>__QUOTE__</p>
</div>
HTML;
return str_replace(['__AUTHOR__','__QUOTE__'],[$matches[1][0],$matches[2][0]], $html);
}
echo bbCode('[quote="author"]text[/quote]');
I have a PHP file that renders an HTML file, inside this HTML file I have this piece of code
<div class="app">{: echo $this->content :}</div>
And I want to replace the opening {: and closing :} tags with the traditional <?php ?> tags to make it look something like this:
<div class="app"><?php echo $this->content ?></div>
$contents = file_get_contents ("file.php");
$contents = str_replace(array('{:', ':}'), array('<?php', '?>'), $contents);
file_put_contents("file.php", $contents);
You could do a simple string replace if you are doing this within PHP
str_replace(array('{:', ':}'), array('<?php', '?>'), $file_content)
Try this :
$string = '<div class="app">{: echo $this->content :}</div>';
$string = str_replace('{:','<?php',$string);
$string = str_replace(':}','?>',$string);
If you want to catch the content inside the you can use this :
$string = '<div class="app">{: echo $this->content :}</div>';
preg_match('/<div class="app">(.+)<\/div>/',$string,$preg_array);
$string = str_replace('{:','<?php',$preg_array[1]);
$string = str_replace(':}','?>',$string);
Output :
<?php echo $this->content ?>
I want to create a page with include function. This page should to grab another site and can change text or code with this function str_replace. I hope that it would be possible. I have written this code, but unfortunately it does not work:
<?php
$text = include('http://www.example.com/index.html');
$text = str_replace("<div id=\"hercss\">Hello.</div>", "<div id=\"mycss\">Welcome!</div>", $text);
echo $text;
?>
Maybe you have a solution? It would be fully appreciated by you.
Try this:
<?php
$url = "http://www.example.com/index.html";
$text = file_get_contents($url);
$text = str_replace("<div id=\"hercss\">Hello.</div>", "<div id=\"mycss\">Welcome!</div>", $text);
echo $text;
?>
Include only works with relative links I believe? The function file_get_contents works across domains
I have the code below on a page basically what I'm trying to do is fill $content variable using the function pagecontent. Anything inside pagecontent function should be added to the $content variable and then my theme system will take that $content and put it in theme. From the answers below it seems you guys think I want the html and php inside the actual function I don't.
This function below is for pagecontent and is what I'm currently trying to use to populate $content.
function pagecontent()
{
return $pagecontent;
}
<?php
//starts the pagecontent and anything inside should be inside the variable is what I want
$content = pagecontent() {
?>
I want anything is this area whether it be PHP or HTML added to $content using pagecontent() function above.
<?php
}///this ends pagecontent
echo functional($content, 'Home');
?>
I think you're looking for output buffering.
<?
// Start output buffering
ob_start();
?> Do all your text here
<? echo 'Or even PHP output ?>
And some more, including <b>HTML</b>
<?
// Get the buffered content into your variable
$content = ob_get_contents();
// Clear the buffer.
ob_get_clean();
// Feed $content to whatever template engine.
echo functional($content, 'Home');
As you are obviously a beginner here's a much simplified, working version to get you started.
function pageContent()
{
$html = '<h1>Added from pageContent function</h1>';
$html .= '<p>Funky eh?</p>';
return $html;
}
$content = pageContent();
echo $content;
The rest of the code you post is superfluous to your problem. Get the bare minimum working first then move on from there.
Way 1:
function page_content(){
ob_start(); ?>
<h1>Hello World!</h1>
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
$content .= page_content();
Way 2:
function page_content( & $content ){
ob_start(); ?>
<h1>Hello World!</h1>
<?php
$buffer = ob_get_contents();
ob_end_clean();
$content .= $buffer;
}
$content = '';
page_content( $content );
Way 3:
function echo_page_content( $name = 'John Doe' ){
return <<<END
<h1>Hello $name!</h1>
END;
}
echo_page_content( );
I have a file B590.php which is having a lot of html code and some php code (eg logged in username, details of user).
I tried using $html = file_get_content("B590.php");
But then $html will have the content of B90.php as plain text(with php code).
Is there any method where I can get the content of the file after it has been evaluated?
There seems to be many related questions like this one and this one but none seems to have any definite answer.
You can use include() to execute the PHP file and output buffering to capture its output:
ob_start();
include('B590.php');
$content = ob_get_clean();
function get_include_contents($filename){
if(is_file($filename)){
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
$html = get_include_contents("/playbooks/html_pdf/B580.php");
This answer was originally posted on Stackoverflow
If you use include or require the file contents will behave as though the current executing file contained the code of that B590.php file, too. If what you want is the "result" (ie output) of that file, you could do this:
ob_start();
include('B590.php');
$html = ob_get_clean();
Example:
B590.php
<div><?php echo 'Foobar'; ?></div>
current.php
$stuff = 'do stuff here';
echo $stuff;
include('B590.php');
will output:
do stuff here
<div>Foobar</div>
Whereas, if current.php looks like this:
$stuff = 'do stuff here';
echo $stuff;
ob_start();
include('B590.php');
$html = ob_get_clean();
echo 'Some more';
echo $html;
The output will be:
do stuff here
Some more
<div>Foobar</div>
To store evaluated result into some variable, try this:
ob_start();
include("B590.php");
$html = ob_get_clean();
$filename = 'B590.php';
$content = '';
if (php_check_syntax($filename)) {
ob_start();
include($filename);
$content = ob_get_clean();
ob_end_clean();
}
echo $content;