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.
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
Related
I get a lot of DMCA removal emails for my website, and I'm trying to automate the process of removing those tracks from my website.
The emails all come looking similar to this.
http://example.com/title-to-post.html title - to post
http://example.com/title-of-post.html title - of post
http://example.com/some-song-artist-some-song-name.html some song artist - some song name
But there's a lot of them, I only wanna return the URL portion of every part of this, example being below.
http://example.com/title-to-post.html
http://example.com/title-of-post.html
http://example.com/some-song-artist-some-song-name.html
EDIT: I am storing these files into a txt file, then calling them using standard code.
$urls = file_get_contents( "oururls.txt" );
$data = explode(",", $urls);
foreach ($data as $item) {
echo "$item";
echo '<br>';
}
Nothing really fancy going on, how ever it's returning the title also and I want just the urls.
If there's always a space after the URL you can explode the text by " " and get the first portion. Example:
$example = explode(" ", $url);
echo $example[0];
I know this question has been asked before but the answers are not the solution to my problem.
When i post a text with this code:
$info=html_entity_decode(mysql_real_escape_string($_POST['info']));
Like :
fdsa
fdsa
fasf
and when i posted this text with antered and spaces it looks like this
<?php echo $info;?>
fdsa\r\nfdsa\r\nfasf\r\n
i try this nl2br($info) but still not working.
What do I need to appear in the text in this way?
fdsa
fdsa
fasf
Replace the \r\n by <br>, the \n by <br>, then the\r by <br>:
$info = html_entity_decode($_POST['info']);
$info = str_replace('\r\n' , '<br>', $info);
$info = str_replace('\n' , '<br>', $info);
$info = str_replace('\r' , '<br>', $info);
$info = html_entities($info);
echo $info;
You have to make multiples replacements since new lines can be represented differently according to the operating system (See this page for more details)
Finally, sanitize the value with html_entities before echoing it, preventing client side attacks.
EDIT : Removed the mysql_... function, not needed (the value isn't intended to be inserted in a MySQL database, not now at least).
Also, read Lashus advice bellow and apply it ;)
i get different string (like a, b, hospital, schools, jobs to work etc. ) as $divname from my db in my PHP section. i use this values as id in mydiv elemnents.
<div id="'.$divname.'"></div>
there is no problem for $divname = a, b, hospital or school but when it comes to jobs to work there is a huge problem cause my id gets spaces and it returns an error to me.
<div id="jobs to work"></div> //as evryone state spaces in id variable is an error.
now my question i need use this $divame variables in my id attribute. how can i do this? how can i delete those spaces or any more idea for using those in id attributes are welcome.
You may do two things:
Use a hashing function to create new id, which will be unqiue as long as your ids are unique as:
$newdivid = md5($olddivid);
You may write a function to remove spaces and combine such string items
function remove_spaces($str) {
$str_arr = explode(' ', $str);
$newstr = "";
foreach($str_arr as $sitem){
$newstr .= trim($sitem);
}
return $newstr;
}
Hope this solves your problem.
i found a php command in another Sof question for this purpose
$new_divname = str_replace(' ', '', $divname);
it deletes all spaces.. and of course my question gets a dublicate:
this is a code to catch all url in the POST and shorting them then insert each of them in row in mysql ..... but here it is inserting all the url in one row ??
so how can i make it catch the first url then insert it in database then go back for the second one and do the same..???
$urlinput=mysql_real_escape_string($_POST['url']);
$pattren="/(http:\/\/)[a-zA-Z0-9]*\.[a-z]*(.*)|(www)\.[a-zA-Z0-9]*\.[com]*(.*)/";
preg_match_all( $pattren, $urlinput, $matches );
foreach($matches[0] as $match) {
$id=rand(10000,99999);
$shorturl=base_convert($id,20,36);
$sql = "insert into url values('$id','$match','$shorturl')";
mysql_query($sql,$con);
}
Here http://php.net/manual/en/function.preg-match-all.php you can read about the 4th parameter of preg_match_all. You can loop over the urls found. I changed the end of your regular expression, so it won't catch the whole line:
$urlinput=mysql_real_escape_string($_POST['url']);
$pattren="/(http:\/\/)[a-zA-Z0-9]*\.[a-z]*(.*)|(www)\.[a-zA-Z0-9]*\.[com]*([a-zA-Z0-9\.\-_\/\?=\:]*)/";
preg_match_all( $pattren, $urlinput, $matches, PREG_SET_ORDER );
foreach($matches as $match) {
$id=rand(10000,99999);
$shorturl=base_convert($id,20,36);
$sql = "insert into url values('$id','" . mysql_real_escape_string($match[0]) . "','$shorturl')";
mysql_query($sql,$con);
}
Also be careful with SQL injection, and use mysql_real_escape_string when you use user data in your queries.
well, "(.*)" in your regex matches everything after it has found the beginning of a url. so if you're expecting multiple urls in one string, or coma separated or what not, they will all be bundled together.
You need to be able to split your input first, and then validate that each element is a valid url before you pick it up for insertion.
I'm trying to do something similar to the way tags are added to questions on S/O using php/jquery.
$tagPart = $_POST['tagPart'];
$tagPart = strip_tags($tagPart,"");
$tagPart = trim($tagPart);
$tag_array = explode(',',$tagPart);
$lastTag = end($tag_array);
$part_query = "SELECT title FROM tag WHERE title LIKE '$lastTag%'";
$part_result = mysql_query($part_query) or die($lastTag);
while ($row = mysql_fetch_assoc($part_result)){
echo "<div id ='link' onclick = 'addText(\"".$row['title']."\");'>" . $row['title'] ."</div>";
}
This works well for adding the first tag..but doesn't show any results for the second tag after a comma is added...
so if i did PHP, JQuery for example...i would be able to add PHP but upon adding the , and the second tag the SQL query doesn't result in any results...i assume its probably matching the entire string and not just the last tag?
Try outputting your sql statement and check it for errors, extra spaces, etc.
$part_query = "SELECT title FROM tag WHERE title LIKE '$lastTag%'";
echo "sql statement: ".$part_query;
You trim $tagPart but maybe you need to trim $lastTag as well:
$lastTag = trim(end($tag_array));
Does $lastTag have a space in it? For example:
$tagPart = 'One, Two, Three';
$tag_array = explode(','$tagPart);
$lastTag = end($tag_array); //$lastTag = ' Three' -> notice the space!
I'm not a MySQL expert, so I'm not sure whether "LIKE" would automatically trim this off, but you may want to do $lastTag = trim(end($tag_array)). The fact that the first tag works would make sense if the space is the issue, because the first tag in the list doesn't have a preceding space.