TheHaloEffect Posted September 19, 2006 Share Posted September 19, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/21277-word-wrapping-for-an-array/ Share on other sites More sharing options...
joquius Posted September 19, 2006 Share Posted September 19, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/21277-word-wrapping-for-an-array/#findComment-94653 Share on other sites More sharing options...
TheHaloEffect Posted September 19, 2006 Author Share Posted September 19, 2006 sorry, currently in the library, trying to squeeze everything in heh.What i mean isI'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 exampleI 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! Quote Link to comment https://forums.phpfreaks.com/topic/21277-word-wrapping-for-an-array/#findComment-94656 Share on other sites More sharing options...
zq29 Posted September 19, 2006 Share Posted September 19, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/21277-word-wrapping-for-an-array/#findComment-94658 Share on other sites More sharing options...
joquius Posted September 19, 2006 Share Posted September 19, 2006 Never used wordwrap() but what I'd usually do is preg_replace ("/(\w{5})/", "\\1\<br\/\>", $textrowwhatever); Quote Link to comment https://forums.phpfreaks.com/topic/21277-word-wrapping-for-an-array/#findComment-94660 Share on other sites More sharing options...
TheHaloEffect Posted September 19, 2006 Author Share Posted September 19, 2006 [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 =/ Quote Link to comment https://forums.phpfreaks.com/topic/21277-word-wrapping-for-an-array/#findComment-94749 Share on other sites More sharing options...
TheHaloEffect Posted September 19, 2006 Author Share Posted September 19, 2006 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 19Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/21277-word-wrapping-for-an-array/#findComment-94782 Share on other sites More sharing options...
zq29 Posted September 19, 2006 Share Posted September 19, 2006 [code]<?phpif(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] Quote Link to comment https://forums.phpfreaks.com/topic/21277-word-wrapping-for-an-array/#findComment-94787 Share on other sites More sharing options...
TheHaloEffect Posted September 19, 2006 Author Share Posted September 19, 2006 10 was just an example :D Quote Link to comment https://forums.phpfreaks.com/topic/21277-word-wrapping-for-an-array/#findComment-94854 Share on other sites More sharing options...
TheHaloEffect Posted September 19, 2006 Author Share Posted September 19, 2006 I get Parse error: syntax error, unexpected T_STRING in /home/hardware/public_html/random/factgen.php on line 18with that :( Quote Link to comment https://forums.phpfreaks.com/topic/21277-word-wrapping-for-an-array/#findComment-94857 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.