Dynadot โ€” .com Registration $8.99

Php parse help

Spaceship Spaceship
Watch

niteowl660

Established Member
Impact
2
php text file parse help

I need a little php parse help. I wrote a program once that would read through a text file of the bible and parse it into MySql, I think using the ereg function. Thats been a few years ago and for some reason I can't wrap my head around it anymore?
I've got an example of the text files below. Inbetween the <--Page Break --> tags is a recipe, I need to read the first line as the Title and the rest as the content of the recipe.
Anyone feel like giving me a little code snippet :)
Code:
<-----------Page_Break----------->
16 BEAN SOUP

1 package 16 Bean Soup
3 bay leaves
1 tablespoon crushed oregano
2 cans no-fat chicken stock
Additional water to cover
3 stalks celery chopped
3 carrots diced
1 large onion chopped
3 cloves garlic sliced
1 pound turkey Italian sausage sliced
2 cans stewed (or diced) tomatoes
Combine first 5 ingredients (liquid should cover mixture by 1"-2") in
Crock Pot Cook on high for 2
hours Add remaining ingredients and shift cooker to low and cook for
additional 3 hours For more
zing, add cayenne or crushed red pepper when adding second set of
ingredients. Serve as
complete meal or over rice. Freezes well.

<-----------Page_Break----------->
ALL DAY CROCKPOT DELIGHT

2-3 lbs. boneless chuck, cut into 1 inch cubes
1/2 c. flour
1/4 c. butter
1 onion, sliced
1 tsp. salt
1/8 tsp. pepper
1 clove garlic, minced
2 c. beer
1/4 c. flour
Coat beef cubes with the 1/2 cup flour. Brown in melted butter. Drain
off excess fat. In crock pot, combine browned meat with onion, salt,
pepper, garlic and beer.
Cover and cook on low 5-7 hours (all day) until meat is tender. Turn
control to high. Dissolve remaining 1/4 cup flour in small amount of
water. Stir into meat mixture, cook on high 30-40 minutes. Serve with
rice and salad.

<-----------Page_Break----------->
ALL DAY MACARONI AND CHEESE

8 ounces elbow macaroni, cooked and drained
4 cups(16 ounces) shredded sharp Chedder Cheese
1 can (12 ounces) evaporated milk
1 1/2 cups milk
2 eggs
1 teaspoon salt
1/2 teaspoon black pepper
Place the cooked macaroni in crockpot that has been sprayed with
nonstick cooking spray. Add the remaining ingredents, all except 1 cup
of the cheese, mix well.
Sprinkle with the remaining 1 cup of cheese and then cover and cook
on low setting for 5 to 6 hours or until the mixture is firm and golden
around the edges. Do not remove the cover or stir until it has finished
cooking.

Can I use explode with <---Page_break---> as the delimiter. If so how would you do this ,get the array count etc..
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
This seems to do what you want:

PHP:
$test = <<<EOF
<-----------Page_Break----------->
16 BEAN SOUP

1 package 16 Bean Soup
3 bay leaves
1 tablespoon crushed oregano
2 cans no-fat chicken stock
Additional water to cover
3 stalks celery chopped
3 carrots diced
1 large onion chopped
3 cloves garlic sliced
1 pound turkey Italian sausage sliced
2 cans stewed (or diced) tomatoes
Combine first 5 ingredients (liquid should cover mixture by 1"-2") in
Crock Pot Cook on high for 2
hours Add remaining ingredients and shift cooker to low and cook for
additional 3 hours For more
zing, add cayenne or crushed red pepper when adding second set of
ingredients. Serve as
complete meal or over rice. Freezes well.

<-----------Page_Break----------->
ALL DAY CROCKPOT DELIGHT

2-3 lbs. boneless chuck, cut into 1 inch cubes
1/2 c. flour
1/4 c. butter
1 onion, sliced
1 tsp. salt
1/8 tsp. pepper
1 clove garlic, minced
2 c. beer
1/4 c. flour
Coat beef cubes with the 1/2 cup flour. Brown in melted butter. Drain
off excess fat. In crock pot, combine browned meat with onion, salt,
pepper, garlic and beer.
Cover and cook on low 5-7 hours (all day) until meat is tender. Turn
control to high. Dissolve remaining 1/4 cup flour in small amount of
water. Stir into meat mixture, cook on high 30-40 minutes. Serve with
rice and salad.

