Jump to content


Photo

Word Wrapping For An Array?


  • Please log in to reply
9 replies to this topic

#1 TheHaloEffect

TheHaloEffect
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 19 September 2006 - 02:07 PM

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

#2 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 19 September 2006 - 02:22 PM

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.
z..z..z..z..z..z..z..z..

#3 TheHaloEffect

TheHaloEffect
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 19 September 2006 - 02:30 PM

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!

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 19 September 2006 - 02:35 PM

What function are you using to pull the text out of the file? Could you post up the relevent piece of code for us to review and guide you please.

#5 joquius

joquius
  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 19 September 2006 - 02:37 PM

Never used wordwrap() but what I'd usually do is preg_replace ("/(\w{5})/", "\\1\<br\/\>", $textrowwhatever);
z..z..z..z..z..z..z..z..

#6 TheHaloEffect

TheHaloEffect
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 19 September 2006 - 04:48 PM

    <?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)]);
}
?>

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 =/

#7 TheHaloEffect

TheHaloEffect
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 19 September 2006 - 05:23 PM

changed it to

    <?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);
}
}
?>

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?

#8 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 19 September 2006 - 05:32 PM

<?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 ;)
    }
}
?>


#9 TheHaloEffect

TheHaloEffect
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 19 September 2006 - 07:04 PM

10 was just an example :D

#10 TheHaloEffect

TheHaloEffect
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 19 September 2006 - 07:10 PM

I get Parse error: syntax error, unexpected T_STRING in /home/hardware/public_html/random/factgen.php on line 18

with that :(




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users