views:

45

answers:

3

Hi Guys,

I am trying to create a php script that inputs the HTTP links pasted into a textarea into a separated row in a database. More exactly:

First page is where the textarea (name=textarea-linkfield) is, links are going to be pasted here

http://www.stackoverflow.com
http://www.yahoo.com
....
http://google.com

The links are being carried over into the php script with $linkfield=$_POST['textarea-linkfield']; and I would like the links to be inserted into the database, each http link per row. Database name: site, table name plow, columns: id, url, ...

L.E. I tried just as proof of concept:

$linkfield=$_POST['textarea-linkfield'];
$strip=explode("\n",$linkfield);
echo $strip[1];

but I get 500 INTERNAL SERVER ERROR

L.E.2

The answer:

// Split the string into pieces
$pieces = explode("\n", str_replace(array("\n", "\r\n"), "\n", trim($linkfield)));

// Build the top of the INSERT query
$sql = "INSERT INTO `plow`(`url`) VALUES\n";

// Build the rest of the ;INSERT query by re-assembling the
// pieces.
$sql .= "('";
$sql .= implode("'), ('", $pieces);
$sql .= "')"; 
mysql_query($sql) or die ('Error: ' . mysql_error());

mysql_close();

Thanks to all for their help. Chris.

A: 

use preg_match to find each URL and add it to the database. Example 3 on this page should do the trick: http://php.net/manual/en/function.preg-match.php this way you can enter URLs without having to use a new line. if you're only using URLs, then you could also add a delimeter after each URL i.e. a comma (which isnt used in a URL) to explode them with using the explode() idea in the comment.

InnateDev
I would like to come up with a script that can work without me adding commas or any other delimiter and the links need to be inputted into the textarea into a new line. Thanks, Chris.
Chris19
A: 

Depending on your os a newline can be "\n", "\r\n" or "\r".

Give this a shot:

$strip=explode("<br>", nl2br($linkfield));

or maybe safer:

$strip=explode("\n", str_replace(array("\n", "\r\n"), "\n", $linkfield));
PMV
Linux environment and is working with the second line of code, now I need to come up with the code to insert it into the database. Thanks PMV.
Chris19
foreach ($strip as $website) { mysql_query("INSERT INTO table (url) VALUES ('$website')");}
PMV
Thanks PMV. Works like a charm.
Chris19
A: 

Here is your question, answered: http://bytes.com/topic/php/answers/850719-extracting-individual-lines-multiline-text-box-processing

Ted
Thanks Ted, indeed that was the answer. I pasted the answer at the bottom of the question for others to use. Thanks all for your help. Good night, Chris.
Chris19