Jump to content

Word Wrapping For An Array?


TheHaloEffect

Recommended Posts

I've got a text file containing thousands of lines of data, which, would take years to insert <br> in the places i need it.

I try using Word Wrapping to insert the br every 20 chars or so, but it says that it expects a string not an array.  Is there anything i can use to input the <br>'s in the right places that works with arrays?

Thanks
Link to comment
https://forums.phpfreaks.com/topic/21277-word-wrapping-for-an-array/
Share on other sites

Please show the code of which you speak. If you're reading a file with a function which outputs an array of lines this could be the issue with what I am assuming was a different function.

Are you trying to parse a file? Could you be more specific as to your goal or where exactley the <br_ />'s need to go. In the mean time maybe check preg_replace() on php.net or preg_split() maybe they are what you're looking for.
sorry, currently in the library, trying to squeeze everything in heh.

What i mean is

I've got a file, containing sentance after sentances after sentace.

For example:-

The quick brown fox jumped over the lazy dog.
This old man, he plays 3, He played nick nack on my knee.
Izzy wizzy let's get busy.
I find your lack of faith disturbing.
It's fun to stay at the Y.M.C.A.
There's no reason to feel down, I said young man!

And I want a word wrap to insert a <br> after every.... 5 words for example

I tried using wordwrap() but it says it needs a string and i'm using an array to pull the sentances out of a file.

Actually using iframes to display something, but it cuts half of the sentance off, so i'm using the word wrap to shorten it, i hope!
[code]    <?php
  if (isset($_POST['fname'])){
$fname = $_POST['fname'];
$filename = "facts.php";
$filedata = file($filename);
shuffle($filedata);
echo str_replace(fname, $fname, $filedata[array_rand($filedata)]);
}
?>[/code]

That's the actual script to pull the data out of a file, and randomly display it.

It displays it in an iframe, only the text is partly cut off, so i was hoping to use word wrap to make it smaller, only it takes strings only, not arrays =/
changed it to

[code]    <?php
  if (isset($_POST['fname'])){
$fname = $_POST['fname'];
$filename = "facts.php";
$filedata = file($filename);
shuffle($filedata);
$farray = str_replace(fname, $fname, $filedata[array_rand($filedata)]);
foreach($farray as $element){
echo wordwrap($filedata, 10,$element);
}
}
?>[/code]

but now i'm getting the error: Warning: Invalid argument supplied for foreach() in /home/hardware/public_html/random/factgen.php on line 19

Any ideas?
[code]<?php
if(isset($_POST['fname'])) {
    $fname = $_POST['fname'];
    $filename = "facts.php";
    $filedata = file($filename);
    shuffle($filedata);
    //$farray = str_replace(fname, $fname, $filedata[array_rand($filedata)]); - Not sure what you're trying to do here
    foreach($filedata as $element) {
        echo wordwrap($element,10,"<br/>"); //10 characters is a little short isn't it? Or very small iFrame ;)
    }
}
?>[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.