Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 6th, 2004, 01:53 AM
Friend of Wrox
 
Join Date: Mar 2004
Location: Ottawa, , Canada.
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default pick text apart

Hi,

I would like to know how to do this, I have this big text stored in a db, but when I get the info back, I wan't to be able to spread it out, and make it look nicer by section, I need to enter a special char, like '\n' or something, that would identify the end of the section, and allow me to display just that part, then move along to the next one... does anyone have any ideas how to do it

Reply With Quote
  #2 (permalink)  
Old March 6th, 2004, 06:01 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Are you asking how to get line breaks in the browser?

Use

<pre style='white-space: pre; display: block;'>
text with \n (newline) line breaks.
</pre>

Or if a server side solution seems more fitting...

You can insert an HTML line break '<br />' at every \n newline character in the text using the nl2br() function.
http://www.php.net/nl2br

If you want fancier formatting then you can use regular expressions to insert what you want, where you want.

Step 1. use trim() function to get rid of leading and trailing whitespace.
$bigtext = trim($bigtext);

Step 2. remove extra \n characters (so that only one separates each block)
$bigtext = preg_replace('/[\\n]{2,}/', '\n', $bigtext);

Step 3. Insert formatting at \n characters.
$bigtext = preg_replace('/[\\n]/', $formatting, $bigtext);

Step 4. Append/Prepend formatting for beginning and end.
$bigtext = $preformatting.$bigtext.$postformatting;

Obviously I haven't tested these reg. expressions but they ought to work.
If you want to see more on these go to
http://www.php.net/pcre

hth,
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #3 (permalink)  
Old March 6th, 2004, 09:32 PM
Friend of Wrox
 
Join Date: Mar 2004
Location: Ottawa, , Canada.
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, nothing that simple...

What is going is on, is that I have a series of instructions which is stored in a 1 entry of a db, now when I pull that information back, it's 1 blob of text... What I want to know how to do, is to take said text, and break it apart, and format each section, like add 'step 1', and edit/delete button, 'step 1', etc...

That's what I'm trying to figure out.

Reply With Quote
  #4 (permalink)  
Old March 6th, 2004, 09:42 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

I think it would help us if you showed us a few small snips of what this blob looks like, how you intend to break it apart. Are you wanting to explode the text on a certain character so each fragment can be formatted differently? I need more info about what you're doing.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #5 (permalink)  
Old March 7th, 2004, 12:15 AM
Friend of Wrox
 
Join Date: Mar 2004
Location: Ottawa, , Canada.
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That is exactly what I'm trying to do, meaning to fragment the text allowing me to format individually...

What I'm trying to do is a repertoire of recipes... So when it comes down to writting in the instructions.. I want to be able to add each alone, but in the end it's part of the same text...

Now when it comes back from the db, I need to split up each of the directions, and display it up on the screen as such, and not have just 1 blob.

I'm sorry, I don't know how else to present it...

Reply With Quote
  #6 (permalink)  
Old March 7th, 2004, 02:33 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Ok that's a little more information -- but I still don't have a very good picture of what you need.

It doesn't sound like the newline character is going to be the separator. I do have one question, out of curiousity, why do you want to store each in the same blob? I apologize for my confusion!

For instance if you have this recipe...

HUMMUS

1 #10 CAN CARBONZO BEANS - DRAIN 1/2 CAN
15 CLOVES FRESH GARLIC
2 TEASPOON SALT
8 TABLESPOON REAL LEMON JUICE
2 CUPS TAHINI
2 TABLESPOON CHOPPED PARSLEY

BLEND IN FOOD PROCESSOR

Obviously there's several newline characters in there. What I need to know to be of help is -- are you working with data already stored or are you trying to come up with a scheme to store them. If you are working with recipes that are already stored, what are you using to separate each recipe?

If you were using XML you could use the PHP DOM XML extension to parse the XML.. or a PEAR class, the latter being a better solution because you're relying on already built software to do all the work.

