PHP - Help Detecting Empty Lines In A Text Area
Hey guy, I really need your help with this.
I have Input from a text area, in this format: a b c d e f g 12 34 435 124 What I need do to is to output only a certain line from every block of text, every block is divided by one or more empty lines. I have done this: $input = $_POST["input"]; $lines = explode("\r",$input); $num = count($lines); $line_to_extract = $_POST['line_to_extract']; for($i=0;$i<$num;$i++) { if($i == $line_to_extract) { echo 'Line '.$i.': '.$lines[$i].'<br />'; } } What I need to do is to detect the empty line so I can keep track of the line to extract. PLEASE HELP!!! Similar TutorialsI'd like to use a text editor like this one: http://tinymce.moxiecode.com/examples/full.php for my forums. But I am not sure exactly how I would prevent abuse and injects to messed up the page, rather than being contained in the designated area it is meant for. Could some one please help me, I know htmlspecailchars will not work, since some of the code needs to render as html I’m trying to construct a button that simply writes an "aleph" character into a text area, see below.
My code does not work, can anyone tell me why ? How should I fix it ?
<!DOCTYPE html> <html> <meta charset="UTF-8"> <head> <title>Example</title> <script type="text/javascript"> //JavaScript code goes here function insertAtEnd(text) { var theArea = document.getElementById("thisArea"); theArea.value += '' + text + '';; } </script> </head> <body> <input type="button" id="aleph" name="aleph" value="Write an aleph" onClick="javascript:insertAtEnd(\'<span>א</span>\');return(false)" /> <textarea id="thisArea"> </textarea> </body> </html> I need to read all the rows from a text file between two specific lines into an array where I can process them. I then need to do the same thing again until the end of the file. The file is too large to read the entire file into an array. The number of rows that are between the key lines are variable. This is what I have so far to read the first set of rows into the array but it is only reading the first row when in fact in this case there are 15. Any pointers as to where I am going wrong please? if ($_POST['srch']=="Import") { //open file for reading $family_rec=[]; $file_handle = fopen($fn, "r"); if(!$file_handle){echo "Could not open file";} while(!feof($file_handle)) { $line = fgets($file_handle); if ((strpos($line, "0 @") !== false) && (strpos($line, "FAM") !== false)) { $famref = explode("@", $line); $line = fgets($file_handle); //echo $line."<br>"; do{ $family_rec[]=$line; $line = fgets($file_handle); //echo $line."<br>"; } while((strpos($line, "0 @") !== false) && (strpos($line, "FAM") !== false)); print_r($family_rec); }//end of family }//end of while not eof }//end of import
I have a .txt file containing hundreds of lines of Names (e.g. Brad, Jennifer, etc.) What coding can I use to only echo the first 20? Hi All, If someone types in my textarea without any hard returns, it echos the entire text on a single line, which expands my site very very far and screws up the layout. how can I echo the text but limit the number of characters per line before I insert my own line break? The code is just: Code: [Select] echo $message; I have a form that is processed using PHP (post method) and there is a textarea people will enter text into. After processing, the text is displayed to the user. However, if the user did a Carriage Return (i.e. hit Enter) when they originally entered the text, that text does NOT show on a new line when displayed later. For example, if they type this into the text area... "Hello there (they hit Enter here) How are you (they hit Enter here) Good I hope" It will display like this, all on one line: "Hello there How are you Good I hope" In other words, it doesn't seem to remember that Enter was hit. Is there some way to "preserve" the fact that someone hit the "Enter" key, so that when text is displayed later, it will show the text on a new line if the user in fact hit "Enter" when originally typing it. I am probably not using the right terms in the right spots (i.e. newline, carriage return, enter, etc) but I think you get the picture Let me know if anyone knows how to do this. Thanks! I want to add a text area to this so when an Administrator of the site clicks the refusal button, they will be able to type in a reason for the refusal. Code: [Select] <?php echo '<a href="adminDeleteGoodie.php?delete_id=' . $delete_s_find . '" onClick="return confirm(\'Are you sure you want to delete ' . $makesure_s . ' from the Goodies Approval List? If so, please leave a reason for the upload being denied. NEED A TEXT AREA RIGHT HERE \');"><img src="images_new/remove.jpg" border="0" /></a>'; ?> Extremely new here and to PHP and I have exhausted my newbie resources and my brain on this one. Concept: Cron Job to INSERT 5 lines per hour (Not the whole list at once) Script to read a pipe delimited text file (about 5k Lines) Open File Get last (bottom) 5 lines of file, ignore the rest Insert those 5 lines into my database Open File, Remove last (bottom) 5 lines I am able to do parts 1-3 but not part 4 (removing last 5 lines) Please help me! Code: [Select] $data = array_slice(file('data.txt'), -5); foreach($data as $value) { $fields=explode("|",$value); $num=count($fields); for ($n=0;$n<$num;$n++) { $null=""; print ($n==0) ? "Importing Video...<BR>" : $null; } // Insert into tmpvideos table here include("includes/config.php"); $last_id_result = @mysql_query( @"SELECT ext_id FROM tmpvideos ORDER BY ext_id+0 DESC, id DESC" ); if ( 0 < @mysql_num_rows( $last_id_result ) ) { $last_id = @mysql_result( $last_id_result, 0, "ext_id" ) + 1; } else { $last_id = "1"; } copy($fields[3], "thumbs/ex_".$last_id.".jpg" ); $fields[3] = "thumbs/ex_".$last_id.".jpg"; set_time_limit(0); mysql_query("INSERT into tmpvideos (id, ext_id, title, description, duration, category, date_added, thumb, video, views, rating, votes, status) VALUES ('','".$last_id."','$fields[0]','$fields[1]','$fields[5]','$fields[6]',NOW(),'$fields[3]','$fields[2]','','','','1')"); } I understand the 'concept' but am unable to grasp the code! I have tried many snippets and really need help. I have the following text file: Code: [Select] 09/16/2011 - 09/16/2011 323780048 FedEx Ground Shipment Detail Report 09/22/2011 CAFE2472 Page: 1 TRACKING # RECIPIENT CD ZIP ZN CARRIER SVC BILL WGT COD DECL VALUE C GR CHG C SRCHG C NET ---------------- --------------- ------ --- ------- - --- -------- ---------- ---------- ---------- ---------- ---------- 561895962528473 97913 440607 4 Ground , RH 2.0 0.00 99.00 6.22 2.45 8.27 561895962528480 97928 352424 5 Ground , RH 17.0 0.00 99.00 9.89 2.45 10.60 561895962528497 97943 857164 6 Ground , RH 17.0 0.00 99.00 12.38 2.45 14.75 561895962528503 97955 193825 5 Ground , RH 4.0 0.00 99.00 7.17 2.45 8.42 561895962528510 6792 491069 3 Ground , RH 3.0 0.00 99.00 5.97 2.45 8.27 561895962528527 97967 030703 5 Ground , RH 1.0 0.00 99.00 5.75 4.10 10.06 561895962528534 97978 665429 4 Ground , RH 4.0 0.00 99.00 6.86 4.10 10.06 561895962528541 97992 371844 4 Ground , RH 1.0 0.00 99.00 5.51 4.25 10.22 561895962528558 98005 386108 4 Ground , RH 18.0 0.00 99.00 9.21 4.25 12.01 GRAND TOTALS COURTESY NET CHARGE 92.66 TOTAL WEIGHT(OF LBS) 63.7 TOTAL WEIGHT(OF KGS) 0.0 CSY DECL VAL SCHG 0.00 COURTESY DISCOUNT 12.52 COURTESY SPECIAL FEES 28.95 CSY FUEL SURCHARGE 7.27 PACKAGE COUNT 9 And I'm attempting to pull ONLY the tracking number and reference number (or order #) from this file. I've been able to get rid of the empty lines, get rid of the first few lines I don't need and grab just tracking and reference numbers , but I can' t figure out how to get rid of of the last few lines....here's my code: Code: [Select] $arr=file("/home/onest4/public_html/beta/fedex.txt"); $x=1; foreach($arr as $str) { if (trim($str) != '') if ($x > 3) { list($tracking,$ordernumber)=explode(" ",$str); echo $tracking; echo "<br />"; echo $ordernumber; echo "<br /><br />"; } else { $x++; continue; } } Here's the result: Code: [Select] 561895962528473 97913 561895962528480 97928 561895962528497 97943 561895962528503 97955 561895962528510 6792 561895962528527 97967 561895962528534 97978 561895962528541 97992 561895962528558 98005 GRAND TOTALS COURTESY NET CHARGE TOTAL WEIGHT(OF LBS) TOTAL WEIGHT(OF KGS) CSY DECL VAL SCHG COURTESY DISCOUNT COURTESY SPECIAL FEES CSY FUEL SURCHARGE PACKAGE COUNT Anyone help with this? Thanks! Note that my code still use the old mysql query and I am aware of that. I want to migrate it to mysqli soon but it will have to wait until I learn how to do so. Meanwhile, please help me with the code that I have now. Thank you
I've looked around the internet to find how to do this and I found two methods:
htmlspecialchars();
and
mysql_real_escape_string();
I wrapped my text into one of the two function in order to pass in text with apostrophe in it to the database.
However I encountered a problem.
With mysql_real_escape_string, I got the desired effect that I want but the problem is that, the function also apply its effect onto html img tag. So <img src="test"> turns into <img src=/"test/"> and therefore images (and also links) will not appear.
With htmlspecialchars, again I got the desired function that I want which is to pass in apostrophe into the database. The problem with this is that when I pull the data out from the database and echo it onto my page, it doesn't render and show as a plain html code.
What confuse me a lot is that, it works fine inside my localhost.
Please help
Thank you!
Attached Files
content-insert.php 3.92KB
4 downloads How can I make this textarea box height larger? The width is fine but I need to make it a little taller so I don't have to scroll to see what I've typed in. Thanks Code: [Select] <tr> <td colspan="4"><b>Notes:</b> <textarea style= "width: 100%"> </textarea> </td> </tr> Hi, I've been asked to modify a section on a site for a local club. Basically, I've been asked to improve the name input section (a user may have to input up 15 names in one visit to the site). At the moment the site works like this: The user enters one members first name in one form box, then the surname in another, then presses "add name" which adds it to the list. What i want to be able to do (and i'm sure i've seen it somewhere), is have the user enter the list of names into one big text area: Type in the first name then the surname, hit enter for a new line in the text area then type the next name....etc. When the list is complete press "add names" button. Is there a name for this type of input? And does it work on the following principle: 1. The entire text sent from the text area can be exploded into an array using the line break as the delimiter 2. Each value in the array is then the first name and second name. 3. I can then explode each value further now using the space as the delimiter. 4. I am left with a bunch of arrays containing the first name in one value and the surname in the second Any help or pointers here would be class Cheers, John I want a Text Area on my form where I can enter the body of an article. It should be sticky so the input is not lost if there are form issues and the form has to be re-loaded. The problem is that when I type "mmm" into this field, after I click "Submit" my form is reloaded - because I didn't complete it - and several blank lines appear before and after the text "mmm"?! Here is my code... Code: [Select] <!-- Body --> <li> <label for="body"><span class="required">*</span>Body:</label> <textarea id="body" name="body" class="text" cols="20" rows="5" wrap="soft"> <?php if(isset($body)){echo htmlentities($body, ENT_QUOTES);} ?> </textarea> <?php if (!empty($errors['body'])){ echo '<span class="error">' . $errors['body'] . '</span>'; } ?> </li> What seems to be the problem?! Debbie I'm trying to retrieve the text from a text area and put this in a PHP variable without submitting the form. The text is provided by a database and I want to format it for the user. How can I do this with PHP? So basically: Code: [Select] <form id="createForm" name="createForm" method="post" action=""> <textarea name="subjectTextArea" id="subjectTextArea" cols="45" rows="5"> This is the text I want to put in a PHP variable before the form is submitted. </textarea> </form> <?php $txtArea = ? ?> Thanks for the help. Hi! I have a text area on my webpage which takes in user php scripts and produces an output in an adjacent window. I am trying to find out how I can auto include some php tags into the input text area so that the user doesn't have to keep typing them in every time. Code: [Select] <form name="form1" method="post" ACTION="editor.php"> <textarea rows="10" cols="60" id="code" name="code"></textarea> <span class="submitbutton">Test code</span> </form> Thanks in advance, Jay Hi, I'm not sure if this should be in PHP or HTML so apologies if it's in the wrong area. I have a form with a text area element but when the text is submitted, tags such as <br> aren't included so the outputted text is never formatted correctly such as there being no paragraphs. Is there a way make sure such tags are included in the output text? Ta. Hi, Im trying to make a script to mass update a load of links in my database but im stuck on the basics, How can i detect a new line in a text area? Anyone know why this dosen't work? Code: [Select] <?php if($_POST[newlinks]) { $newlinks = preg_split("\n", ($_POST['newlinks'])); $i = 1; foreach ($newlinks as $newlink) { echo "$i - $newlink <br />"; $i++; } } else { echo "<form method=\"POST\" action=\"changehost.php\">"; echo "<br /> <strong>Enter the new links to try and replace old ones</strong> <br />"; echo "<textarea rows=\"16\" name=\"newlinks\" cols=\"84\"></textarea>"; echo "<input type=\"submit\" value=\"Submit\" name=\"B1\">"; ?> Hi, I am trying to make a php website to create listings however I am having problems with the following code: <?php require_once("/home/a9653716/public_html/functions.php"); if(!$_SESSION['uid']){ header("Location: index.php"); } $id = mss($_GET['id']); if ($id) { $sql = "SELECT * FROM `forum_sub_cats` WHERE `id`='" . $id . "'"; $res = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($res) == 0) { echo "The forum you are trying to create a topic on, does not exist!\n"; } else { $row1 = mysql_fetch_assoc($res); if ($row1['admin'] == 1 && $admin_user_level == 0) { echo "You are not an administrator, therefore you cannot post on this forum!\n"; } else { if (!$_POST['submit']) { echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form method=\"post\" action=\"./index.php?act=create&id=".$id."\">\n"; echo "<tr><td>Forum Sub Category</td><td><select name=\"cat\">\n"; $sql2 = "SELECT * FROM `forum_cats` WHERE `admin` < " . $admin_user_level . "+1"; $res2 = mysql_query($sql2) or die(mysql_error()); while ($row = mysql_fetch_assoc($res2)) { $sql3 = "SELECT * FROM `forum_sub_cats` WHERE `cid`='" . $row['id'] . "'"; $res3 = mysql_query($sql3) or die(mysql_error()); echo "<option value=\"0\">" . $row['name'] . "</option>\n"; while ($row2 = mysql_fetch_assoc($res3)) { $selected = ($row2['id'] == $id) ? " SELECTED" : ""; echo "<option value=\"" . $row2['id'] . "\"" . $selected . "> " . $row2['name'] . "</option>\n"; } } echo "</select></td></tr>\n"; echo "<tr><td>Topic Title</td><td><input type=\"text\" name=\"title\"></td></tr>\n"; ?> <script>edToolbar('message'); </script> <?php echo "<tr><td>Message</td><td><textarea id=\"message\" name=\"message\" class=\"ed\"></textarea></td></tr>\n"; echo "<tr><td>Tags (Seperate with commas)</td><td><input type=\"text\" name=\"tags\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Create Topic\"></td></tr>\n"; echo "</form></table>\n"; } else { $cat = mss($_POST['cat']); $title = mss($_POST['title']); $msg = mss($_POST['message']); $tags = mss($POST['tags']); if ($cat && $title && $msg && $tags) { $sql = "SELECT admin FROM `forum_sub_cats` WHERE `id`='" . $cat . "'"; $res = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($res) == 0) { echo "This forum sub category does not exist!\n"; } else { $row = mysql_fetch_assoc($res); if ($row['admin'] == 1 && $admin_user_level != 1) { echo "You are not an admin therefore you cannot post a new topic on this forum!\n"; } else { if (strlen($title) < 3 || strlen($title) > 32) { echo "The title must be between 3 and 32 characters!\n"; } else { if (strlen($msg) < 3 || strlen($msg) > 10000) { echo "The message must be between 3 and 10,000 characters!\n"; } else { if (strlen($tags) < 2 || strlen($tags) > 200) { echo "You must submit at least 1 tag and the total length mustn't be more than 200 characters!\n"; } else { $date = date("m-d-y") . " at " . date("h:i:s"); $time = time(); $sql2 = "INSERT INTO `forum_topics` (`cid`,`title`,`uid`,`date`,`time`,`message`,`tags`) VALUES('" . $cat . "','" . $title . "','" . $_SESSION['uid'] . "','" . $date . "','" . $time . "','" . $msg . "','" . $tags . "')"; $res2 = mysql_query($sql2) or die(mysql_error()); $tid = mysql_insert_id(); topic_go($tid); } } } } } } else { echo "Please supply all the fields! \n"; } } } } } else { if (!$_POST['submit']) { echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form method=\"post\" action=\"./index.php?act=create\">\n"; echo "<tr><td>Forum Sub Category</td><td><select name=\"cat\">\n"; $sql2 = "SELECT * FROM `forum_cats` WHERE `admin` < " . $admin_user_level . "+1"; $res2 = mysql_query($sql2) or die(mysql_error()); while ($row = mysql_fetch_assoc($res2)) { $sql3 = "SELECT * FROM `forum_sub_cats` WHERE `cid`='" . $row['id'] . "'"; $res3 = mysql_query($sql3) or die(mysql_error()); echo "<option value=\"0\">" . $row['name'] . "</option>\n"; while ($row2 = mysql_fetch_assoc($res3)) { $selected = ($row2['id'] == $id) ? " SELECTED" : ""; echo "<option value=\"" . $row2['id'] . "\"" . $selected . "> " . $row2['name'] . "</option>\n"; } } echo "</select></td></tr>\n"; echo "<tr><td>Listing Title</td><td><input type=\"text\" name=\"title\"></td></tr>\n"; echo "<tr><td>Message</td><td><textarea id=\"markItUp\" name=\"message\" style=\"width:300px;height:100px;\"></textarea></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Create Topic\"></td></tr>\n"; echo "</form></table>\n"; } else { $cat = mss($_POST['cat']); $title = mss($_POST['title']); $msg = mss($_POST['message']); $tags = mss($POST['tags']); if ($cat && $title && $msg && $tags) { $sql = "SELECT admin FROM `forum_sub_cats` WHERE `id`='" . $cat . "'"; $res = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($res) == 0) { echo "This forum sub category does not exist!\n"; } else { $row = mysql_fetch_assoc($res); if ($row['admin'] == 1 && $admin_user_level != 1) { echo "You are not an admin therefore you cannot post a new topic on this forum!\n"; } else { if (strlen($title) < 3 || strlen($title) > 32) { echo "The title must be between 3 and 32 characters!\n"; } else { if (strlen($msg) < 3 || strlen($msg) > 10000) { echo "The message must be between 3 and 10,000 characters!\n"; } else { if (strlen($tags) < 2 || strlen($tags) > 200) { echo "You must submit at least 1 tag and no more than a total of 200 characters!\n"; } else { $date = date("m-d-y") . " at " . date("h:i:s"); $time = time(); $sql2 = "INSERT INTO `forum_topics` (`cid`,`title`,`uid`,`date`,`time`,`message`,`tags`) VALUES ('" . $cat . "','" . $title . "','" . $_SESSION['uid'] . "','" . $date . "','" . $time . "','" . $msg . "','" . $tags . "')"; $res2 = mysql_query($sql2) or die(mysql_error()); $tid = mysql_insert_id(); header("Location: index.php?act=topic&id=" . $tid . ""); } } } } } } else { echo "Please supply all the fields!\n"; } } } ?> It seems to work fine until I put the tags text box in. It works fine however when I submit something it says that I havn't supplied all the fields. I have spent quite a while trying to figure this one out but I am very new to php and understand that it's probably a missing comma or something! how to get text-area text into variable without using <form> i mean can i get through $_GET please provide me solution it needed badly if ((isset($_GET['insertinto'])) &&($_GET['insertinto']=="true")){ if(isset($_SESSION["ajaxtext"])) { unset($_SESSION['ajaxtext']); $mysession = "" ; //my textarea typed data should b here $_SESSION['ajaxtext'] = $mysession ; //insertrow() ; } } print <<<EOD <textarea rows='3' cols='40' id='ajaxtext' name ='ajaxtext'>$mysession</textarea><br> EOD; Hi all, please bare with me i am a newbie to this forum, i will try my best to provide all clear accurate information where possible, please find a below breakdown, i've had a little help from a user already but as am still getting problems and confused i will take his advise am post it in the right place ! 1) I have the below code and wish to ban specifc words, numbers and emails of my choice. 2) If the users enter a baned word etc either nothing will happen or it will forward them to another page. Please find attached index.php which is full working script of where i wish to add my banned words code too, also please find attached indextest.php containing the working script and code of bannng specfic words that i can't get to work. When i run indextest.php on my linux server the whole page displays perfectly but when you enter a baned or unbaned word and click submit nothing happens at all, so am very confused to where am going wrong. Any kind, helpfull guides to what i'm doing wrong would be highly appriciate as i do want to learn from my mistakes. All the best Steve |