Solved

PHP echo x words, insert photo and repeat

Posted on 2014-04-10
9
342 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 500 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 55

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
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 …

828 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