For instance, if you were to do your recipes in XML, you'd have an XML document that looks something like the following.

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<recipe>
    <title>Hummus</title>
    <ingredients>
        <ingredient>
            <quantity>1</quantity>
            <measurement>#10 can</measurement>
            <instructions>DRAIN 1/2 CAN</instructions>
            <product>CARBONZO BEANS</product>
        </ingredient>
        <ingredient>
            <quantity>1</quantity>
            <measurement>CLOVES</measurement>
            <product>FRESH GARLIC</product>
        </ingredient>
        <ingredient>
            <quantity>2</quantity>
            <measurement>TEASPOON</measurement>
            <product>SALT</product>
        </ingredient>
        <ingredient>
            <quantity>8</quantity>
            <measurement>TABLESPOON</measurement>
            <product>REAL LEMON JUICE</product>
        </ingredient>
        <ingredient>
            <quantity>2</quantity>
            <measurement>CUPS</measurement>
            <product>TAHINI</product>
        </ingredient>
        <ingredient>
            <quantity>2</quantity>
            <measurement>TABLESPOONS</measurement>
            <instructions>CHOPPED</instructions>
            <product>PARSLEY</product>
        </ingredient>
    </ingredients>
    <directions>
        BLEND IN FOOD PROCESSOR 
    </directions>
</recipe>
Which could be quite versatile bringing you the ability to calculate an estimated food cost, serving sizes, etc. And give you the ability to distinguish one from another.

Something like that would be beyond the scope of p2p though -- it would involve a somewhat complicated set-up.

Something simpler would be to have dashed lines (of the same length) separate each recipe, then you could call on a function like explode() to separate each recipe into an array. Or a slightly more complicated route -- just come up with a database schema for each component of a recipe and store each component separately.

If you are working with data that's already there.. if you could post a snip of the blob, just enough for us to get an idea of what you're working with we'd be more inclined to be able to offer a solution.

(That's good stuff by the way!)

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
Reply With Quote
  #7 (permalink)  
Old March 7th, 2004, 12:13 PM
Friend of Wrox
 
Join Date: Mar 2004
Location: Ottawa, , Canada.
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well the new line char was just an idea... I just need to add a char that would not be normally typed in, so that I can make a search off of it, and be able to split it up.

Now the reason that it's all 1 entry, is that each recipe doesn't have the same amout of instructions, so it's not really feasable to have many entries in the database like instruc_1, instruc_2, etc...

I'm working on the drawing bored, right now, I have all these recipes on peices of paper, and I thought it would be nice to have it in a db, and using a web page as the tool to enter the data itself.

That xml bit looks nice, but I have no clue as to what you entered there? Haven't touched xml that much...

Reply With Quote
  #8 (permalink)  
Old March 12th, 2004, 04:15 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Or you can use a regular expression search n' split. Honestly, the easiest thing for you to do is to show us a few examples of the actual text you'd like to split up, and where you'd like it split.

Rich's XML example is pretty straightforward. All you really need to know about XML is that it's a tag-based language (tags being "stuff in brackets", just like HTML) that allows you to store data in a heirarchical way. It's like a text-only version of a database, in that YOU are responsible for both creating the XML schema and the content. That is to say, you are responsible for deciding what tag names you want to use, and how you're going to organize them. It's similar to how you are responsible for creating the names and data types of columns in a database table.

For example, if I wanted to store p2p poster information, I might have an XML file that looks like:

<posters>
  <poster>
    <name>nikolai</name>
    <url>http://www.bigaction.org/</url>
  </poster>
  <poster>
    <name>quesadilla5</name>
    <url>http://www.smilingsouls.net/</url>
    <location>Greencastle, IN</location>
  </poster>
</posters>


For more information, I strongly suggest using W3 schools online tutorials to learn all you ever wanted to know about XML, XSLT, XPath, HTML, CSS, etc...

  http://www.w3schools.com/

Take care,

Nik
http://www.bigaction.org/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does anyone know a way i can pick out only numbers crmpicco Perl 2 March 15th, 2007 08:57 AM
Need to pick up a declaration from a node ramesh.kumarm XSLT 3 March 8th, 2006 12:40 AM
pick out the 11th occurance crmpicco Classic ASP Basics 4 February 11th, 2005 04:00 AM
pick file from unix vakil Biztalk 1 July 9th, 2003 04:11 AM



All times are GMT -4. The time now is 10:26 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.