473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Extracting individual lines from a Multiline text box for processing

Greetings Master of PHP /bow

I would like to preform the following task but I'm unable to figure out how, I never really got into Arrays and usually just use work arounds. I would be very greatful for your assistance.

1. I want to Copy and Paste data from my computer into a text field (Multiline)

2. When I click submit the php code should then take all of the data from the text field and seperate each line into a seperate variable / array entry.

3. I then need to enter that data into a mysql database (I know how to work with DBs just not the extract from array and enter bit)

I'm not sure if it can be done without an array? I guess you could enter the data into the database on each cycle of the extraction process?

There is no set format to the data, it will look something like this.

French
German
Spanish-Portugal
Chineese

and in my database I would like a field called Language with 1 row per language.

etc...

Thanking you in advance for your help!
Nov 2 '08 #1
1 4682
Atli
5,058 Expert 4TB
Hi.

This is actually quite simple to do once you get used to using arrays.
... Which is true of most things I guess :)

In this case, you can simply split your string into pieces using the explode function. This will create an array, each element containing one piece.

Then you could either use a foreach loop loop to insert each piece into your database.
Or, my personal preference, use the implode function to re-assemble the pieces into a VALUE clause for a single INSERT query.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // This is the string, delimited by a comma, rather than a new-line.
  3. $rawString = "piece1,piece2,piece3";
  4.  
  5. // Split the string into pieces
  6. $pieces = explode(",", $rawString);
  7.  
  8. // Build the top of the INSERT query
  9. $sql = "INSERT INTO `myTable`(`myField`) VALUES\n";
  10.  
  11. // Build the rest of the INSERT query by re-assembling the
  12. // pieces.
  13. $sql .= "('";
  14. $sql .= implode("'), ('", $pieces);
  15. $sql .= "')"; 
  16. ?>
  17.  
Now the $sql variable should be somewhat like:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO `myTable`(`myField`) VALUES
  2. ('piece1'), ('piece2'), ('piece3')
  3.  
Be careful tho if this data is coming from a client.
Make sure you run it through the mysql_real_escape_string function first, and perhaps even the trim function to.
Nov 3 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
13
by: Kosio | last post by:
Hello, I know of a way to extract digits from a number using the %10 and divide by 10. But I am wondering if there is an algorithm out there that does not use a divide by 10 feature. The...
37
by: Xiao Jianfeng | last post by:
Hi, I need to print a long sting, which is two long so it must expand two lines. I know that we can use backslash(\) to explicitly join two lines into a logical line, but this doesn't work for...
16
by: Preben Randhol | last post by:
Hi A short newbie question. I would like to extract some values from a given text file directly into python variables. Can this be done simply by either standard library or other libraries? Some...
3
by: ad | last post by:
I have set a TextBox to MultiLines and set the rows to 10. I assigned the Text of the TextBox some lins of text at desgin time, but only the first line remain in the TextBox. How can I assign...
4
by: giddy | last post by:
hi when i run this class i made here , this is what it looks like without text - http://gidsfiles.googlepages.com/LinedTextBox_1.jpg WITH TEXT (heres the issue) -...
6
by: ivan.perak | last post by:
Hello, im a beginner in VB.NET... The thing i would like to do is as it follows.... I have a text file (list of names, every name to the next line) which is about 350000 lines long. I would...
2
by: Mike | last post by:
I am trying to write a little program for my own use using VB2005 express edition. I have a list of peoples names in a file that I read into an array of strings. I am using a multiline textbox to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.