PHP: count the words in a DIV - php

I say PHP, because I have this snippet to count the words with PHP, maybe it's better with jQuery?
$words = str_word_count(strip_tags($myString));
I have a PHP page with static HTML mixed with some PHP variables like so:
<?php
$foo = "hello";
?>
<html>
<body>
<div>total words: <?= $words ?></div>
<div class="to_count">
<?= $foo ?> <b>big</b> <i>world</i>, how <span>are</span> we today?
</div>
</body>
</html>
I tried looking into PHP's output buffering and slipped an ob_start() and $buffer = ob_get_clean(); around the .to_count DIV, but I can't seem to use the $buffer in the top section on the PHP page to count the words.
Any help to set me on the way is appreciated, cheers.

With jQuery and regex:
var wordCount = $.trim($(".to_count").text()).split(/\s+/g).length;

you can't use buffer before it is declared. If you do it will default to a value that isn't useful. I recommend counting the words before inserting them into the HTML and setting a variable with the count.

I recommend building the content of the .to_count div before actually rendering it. Something like this:
<?php
$foo = "hello";
$content = "$foo <b>big</b> <i>world</i>, how <span>are</span> we today?";
$words = str_word_count(strip_tags($content));
?>
<html>
<body>
<div>total words: <?= $words ?></div>
<div class="to_count"><?= $content ?></div>
</body>
</html>

You could use output buffering to generate it. Which I think is tider than generating HTML in php.
<?php
ob_start();
$foo = "hello";
?>
<?php echo $foo ?> <b>big</b> <i>world</i>, how <span>are</span> we today?
<?php
$myString = ob_get_contents();
ob_end_clean();
$words = str_word_count(strip_tags($myString));
?>
<html>
<body>
<div>total words: <?php echo $words ?></div>
<div class="to_count">
<?php echo $myString ?>
</div>
</body>
</html>

Related

put string in variable using <? ?>

I think it is sometimes easier to use php tags instead of echo for example
<?
if()
echo "<img src='' onclick='alert(\"hello\")'/>";
?>
instead of that I code like this
<?
if(){
?>
<img src='' onclick='alert("hello")'/>
<?}
?>
We got rid of backslashing. But what about strings I want something like this:
<?
$str="?>
<img src='' onclick='alert("hello")'/>
<?";
?>
You should use the PHP heredoc syntax:
<?php
$str = <<<IMGTAG
<img src="" onclick="alert('hello')"/>
IMGTAG;
echo $str;
?>
Enjoy your code.
There is an alternative Syntax specifically for this kind of formation:
<?php if (x): ?>
<div>...</div>
<?php endif; ?>
Also there are short tags:
<?= "hello world" ?>
This directly prints a string and is equal to:
<?php echo "hello world" ?>
For string assignment you can do what Magicianred sugested. You could also do it with output buffering:
<?php ob_start(); ?>
<div>test</div>
<?php
$str = ob_get_contents();
ob_end_clean();
echo $str;
?>
Though output buffering shouldn't be abused for this. Heredoc syntax is the best solution here.

Return big HTML block with some php without make string

