Solved

PHP echo x words, insert photo and repeat

Posted on 2014-04-10
9
347 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 57

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

739 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