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
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.
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

[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 =/
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.