Solved

PHP echo x words, insert photo and repeat

Posted on 2014-04-10
9
335 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 30

Accepted Solution

by:
Marco Gasi earned 500 total points
Comment Utility
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
Comment Utility
Awesome! Thank you
0
 
LVL 1

Author Comment

by:movieprodw
Comment Utility
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
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 1

Author Comment

by:movieprodw
Comment Utility
That did the trick!

Thank you very much!
0
 
LVL 1

Author Comment

by:movieprodw
Comment Utility
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
Comment Utility
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 30

Expert Comment

by:Marco Gasi
Comment Utility
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
Comment Utility
I am sorry, I didn't realize there were 2 of you helping!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now