Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP echo x words, insert photo and repeat

Posted on 2014-04-10
9
Medium Priority
?
359 Views
Last Modified: 2014-04-10
Hello,

I have the following code:
<? $num_words = str_word_count($row_cms['6']); ?>

Open in new window


I would like to make it so that it counts the words ie 623 then cuts the number in 3rds

echo 1/3
echo <img src="example.jpg">
echo 2/3
echo <img src="example.jpg">
echo 3/3
echo <img src="example.jpg">
0
Comment
Question by:movieprodw
  • 6
  • 2
9 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 39992601
See if this makes sense for you:

$str = "first second third fourth fifth sixth";
echo $str;
echo "<br>";
echo "<br>";
echo "<br>";

$words = str_word_count($str, 1);
$chunks = array_chunk($words, 2);
foreach ($chunks as $ch)
{
	foreach ($ch as $c)
	{
		echo "$c ";
	}
	echo "<br><img src='example.jpg' /><br>";
}

Open in new window


You can see the result here: http://eetest.delphicoding.com/index.php
0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 39992909
Awesome! Thank you
0
 
LVL 1

Author Comment

by:movieprodw
ID: 39992916
Actually found an issue, the output has HTML in it, <p> and &nbsp;

It is printing like this ' time p p nbsp p p There'
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 60

Expert Comment

by:Julian Hansen
ID: 39992919
Just for fun - here is an old school approach because words are different lengths - splitting on word count may not result in balanced paragraphs. This approach splits on third of string length and then tries to find the closest word break left or right of each third.
<?php

$str = "The quick brown fox jumps over the lazy dog. How now brown cow. Peter picked a peck of pickeled peppers. She sells sea shells on the sea shore. The sixth sick sheiks sixth sick sheep. Going up or";
// LENGTH OF STRING
$len = strlen($str);
// FIND THIRD OF STRING
$onethird = intval($len / 3);
$index=0;
// SEARCH LEFT AND RIGHT OF THIRD POSITION UNTIL FIRST
// SPACE IS FOUND
while ($onethird > $index && $str[$onethird - $index] != ' ' && $str[$onethird + $index] != ' ') $index++;
// SET THIRD TO POSITION OF CLOSEST WORD BREAK
// TO ONETHIRD POSITION
if ($str[$onethird+$index] == ' ') $third = $onethird + $index;
else $third = $onethird - $index;

// RESET TO DO FOR SECOND THIRD
$index=0;
// START POINT IS DOUBLE PREVIOUS
$twothird = $onethird << 1;
// SET TWOTHIRD TO POSITION OF CLOSEST
// WORD BREAK
while($str[$twothird - $index] != ' ' && $str[$twothird+$index] != ' ') $index++;
if ($str[$twothird+$index] == ' ') $twothird += $index;
else $twothird -= $index;

echo substr($str, 0, $third) . "<br/>";
echo '<img src="examples.jpg" /><br/>';
echo substr($str, $third, $twothird - $third) . "<br/>";
echo '<img src="examples.jpg" /><br/>';
echo substr($str, $twothird) . "<br/>";
echo '<img src="examples.jpg" /><br/>';
?>

Open in new window

0
 
LVL 1

Author Comment

by:movieprodw
ID: 39992935
That did the trick!

Thank you very much!
0
 
LVL 1

Author Comment

by:movieprodw
ID: 39993031
If you are still around I have a quick question.

It is breaking my line and adding a <p> tag.

Before:
<p>There already had been plans for a golf course and homes to be built at Pebble Beach but Morse decided on fewer homes and a magnificent golf course, hiring Jack Neville and Douglas Grant as designers. Neville had won five state Amateurs and Grant had just returned from six years in Scotland and England, where he had studied the latest types of bunkering and greens construction on their championship courses, important elements in building a course that has become a mecca in golf. And, unlike most other prestigious courses in America, Pebble Beach Golf Links is open to the public.</p>

Open in new window


After:
<p>There already had been plans for a golf course and homes to be built at Pebble Beach but Morse decided on fewer homes and a magnificent golf course, hiring Jack Neville and Douglas Grant as designers. Neville had won five state Amateurs and Grant had just returned from six years in Scotland<div class="img2">
<img src="/admin/uploads/pebble-beach-18th-green.jpg" width="250" /><br />
<div class="caption"><p>The 18th hole at Pebble Beach during the AT&amp;T Pebble Beach Pro-Am.</p></div>
</div>
 and England, where he had studied the latest types of bunkering and greens construction on their championship courses, important elements in building a course that has become a mecca in golf. And, unlike most other prestigious courses in America, Pebble Beach Golf Links is open to the public.</p>

Open in new window

0
 
LVL 1

Author Comment

by:movieprodw
ID: 39993036
Oh guess it is not adding a P tag, but it is breaking the line.

Can you not insert a float div in the middle of a sentence? Thought you could and it would not break the line
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39993387
I'm sorry, but I was away.
Let me say I really don't understand why julian code solve a problem which refers to html tags within the string: personally, I think you first used htmlspecialchars to ouput the string and then you have dropped the function.

About the breakline, just remove the <br> which both me and Julian have inserted in our code samples

me: echo "<br><img src='example.jpg' /><br>";

Julian: echo substr($str, 0, $third) . "<br/>";
echo '<img src="examples.jpg" /><br/>';

Hope this works
0
 
LVL 1

Author Comment

by:movieprodw
ID: 39993407
I am sorry, I didn't realize there were 2 of you helping!
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question