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 ?>
Related
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...
I am working on a script with templates. So I have this PHP code:
<?php
$string = "TEST";
echo(file_get_contents('themes/default/test.html'));
?>
And I have this HTML (the test.html file):
<html>
<p>{$string}</p>
</html>
How can I make PHP actually display the variable inside the curly brackets? At the moment it displays {$string}.
P.S:
The string might also be an object with many many variables, and I will display them like that: {$object->variable}.
P.S 2: The HTML must stay as it is. This works:
$string = "I'm working!"
echo("The string is {$string}");
I need to use the same principle to display the value.
You can use the following code to achieve the desired result:
<?php
$string = "TEST";
$doc = file_get_contents('themes/default/test.html'));
echo preg_replace('/\{([A-Z]+)\}/', "$$1", $doc);
?>
P.S. Please note that it will assume that every string wrapped in { }
has a variable defined. So No error checking is implemented in the code above. furthermore it assumes that all variables have only alpha characters.
If it is possible to save your replacees in an array instead of normal variables you could use code below. I'm using it with a similar use case.
function loadFile($path) {
$vars = array();
$vars['string'] = "value";
$patterns = array_map("maskPattern", array_keys($vars));
$result = str_replace($patterns, $vars, file_get_contents($path));
return $result;
}
function maskPattern($value) {
return "{$" . $value . "}";
}
All you PHP must be in a <?php ?> block like this:
<html>
<p><?php echo "{" . $string . "}";?></p>
</html>
If you know the variable to replace in the html you can use the PHP function 'str_replace'. For your script,
$string = "TEST";
$content = file_get_contents('test.html');
$content = str_replace('{$string}', $string, $content);
echo($content);
It's simple to use echo.
<html>
<p>{<?php echo $string;?>}</p>
</html>
UPDATE 1:
After reading so many comments, found a solution, try this:
$string = "TEST";
$template = file_get_contents('themes/default/test.html', FILE_USE_INCLUDE_PATH);
$page = str_replace('{$string}',$string,$template);
echo $page;
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 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
Want to replace some words on the fly on my website.
$content = preg_replace('/\bWord\b/i', 'Replacement', $content);
That works so far. But now i want only change the the words which are inside
div id="content"
How do i do that?
$dom = new DOMDocument();
$dom->loadHTML($html);
$x = new DOMXPath($dom);
$pattern = '/foo/';
foreach($x->query("//div[#id='content']//text()") as $text){
preg_match_all($pattern,$text->wholeText,$occurances,PREG_OFFSET_CAPTURE);
$occurances = array_reverse($occurances[0]);
foreach($occurances as $occ){
$text->replaceData($occ[1],strlen($occ[0]),'oof');
}
//alternative if you want to do it in one go:
//$text->parentNode->replaceChild(new DOMText(preg_replace($pattern,'oof',$text->wholeText)),$text);
}
echo $dom->saveHTML();
//replaces all occurances of 'foo' with 'oof'
//if you don't really need a regex to match a word, you can limit the text-nodes
//searched by altering the xpath to "//div[#id='content']//text()[contains(.,'searchword')]"
use the_content filter, you can place it in your themes function.php file
add_filter('the_content', 'your_custom_filter');
function your_custom_filter($content) {
$pattern = '/\bWord\b/i'
$content = preg_replace($pattern,'Replacement', $content);
return $content;
}
UPDATE: This applies only if you are using WordPress of course.
If the content is dynamically driven then just echo the return value of $content into the div with id of content. If the content is static then you'll have to either use this PHP snippet on the text then echo out the return into the div, or use JavaScript (dirty method!).
$content = "Your string of text goes here";
$content = preg_replace('/\bWord\b/i', 'Replacement', $content);
<div id="content">
<?php echo $content; ?>
</div>