Page 1 of 1

Divide String into Parts

Posted: Wed Nov 22, 2006 7:12 am
by tecktalkcm0391
I have a text file with this in it:
# RUle 1


# Rule 2


# ---
# Start Codes
# ---



# ---
# End Codes
# ---
I want to make PHP divide the text into an array where everything before:
# ---
# Start Codes
# ---
goes into $part[0]
# ---
# Start Codes
# ---
goes into $part[1]; the stuff between the
# ---
# Start Codes
# ---

and

# ---
# End Codes
# ---
goes to $part[2]; and the
# ---
# End Codes
# ---
goes to $part[3]; and anything after the

# ---
# End Codes
# ---
Goes to $part[4].


I've got this far:

Code: Select all

<?php
$myFile = "thefile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);


$start = split("# --- /n
# Start Codes/n
# ---", $theData, 1);
$part = split("# ---
# End Codes
# ---", $start);


?>
But nothing is happening all I get is an array[0] not anyother parts....

Posted: Wed Nov 22, 2006 7:43 am
by volka
Is this file format mandatory?

Posted: Wed Nov 22, 2006 9:17 am
by Jenk
\n, not /n

use preg_split()

Posted: Wed Nov 22, 2006 9:58 am
by tecktalkcm0391
I really just want to make it so that I can get the section which I'll have codes PHP needs to edit.

Posted: Wed Nov 22, 2006 10:53 am
by snowrhythm
Why don't you make the split a little simpler by using something like a "~" wherever you want your sections to be split?

Posted: Wed Nov 22, 2006 2:43 pm
by tecktalkcm0391
Awsome I put some #~~~~~~~ in for the sections and now it works! Thanks!