Divide String into Parts

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Divide String into Parts

Post 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....
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Is this file format mandatory?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

\n, not /n

use preg_split()
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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.
User avatar
snowrhythm
Forum Commoner
Posts: 75
Joined: Thu May 04, 2006 1:14 pm
Location: North Bay Area, CA

Post 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?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Awsome I put some #~~~~~~~ in for the sections and now it works! Thanks!
Post Reply