PDA

View Full Version : Split. Not really understanding it.



Awesome-O 4000
April 2nd, 2008, 03:55 PM
That title is sort of deceiving, because I see how it works. What I'm attempting to accomplish with it, however, is confusing to me. Let's see if I can explain this without looking like an idiot. I'm just beginning to try to learn a bit of PHP, however, so i'm sure I will look like an idiot by the end of this post.

For a site I'm working on, the idea is to have the client be in charge of any content editing. So for instance, there is a news section, they can post/edit whatever they like via a simple text file, and it appears on the site by way of a php fread, leaving us without the pain in the *** of copywriting and such.

So say my text file looks like this:



sometext
\\\\
sometext2
\\\\
sometext3
where \\\\ is a separator. I am told I can "split" the \\\\ into an array, where 0=sometext, 1=sometext2, and so forth. I will then create a loop in order to display the text.

Does this sound correct in theory? Again, apologies if I sound retarded here. And if this is correct, could someone help me out with the PHP code? I have the fread all set up correctly I think.



<?php
$file = fopen("tester.txt","r");
fread($file,filesize("tester.txt"));
fclose($file);
?>

CharlestheFish
April 2nd, 2008, 05:41 PM
The [d-php]explode[/d-php]() function should help you out.

Awesome-O 4000
April 2nd, 2008, 06:23 PM
I'm close. I think.



<?php
$file = fopen("balls.txt","r");
$balls = fread($file,filesize("balls.txt"));
fclose($file);

$ary = split("\\\\",$balls);
echo $ary[0];
?>

simplistik
April 2nd, 2008, 07:28 PM
$ary = explode("\\\\",$balls);
print_r($ary);




preg_split(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to split(). If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.

Awesome-O 4000
April 3rd, 2008, 10:48 AM
Sweet. Thanks simplistik!

Awesome-O 4000
April 3rd, 2008, 03:28 PM
Okay. I have some text displayed the way I want it, and everything is working smoothly.



<?php
$file = fopen("balls.txt","r");
$balls = fread($file,filesize("balls.txt"));
fclose($file);

$ary = explode("####",$balls);

foreach ($ary as $value) {
?>
<div class="text"><? echo $value; ?></div>
<?
}
?>


But say the client wants to have some text displayed as a header with some copy below it? How can I achieve that? I imagine I'll need to specify a <h1> or whatever, but how do I incorporate it with the above?

Yeldarb
April 3rd, 2008, 03:38 PM
It would probably be a lot easier if you just omitted the #### separators and use the file() function that automatically loads each line in a file into an element of an array. Then the client won't have to worry about the separators.

If you just output the text like that html should work in the file. Eg <h1>Line 1</h1> will show up as an h1 element when you echo it out.

Awesome-O 4000
April 3rd, 2008, 03:45 PM
That does sound interesting. I will have to look into that.

Would it really be effective for this? I mean the text file might have a paragraph or two between each news item.

simplistik
April 3rd, 2008, 03:55 PM
You need to make your delimiter something that's not common, #### or ### is used at the bottom of many articles so it could throw your whole file off, use something like <!--break--> something that a) if parsed out as html wouldn't display and b) no client in the world would need to use it.

Awesome-O 4000
April 3rd, 2008, 03:56 PM
Oh. I didn't know that. I chose #### because I figured no one would ever need to type that. Thanks for the heads up. :)

simplistik
April 3rd, 2008, 04:00 PM
Oh. I didn't know that. I chose #### because I figured no one would ever need to type that. Thanks for the heads up. :)Yea old fogies used it back in the day to denote an end of a page.

Awesome-O 4000
April 3rd, 2008, 04:26 PM
I think I need to know what to do, but how can I get it my loop to display just one element in the array, rather than all of them?