Solved

PHP echo x words, insert photo and repeat

Posted on 2014-04-10
9
350 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

695 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