How can i return big html block with some php by using <<<HTML HTML; .
return <<<HTML
<div>Here some text</div>
<?php thisFunctionEchosomthingNotReturn(); ?>
<?php if($isflag){?>
<span>DO not do this</span>
<?php } ?>
<?php echo $whatever; ?>
HTML;
I can't understand what will work and what will not! how should i use this kind of return <<<HTML HTML; block with some php variable that i need to echo and some function that echo some thing (not return)
You can use 'capture output' for this task. see Output Control Functions
i has some example code that i have just tested. It captures the output of the div tag in $out1 and shows it again later.
This technique is used in many 'templating' libraries and in 'views' in the 'frameworks'.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test of Output control functions</title>
</head>
<body>
<?php ob_start(); // capture the buffer ?>
<div style="border: 4px solid red">
<p>This is a test paragraph</p>
<p>This is test PHP code: <?php echo time(); ?></p>
</div>
<?php $out1 = ob_get_contents(); // end capture ?>
</body>
</html>
<?php echo $out1; // output now or save for later. ?>
<?php var_dump($out1, strlen($out1)); ?>
<?php exit; ?>
Okay, google heredoc syntax for PHP.
but this is how it works (which I think you are trying to do.
$html = <<<HTML
<div>
<h1>$phpVariableTitle</h1>
<div>
{$thisFunctionEchosomthingNotReturn()}
</div>
</div>
HTML;
return $html;
Try that. IMPORTANT! heredoc syntax requires your closing tag be left aligned with no tabs. So make sure there are no spaces or tabs to the left of your heredoc tags, in this example my heredoc tags are called HTML. Also, wrapping your php variables/functions with curly braces is optional but good practice for this method. NO PHP tags in side heredoc block.
Hope that helps.
To make a conditional statement work inside you need to use a function:
class My_Class {
public function myCondition($param) {
if($param === true) {
return '<p>True</p>';
} else {
return '<p>False</p>';
}
}
}
$object =new My_Class();
$html = <<<HTML
<div>
<h1>Conditional Statement</h1>
<div> {$object->myCondition(true)} </div>
</div>
HTML;
something like that should work. But I haven't tested it.
I am unable to understand your question properly may be this may help:
<HTML>
<div>Here some text</div>
<?php thisFunctionEchosomthingNotReturn();
if($isflag){?>
<span>DO not do this</span>
<?php }//Closing If if it ends here.
echo $whatever; ?>
</HTML>
You cannot write control structures / functions logic inside of HEREDOC syntax.
Alternate way..
<div>Here some text</div>
<?php thisFunctionEchosomthingNotReturn(); ?>
<?php if($isflag){?>
<span>DO not do this</span>
<?php echo $whatever; }?>

PHP - Placing html in variable on out of php tag

I have question, how to placing my html content like this:
<?php
$html =
?>
//in this space, i will place my html
<span></span>
<?php
;
?>
// and i print it
<?php echo $html;?>
Why not just do that between the PHP tags?
<?php
$html = '<span></span>';
echo $html;
?>
You need output buffering for this.
<?php
ob_start();
?>
<!-- your html code here -->
<span></span>
<?php
$html = ob_get_clean();
?>
From what I understand you might want to have a look at heredoc syntax. However, your question is not exactly clear.
<?php
$html = <<<EOT
<span></span>
<!-- You can place anything in here "without escaping" -->
EOT;
echo $html;
?>

How can i get the content between ?> and <?php

getThis is just a personal learning question. I want to retrieve all the content between my function $page->content_start(); and $page->content_end(); how is the best way to get it? I don't want to include it or echo it, is it possible?
Thank you very much, your advices is very appreciated
<?php
include 'pages/indexpage.class.php';
include 'modules/monmodule.class.php';
$page = new IndexPage();
$mod = new MonModule($page);
$mod->create();
$page->content_start();
?>
<div class="asss_ass"></div>
<span></span>
<?php
$page->content_end();
print_r($page->get_content());
?>
Use output buffering:
ob_start();
?>
<div class="asss_ass"></div>
<span></span>
<?php
$content = ob_get_clean();

Possible to treat html that is directly outputted as a php string

I would simply like to know if something similar to this is possible in php somehow:
<?php
$myhtmlstring = "
?>
<table>
<tr>
<td>test</td>
</tr>
</table>
<?php
";
?>
The reason for this is I would like to be able to write the html in this nice looking format but have php trim the white space after the fact.
You can use heredoc.
You can use the alternative heredoc syntax:
$myhtmlstring = <<<EOT
<table>...</table>
EOT;
Or you can use output buffering:
<?php
ob_start();
?>
<table>...</table>
<?php
$myhtmlstring = ob_get_clean();
?>
Yes
<?php
$myhtmlstring = '
<table>
<tr>
<td>test</td>
</tr>
</table>
<?php
';
// Do what you want with the HTML in a PHP variable
// Echo the HTML from the PHP variable to make the webpage
echo $myhtmlstring;
?>
I usually use the buffer functions, like so:
<?php
$whatever = "Hey man";
// This starts the buffer, so output will no longer be written.
ob_start();
?>
<html>
<head>
<title><?php echo $whatever ?></title>
</head>
<body>
<h1><?php echo $whatever ?></h1>
<p>I like this in part because you can use variables.</p>
</body>
</html>
<?php
// Here's the magic part!
$myhtmlstring = ob_get_clean();
?>
For more information about the buffer functions, look up ob_start() on
php.net.
do you mean so?
<?php
$string = '<table border="1">
<tr>
<td> test </td>
</tr>
</table>';
echo $string;
?>

Categories