A begginers question. I have this little code:
<?php
$content = 'hello there, hello!';
echo substr_count("$content","hello");
?>
How could i replace 'hello there, hello!' part with another php enclosure, like
<?php the_content(); ?>
for example. What is the correct way of doing this? Thanks!
<?php
echo substr_count(the_content(),"hello");
?>
This:
<?php
$a = 10;
?>
<?php
$b = 20;
?>
<?php
echo $a + $b;
?>
--output:--
30
is equivalent to:
<?php
$a = 10;
$b = 20;
echo $a + $b;
?>
If the_content does not return a meaningful value but rather echos things out, use output buffering:
ob_start();
the_content();
$captured_content = ob_get_clean();
echo substr_count($captured_content, "hello");
$content = the_content();
echo substr_count($content,"hello");
or
echo substr_count(the_content(),"hello");
It is really the basics, try to look at some tutorials ;)
Related
I just want the last value to be printed, but it is printing all of them:
<?php if (get_field('share_sentiment')):?>
<?php while (has_sub_field('share_sentiment')):?>
<?php if (get_sub_field('share_medium')):?>
<?php $kid = 0; ?>
<?php while (has_sub_field('share_medium')):?>
<?php
$negative += get_sub_field('medium_negative');
$positive += get_sub_field('medium_positive');
$totalMinus = ($positive - $negative) / ($positive + $negative);
$rounded = round($totalMinus, 3);
print_r($rounded);
?>
<?php endwhile;?>
<?php endif;?>
<?php endwhile;?>
<?php endif;?>
Put print_r($rounded); after the loop. And $rounded = round($totalMinus, 3); too, I guess.
Try to use
echo $variable;
instead of print_r since the language used is php?
I'm stuck on how to write the test.php page result (after php has run) to a string:
testFunctions.php:
<?php
function htmlify($html, $format){
if ($format == "print"){
$html = str_replace("<", "<", $html);
$html = str_replace(">", ">", $html);
$html = str_replace(" ", " ", $html);
$html = nl2br($html);
return $html;
}
};
$input = <<<HTML
<div style="background color:#959595; width:400px;">
<br>
input <b>text</b>
<br>
</div>
HTML;
function content($input, $mode){
if ($mode =="display"){
return $input;
}
else if ($mode =="source"){
return htmlify($input, "print");
};
};
function pagePrint($page){
$a = array(
'file_get_contents' => array($page),
'htmlify' => array($page, "print")
);
foreach($a as $func=>$args){
$x = call_user_func_array($func, $args);
$page .= $x;
}
return $page;
};
$file = "test.php";
?>
test.php:
<?php include "testFunctions.php"; ?>
<br><hr>here is the rendered html:<hr>
<?php $a = content($input, "display"); echo $a; ?>
<br><hr>here is the source code:<hr>
<?php $a = content($input, "source"); echo $a; ?>
<br><hr>here is the source code of the entire page after the php has been executed:<hr>
<div style="margin-left:40px; background-color:#ebebeb;">
<?php $a = pagePrint($file); echo $a; ?>
</div>
I'd like to keep all the php in the testFunctions.php file, so I can place simple function calls into templates for html emails.
Thanks!
You can use output buffering to capture the output of an included file and assign it to variable:
function pagePrint($page, array $args){
extract($args, EXTR_SKIP);
ob_start();
include $page;
$html = ob_get_clean();
return $html;
}
pagePrint("test.php", array("myvar" => "some value");
And with test.php
<h1><?php echo $myvar; ?></h1>
Would output:
<h1>some value</h1>
This may not be exactly what you're looking for but it seems you want to build an engine of sorts for processing email templates into which you can put php functions? You might check out http://phpsavant.com/ which is a simple template engine that will let you put in php functions directly into a template file as well as basic variable assignment.
I'm not sure what printPage is supposed to be doing but I would re-write it like this just to make it more obvious because the array of function calls is a bit complicated and I think this is all that is really happening:
function pagePrint($page) {
$contents = file_get_contents($page);
return $page . htmlify($contents,'print');
};
and you might consider getting rid of htmlify() function and use either of the built-in functions htmlentities() or htmlspecialchars()
Seems like my original method may not have been the best way of going about it. Instead of posing a new question on the same topic, figured it was better to offer an alternate method and see if it leads to the solution I am after.
testFunctions.php:
$content1 = "WHOA!";
$content2 = "HEY!";
$file = "test.html";
$o = file_get_contents('test.html');
$o = ".$o.";
echo $o;
?>
text.php:
<hr>this should say "WHOA!":<hr>
$content1
<br><hr>this should say "HEY!":<hr>
$content2
I'm basically trying to get $o to return a string of the test.php file, but I want the php variables to be parsed. as if it was read like this:
$o = "
<html>$content1</html>
";
or
$o = <<<HTML
<html>$content1</html>
HTML;
Thanks!
Basically, I need to view the PHP code of a file, after includes. I am trying to see EXACTLY what PHP code is run. eg...
<?php // a.php
$a = 10;
?>
<?php // b.php
include('a.php');
$b = 20;
?>
If I was trying to get the code of b.php, it would display the following:
<?php
$a = 10;
$b = 20;
?>
Is that possible? If so, how?
// at the end of your script
<?php
$totalRunCode = '';
$scripts = get_included_files();
foreach($scripts as $script)
{
$totalRunCode .= file_get_contents($script);
}
// do whatever with totalRunCode
Though I don't know why you'd want to do this.
When I try and echo this string it shows "0"
I tried it on my local server and on http://writecodeonline.com/php/ both times same thing happened. This has never happened to me before, what is it and how do I fix? Thanks in advance.
<?PHP
$info = '
<div id="gallery_option_###number###">
<a href="#galleryButton###number###" onclick="gallery_button_down(' + "'###number###'" + ')">
Burn Notice
</a>
<div id="info_option_###number###">
<!--
[title]title[|title]
[description]test[|description]
[image]url[|image]
-->
</div>
</div>';
echo $info;
?>
You are following JavaScript way of string concatenation.
Read:
http://php.net/manual/en/language.operators.string.php
<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"
$a = "Hello ";
$a .= "World!"; // now $a contains "Hello World!"
?>
how do I do when I want to preg_replace a href, but only if it's my own?
$a = 'href="http://mysite.com/?s=Bananas&lang=en"';
$host = 'http://mysite.com';
$a = preg_replace('#href="'.$host.'\/?[(s|p)]=([.*?])&lang=([.*?])"#e','href="index.php#$1\/$2\\lang\/$3"',$a);
//The result I want:
echo $a;
//Becomes href="http://mysite.com/#s/Bananas\\lang/en"
But what am I doing wrong?
This regex-syntax is very difficult...
<?php
$a = 'href="http://mysite.com/?s=Bananas&lang=en"';
$host = 'http://mysite.com';
echo preg_replace('#href="'.preg_quote($host).'/\?(s|p)=(.*?)&lang=(.*?)"#','href="'.$host.'/#$1/$2\\\\\lang/$3"',$a);
?>
This seems to work for me :)