开发者

Split multiple lines and input them in separate row in a database

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-linkf开发者_Python百科ield']; 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.


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));


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