<-----------Page_Break----------->
ALL DAY MACARONI AND CHEESE

8 ounces elbow macaroni, cooked and drained
4 cups(16 ounces) shredded sharp Chedder Cheese
1 can (12 ounces) evaporated milk
1 1/2 cups milk
2 eggs
1 teaspoon salt
1/2 teaspoon black pepper
Place the cooked macaroni in crockpot that has been sprayed with
nonstick cooking spray. Add the remaining ingredents, all except 1 cup
of the cheese, mix well.
Sprinkle with the remaining 1 cup of cheese and then cover and cook
on low setting for 5 to 6 hours or until the mixture is firm and golden
around the edges. Do not remove the cover or stir until it has finished
cooking.
EOF;

$test_array = explode('<-----------Page_Break----------->',$test );

foreach( $test_array as $i => $block )
{
	if( $block == "" ) continue;
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//  TRIM WHITESPACES
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	$block = trim($block);
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//  MATCH TITLE
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	if( preg_match('/.*\\r|\\n/i', $block, $regs) )
	{
	  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	  //  ASSIGN TITLE
	  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	  $recipe[$i]['title'] = trim($regs[0]);
	  
	  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	  //  REMOVE TITLE FROM BLOCK
	  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	  $block = preg_replace( "/" . preg_quote(trim($regs[0]), '/') . "/i" , '' , $block );
	  
	  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	  //  GRAB REMAINING RECIPE AND TRIM IT UP
	  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	  $recipe[$i]['directions'] = trim($block);
	}	
}

produces array:

Code:
Array
(
    [1] => Array
        (
            [title] => 16 BEAN SOUP
            [directions] => 1 package 
3 bay leaves
1 tablespoon crushed oregano
2 cans no-fat chicken stock
Additional water to cover
3 stalks celery chopped
3 carrots diced
1 large onion chopped
3 cloves garlic sliced
1 pound turkey Italian sausage sliced
2 cans stewed (or diced) tomatoes
Combine first 5 ingredients (liquid should cover mixture by 1"-2") in
Crock Pot Cook on high for 2
hours Add remaining ingredients and shift cooker to low and cook for
additional 3 hours For more
zing, add cayenne or crushed red pepper when adding second set of
ingredients. Serve as
complete meal or over rice. Freezes well.
        )

    [2] => Array
        (
            [title] => ALL DAY CROCKPOT DELIGHT
            [directions] => 2-3 lbs. boneless chuck, cut into 1 inch cubes
1/2 c. flour
1/4 c. butter
1 onion, sliced
1 tsp. salt
1/8 tsp. pepper
1 clove garlic, minced
2 c. beer
1/4 c. flour
Coat beef cubes with the 1/2 cup flour. Brown in melted butter. Drain
off excess fat. In crock pot, combine browned meat with onion, salt,
pepper, garlic and beer.
Cover and cook on low 5-7 hours (all day) until meat is tender. Turn
control to high. Dissolve remaining 1/4 cup flour in small amount of
water. Stir into meat mixture, cook on high 30-40 minutes. Serve with
rice and salad.
        )

    [3] => Array
        (
            [title] => ALL DAY MACARONI AND CHEESE
            [directions] => 8 ounces elbow macaroni, cooked and drained
4 cups(16 ounces) shredded sharp Chedder Cheese
1 can (12 ounces) evaporated milk
1 1/2 cups milk
2 eggs
1 teaspoon salt
1/2 teaspoon black pepper
Place the cooked macaroni in crockpot that has been sprayed with
nonstick cooking spray. Add the remaining ingredents, all except 1 cup
of the cheese, mix well.
Sprinkle with the remaining 1 cup of cheese and then cover and cook
on low setting for 5 to 6 hours or until the mixture is firm and golden
around the edges. Do not remove the cover or stir until it has finished
cooking.
        )

)
 
1
•••
Thanks. I'll try that. I put together a rough script today but yours looks alot cleaner and straight to the point
 
0
•••
Nice script I will also use this...
Thanks
 
0
•••
Dynadot โ€” .com Registration $8.99Dynadot โ€” .com Registration $8.99
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back