Solved

# Add \$x in to \$v every 10 charature. (\$x changes every time)

Posted on 2006-04-30
183 Views
Add \$x in to \$v every 10 charature. (\$x changes every time)
0
Question by:mnb93

LVL 17

Expert Comment

Hi mnb93,

I don't understand what you want.

Joe P
0

LVL 5

Author Comment

dw I worked out what I needed.
0

LVL 5

Author Comment

\$d = '';
for(\$i=0;\$i<strlen(\$x);\$i++)
{
if(\$i%10 == 0 AND \$i !== 0)
{
\$d .= \$s.substr(\$x, \$i, 1);
\$s = mkNews();
}
else
{
//do nothing
\$d .= substr(\$x, \$i, 1);
}
}

Points will go to anyone who can make something faster than that.
0

LVL 17

Expert Comment

You could do:
if (strlen != 0)
{
for(\$i=1;\$i<strlen(\$x);\$i++)
{
if(\$i%10 == 0)
{
\$d .= \$s.substr(\$x, \$i, 1);
\$s = mkNews();
}
else
{
//do nothing
\$d .= substr(\$x, \$i, 1);
}
}
}

This, although it looks larger eliminiates the check if i = 0 every single time.  It starts at 1 checking that the strlen() is in fact greater then 0.
Also let me write another one.
0

LVL 17

Expert Comment

\$length = strlen(\$x);
\$pos = 0;
while(\$length > 10)
{
\$d .= substr(\$x, \$pos, 9);
\$d .= \$s.substr(\$x, \$i, 1);
\$s = mkNews();
\$pos += 10;
\$length -= 10;
}
\$d  .= substr(\$x, \$pos);

This versino actually goes 10 letters at a time.  Works for \$x string of any size it takes the 10 letters like so:
Call mkNews()

Until there are less then 10 letters left to add, just add them onto the end.
Much faster cause your not looping letter by letter, your taking them 10 at a time.

Joe P
0

LVL 17

Accepted Solution

Correction: while loop should be: while (\$length >= 10)

Also if you wanted you could combine three lines into one with a for loop.
\$length = strlen(\$x);
while (\$length >= 10)
\$length -= 10;

All into:
for (\$length = strlen(\$x); \$length >= 10; \$length -= 10)

Joe P
0

LVL 49

Assisted Solution

\$d =  preg_replace('/(.{10})/e', '"\0".mkNews()', \$x);

-r-
0

## Featured Post

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
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…