PHP - Separating Html Tags In Php
Hi there,
I'm new to the board, so apologise if something like this has been asked before. I'm also quite new to php, so the disaster that is my code will probably make your stomach turn, but it works so far... I am using file_get_contents to grab a website I have access to (my work rota, incidentally), and parse a bit of the html. The aim is to be able to take my rota and do a few things with the times and dates, etc.. So far, I have got the file, parsed the crap out of the string that I don't need, and i'm left with something that looks like this: Code: [Select] <TR><TD ></TD></TR><TR bgcolor='ffff00'><TD>Date</TD><TD>Duty</TD><TD>Dep</TD><TD>Begin</TD><TD>End</TD><TD>Arr</TD></TR><TR><TD ></TD></TR><TR><TD>23 Apr 12, Mon</TD><TD>297</TD><TD>STN</TD><TD>15:55</TD><TD>17:10</TD><TD>DUB</TD></TR><TR><TD>23 Apr 12, Mon</TD><TD>288</TD><TD>DUB</TD><TD>17:35</TD><TD>18:50</TD><TD>STN</TD></TR><TR><TD>23 Apr 12, Mon</TD><TD>293</TD><TD>STN</TD><TD>19:15</TD><TD>20:30</TD><TD>DUB</TD></TR><TR><TD>23 Apr 12, Mon</TD><TD>298</TD><TD>DUB</TD><TD>20:55</TD><TD>22:05</TD><TD>STN</TD></TR><TR><TD ></TD></TR> The people who wrote my rota weren't very tidy, but in a nutshell, I want the information between the TD/TR brackets. Each Row contains cells with my date, time, destination etc in it. I can use striptags, but I end up with a long string that is difficult to split into useful information. I need something that parses the string like this.. "for every row grab information between <td> and </td> and load into $td[0]" etc.." How would I go about doing something like this? I'm a bit stumped. Thanks in advance Horgy Similar TutorialsI'm trying to separate the HTML(form) into a separate .html file (from the php file below). Someone suggested to "place the HTML in someFile.html and the PHP in someFile.php, and alter the <form> tag's 'action' element in the .html file to target someFile.php". But I don't know how to alter the <form> tag's 'action' element in the .html file to target this upload_file.php file. Any additional help will be appreciated. <?php session_start(); require_once 'phps3integration_lib.php'; $message = ""; if (@$_POST['submit'] != "") { $allowed_ext = array("gif", "jpeg", "jpg", "png", "pdf", "doc", "docs", "zip", "flv", "mp4"); $extension = end(explode(".", $_FILES["file"]["name"])); if (($_FILES["file"]["size"] < 32428800) && in_array($extension, $allowed_ext)) { if ($_FILES["file"]["error"] > 0) { //$message.="There is some error in upload, see: " . $_FILES["file"]["error"] . "<br>";//Enable this to see actual error $message.="There is some error in upload. Please try after some time."; } else { $uploaded_file = uploaded_file_to_s3($_FILES["file"], "uploads", true); if ($uploaded_file != FALSE) { $user_name = @$_POST['user_name'] != "" ? @$_POST['user_name'] : "Anonymous"; $form_data = array( 'file' => $uploaded_file, 'user_name' => $user_name, 'type' => 'file' ); mysql_query("INSERT INTO `phps3files` (`id`, `file`, `user_name`, `type`) VALUES (NULL, '" . $uploaded_file . "', '" . $user_name . "', 'file')") or die(mysql_error()); $message.= "File successfully uploaded in S3 Bucket."; } else { $message.="There is some error in upload. Please try after some time."; } } } else { $message.= "Invalid file, Please upload a gif/jpeg/jpg/png/pdf/doc/docs/zip file of maximum size 30 MB."; } } ?> <?php require_once 'header.php'; ?> <fieldset> <legend>PHP AWS S3 integration library Demo1</legend> Description: In this demo a file is being upload to an S3 bucket using "PHP AWS S3 integration library". After upload you can check the uploaded file in below table. If you require some manipulation before uploading file to S3 then check <a href="upload_file_manually.php">Demo2</a> <br /> <br /> <form action="" method="post" enctype="multipart/form-data"> <div class="control-group"> <label for="file" class="control-label">Choose a file to upload: <span style="color:red">*</span></label> <div class='controls'> <input id="file" type="file" name="file" /> <?php //echo form_error('file'); ?> </div> </div> <div class="control-group"> <label for="user_name" class="control-label">Your name:</label> <div class='controls'> <input id="user_name" type="text" name="user_name" maxlength="255" value="" /> <?php //echo form_error('user_name'); ?> </div> </div> <div class="control-group"> <label></label> <div class='controls'> <input type="submit" name="submit" value="Submit" class="btn"> </div> </div> </form> </fieldset> <?php if ($message != "" || @$_SESSION['message'] != "") { ?> <div class="alert alert-success"> <?php echo $message; ?> <?php echo @$_SESSION['message']; @$_SESSION['message'] = ''; ?> </div> <?php } ?> <div> <table class="table table-hover"> <caption> <strong>Last 10 user uploaded files</strong> </caption> <?php $files_result = mysql_query("SELECT * from `phps3files` WHERE type LIKE 'file' ORDER by id DESC LIMIT 10"); $i = 1; while ($file = mysql_fetch_object($files_result)) { ?> <tr> <td><?php echo $i++; ?></td> <td><a href="<?php echo site_url_s3("uploads/" . $file->file); ?>" target="_blank">View/Download</a> </td> <td><a href="<?php echo site_url("delete_file.php?id=" . $file->id); ?>">Delete file from S3</a></td> <td><?php echo "Uploaded by: " . $file->user_name; ?></td> </tr> <?php } if ($i == 1) { ?> <tr> <td colspan="2"> No files uploaded yet</td> </tr> <?php } ?> </table> </div> <h4>Source Code Part of Demo</h4> <pre class="prettyprint lang-php linenums"> <?php session_start(); require_once 'phps3integration_lib.php'; $message = ""; if (@$_POST['submit'] != "") { $allowed_ext = array("gif", "jpeg", "jpg", "png", "pdf", "doc", "docs", "zip"); $extension = end(explode(".", $_FILES["file"]["name"])); if (($_FILES["file"]["size"] < 32428800) && in_array($extension, $allowed_ext)) { if ($_FILES["file"]["error"] > 0) { //$message.="There is some error in upload, see: " . $_FILES["file"]["error"] . "<br>";//Enable this to see actual error $message.="There is some error in upload. Please try after some time."; } else { $uploaded_file = uploaded_file_to_s3($_FILES["file"], "uploads", true); if ($uploaded_file != FALSE) { $user_name = @$_POST['user_name'] != "" ? @$_POST['user_name'] : "Anonymous"; $form_data = array( 'file' => $uploaded_file, 'user_name' => $user_name, 'type' => 'file' ); mysql_query("INSERT INTO `phps3files` (`id`, `file`, `user_name`, `type`) VALUES (NULL, '" . $uploaded_file . "', '" . $user_name . "', 'file')") or die(mysql_error()); $message.= "File successfully uploaded in S3 Bucket."; } else { $message.="There is some error in upload. Please try after some time."; } } } else { $message.= "Invalid file, Please upload a gif/jpeg/jpg/png/pdf/doc/docs/zip file of maximum size 30 MB."; } } ?> </pre> <?php require_once 'footer.php'; ?> Say I have the following text stored in a MySQL database... Code: [Select] <b>Classic Quote from movie</b> and I retrieve it into a variable called $text, how do I properly echo that so that it keeps the bold tags and actually display the text "Classic quote from movie" in BOLD? I'm doing something wrong somewhere along the line (simply doing "echo $text;") because it displays on the page as... Code: [Select] <b>Classic Quote from movie</b> Instead of... Classic Quote from movie Any info on properly storing and echoing back HTML would be very appreciated. Do anyone know how i can put the html tags in the same line as the other html tags? Here's an example: Code: [Select] Images | Link | Delete | Enabled On my code, it break the tags to the new line without put on the same line as the other tags. Here's the code: Code: [Select] [code]<?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'myusername'); define('DB_PASSWORD', 'mypass'); define('DB_DATABASE', 'mydbtable'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $username = clean($_GET['user']); $password = clean($_GET['pass']); if($username == '') { $errmsg_arr[] = 'username ID missing'; $errflag = true; } if($password == '') { $errmsg_arr[] = 'PASSWORD ID missing'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $qry="SELECT * FROM members WHERE username='$username' AND passwd='$password'"; $result=mysql_query($qry) or die('Error:<br />' . $qry . '<br />' . mysql_error()); if(mysql_num_rows($result) > 0) { $qrytable1="SELECT images, id, public FROM members WHERE username='$username'"; $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error()); while ($row = mysql_fetch_array($result1)) { echo '<p id="images"> <a href="images.php?id='.$row['id'].'">Images</a></td> | <a href="http://' . $row["links"] . '">Link</a> </td> | <a href="delete.php?id='.$row['id'].'">Delete</a> </td> | <p> <p id="test">'.$row['Public'].'</td>''; } } } ?>[/code] Hey guys, I'm a total newbie here, and just about as a new to php. My issue: I have a very large .html file that contain multiple articles (I actually have a few of these, but we'll start with one for practicality). The article titles are all wrapped in <h2> tags, there are 10 articles in one file. The articles are very simple, just a title wrapped with <h2> and then a few paragraphs wrapped in <p> tags. What I want to know how to do: I want to know if there's a way to open that file, and have each article saved as it's own .html or .txt document (the title & following paragraphs of each article). Ultimately taking my 1 large file, and creating the subsequent 10 smaller files from the articles inside of it. I am having trouble explaining this in text so I'll try to illustrate: I have "Articles.html" - which contains (article1,article2,article3.. ..article10) I want to split "Articles.html" and create "Article1.html", "Article2.html", "Article3.html", etc. Is that possible? Or am I looking at something far more complex than I can imagine at this point - perhaps something I'd be better off doing by hand? Ultimately I intend to stick all these articles into a database, but that's the 2nd part of what I want to do (and I think will be the easier of the tasks). Let me know if you need any additional information in the event my description above is unclear... I simply am having issues figuring out how to separate out the text into individual articles. Lets say I pull a value from a database field that has a value of 1,2 and what I want to do is seperate them and then run each of those values against a separate table how would that be accomplished. Hi there. I'm doing a calendar page with php. It's based on a template and in this template all the output text and html-tags are saved into a xml string. I want to add a tooltip/hovering effect and found one on the net. I applied this script but it doesn't work. It works on "plain html", but when it is within the php it just won't work.. This is how my code looks like: START: $xml = '<?xml version="1.0" ?><response><content><![CDATA['; --- kode -- kode -- kode --- $xml.="<div class='calevent'> <a href='' class='someClass' title='<img src=1.jpg>'>Curabitur dolor eros</a> <a href='java script:navigate(\"\",\"\",\"".$row[0]."\")'>"; $xml.=$erow[0]; $xml.="</a></div>"; END: echo $xml; ------------- The script is supposed to add tooltips when an element is declared "someClass" and it works on plain html on the same page.. Take a look: http://clubbinghagen.dk/calendar/ Links in the calendar have no tooltip, but the Admin link in the center bottom (a bit hard to see!) has a working image as a tooltip. Any ideas?? I'm going through my project trying to find a way of separating the backend (the query and php code) from the frontend (html/css). I want to bring the two together with the include function. I've done other but I'm getting confused because the while loops html code. I don't want someone to do this for me, I just what someone to highlight the most logical way. <?php include 'auth.inc.php'; include 'db.inc.php'; include 'buy.inc.php'; //connect to database. $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die ('Unable to connect. Check your connection parameters.'); mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); //select orders that this user has bought. $query2 = sprintf('SELECT d.name_id, d.order_id, d.order_qty, d.product_code, feedback, p.title, c.email FROM order_details d LEFT JOIN product p ON d.product_code = p.product_code LEFT JOIN contact c ON d.name_id = c.name_id WHERE d.buyer_id = "%u"', mysql_real_escape_string($_SESSION['name_id'])); $result2 = mysql_query($query2, $db) or die(mysql_error()); //echo out the information in a while loop, add pagination? $odd = true; while ($row2 = mysql_fetch_array($result2)) { echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">'; $odd = !$odd; echo '<td style="text-align: center; width:100px;">' . $row2['title'] . ' <table> <tr> <th colspan="2">Shipping Information</th> </tr><tr> <td>First Name:</td> <td>' . $row2['title']. '</td> </tr><tr> <td>First Name:</td> <td>' . $row2['order_qty']. '</td> </tr><tr> <td>Last Name:</td> <td>' . $row2['product_code']. '</td> </tr><tr> <td>Billing Address:</td> <td>' . $row2['email'] . '</td> <td>' . $row2['order_id'] . '</td> </tr> </table> </tr>'; if ($row2['feedback'] == 0){ echo ' <form action="reputation.php" method="POST"><p>Please give the seller feedback on how you have received your product. <td><input type="hidden" name="name_id" value="' . $row2['name_id'] . '"></td> <td><input type="hidden" name="order_id" value="' . $row2['order_id'] . '"></td> <td><input type="submit" name="submit" value="Feedback"/></td> </form>'; } } ?> I'm having trouble trying to separate the output into variables that I can use/echo on my page. when I do a print I see the two rows of data all grouped together how can I separate each result base on field and row? maybe something like $line['m_id'][0], $line['m_name'][0], $line['m_id'][1], $line['m_name'][1], etc... $bio = mysql_query("SELECT * FROM soc_meminfo WHERE m_id = '".mysql_real_escape_string($en['mm_id'])."'"); if (mysql_num_rows($bio) == 0) call404(); while ($line = mysql_fetch_assoc($bio)) { foreach ($line as $key => $value) { $en['b'.$key] = str_replace("\n",'<br/>',stripslashes($value)); } echo '<pre>'; print_r($line); echo '</pre>'; } Hi there i was wondering how i would go about using html tags inside the php tags so i could put data from a database into a textarea / text input i had a go but had no such luck with this attempt: <?php echo "Title:<br>"; echo "<input type="text" name="title" value="$row['Title']">"; ?> the code above is to give you an idea of what i want to do, if you can point me to any guides or show me how it will be greatly appriciated Thanks, Blink359 How can I add html tags in php as a variable? For example, for some reason my code gives me a header error if I use html before the php tags. I just need to add the style in there. I was wondering how I could add a style of any html tag within php as a variable?? Like I was thinking like <?php $style = "<style> div.error { border: 1px dashed #660000; background: #fee; color: #660000; } </style>"; echo $style; ?> But that would probably just print out the code... how can I make it so it wont print out on screen and I can actually use the style? Sorry for the noob question... im kind of an intermeddiate php programmer but the noobs questions still seem to never go away! Hi, this is probably really simple for the majority here, but it's catching me out. I want to know what I need to add to the code below to strip <> HTML tags from the field post_title. Hope somebody can help. Thanks in advance. Code: [Select] <?php mysql_connect("********", "********", "********") or die(mysql_error()); mysql_select_db("website_news") or die(mysql_error()); $data = mysql_query("SELECT * FROM news_posts ORDER BY post_date DESC LIMIT 0, 5") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { Print "<font color='black' face='arial'><li><a href='/website/news/?p=".$info['ID'] . "'>"; if ( strlen($info['post_title']) > 60) { $PostTitle = substr($info['post_title'], 0, 60); print $PostTitle. "..."; } else { print $info['post_title']; } Print "</a> <font color=#666666><i> - posted on "; $date = date('j M \'y', strtotime($info['post_date'])); Print $date; Print "</i>"; } ?> Hello, I'm working on a page where users can add articles by writing text in textareas with a WYSIWYG editor. When they submit the form it's saved in a database. As a summary of the article i grab the first 800 characters of the article, but as you could imagine there might occur html tags like <div> or <span> in the summary which are not closed. To prevent this from ruining my page layout when their articles are posted on the wegbsite I could use strip_tags but I'd like to keep the format, also this would delete images. I couldn't think of another solution then a function which checks for open tags and if so; add closing tags at the end of the summary. I already made a similar function a while back, but that one only checks for <div> and <span>, as those are the worst.. The nasty part is that I kind of deleted that function accidentally, and I can't fully remember how I wrote that.. So what I would like to have is a function that checks for all unclosed html tags and add the associated closing tags, in the right order, at the end of the summary. Any help getting on the right track is appreciated. Hello, I can't find those or mr. Google, can anyone please help me to find them? the tags that can be included in HTML and deciphered by PHP as a PHP code <if> <else> <elseif> etc.. Thanks Hello, I just wanted to run over some code i'm using to loop through for a page. And wanted people critique on it really. It is a bit messy i feel trying to include all the html tags inside the loop in order for me to achieve the page look i want and whilst maintaining to xhtml standards. Would any one please take a look at my code which works and tell me how they would do it or if its done badly as i'm interested to know $q = "SELECT title, content, get, link FROM content WHERE page='index'"; $r = mysqli_query($dbc, $q); while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { echo '<h2>'; // print for start of H-tag echo $row['title']; // print title inside H-tag echo '</h2><p>'; // close H-tag, line break, start of P-tag $get = $row['get']; $link = $row['link']; $content = $row['content']; $content = explode(' ', $content); $y = 90; $x = 0; while ($x < $y) { echo $content[$x].' '; // print content inside P-tag $x++; } // end of content echo '</p><a href="seo.php?x='; // close P-tag, start A-Tag print link echo "$get"; // $_GET[''] echo '">read more...&#187;&#187;</a>'; // close url start the anchor text and close off </a> } // End of while echo '</div>'; // End of left content echo '<div class="right">'; // Start of right div echo '<h2>News</h2>'; // News title I'd appreciate any feed back from more experienced users/coders Thank you in advance :-) I'm trying to learn php, and I've picked up a php/mysql member system type script, and I'm trying to put the login/sign-up forms into my own pages. The problem I'm having though is getting PHP and HTML to work in the same echo""; Hello firstly i would like to say im very new to this forum (Signed up 5 minutes ago). I have decided to join as im in my final year at university and have been asked to create a website, so im guessing ill be coming back here a lot for help! . Anyway my most current problem is i have an if statement that hides the "Register" button if the user is logged in and Shows a welcome message and the users name if the user is logged in. However i would like to make the log in form disappear if the user is logged in. I know that by putting the form code in the else part of my if statement it will disappear if the user is logged in however when i do this it stops my whole website working. Any help would be very grateful. Thanks Glen. In my mail function, the $message parameter contains html tags such as <br><B> <a> etc etc.. But when I receive the email those tags are non functional. I do not get a bold text or a line break or a hyperlink as I should. How do i make it work? Can someone give me some guidance of how to separate Form Display from Form Processing? I have always used forms that submitted back to themselves which isn't so bad, but then trying to cram in code to display the form, validation errors, and messages after the form is processed all in one file is insane?! Currently I am working on a simple "Add a Comment" form. It would be nice to have a separate form processing script, but I don't know where to begin... Debbie I wrote a VB6 app that uses a web browser control to create an inbuilt instance of IE. I then used that to navigate to a LOC web page, do a search and collect MARC tag data. I was wondering how you could do this in PHP.? I put up some attachments that show this working in VB6. The searchable web page that returns results is here http://www.loc.gov/cgi-bin/zgate?ACTION=INIT&FORM_HOST_PORT=/prod/www/data/z3950/locils2.html,z3950.loc.gov,7090 So you do a search for something. LOC gives you a list. You click on an item in the list where it says 'more on this record' then you click 'tagged display' And that is the data you want to have collected into your variable. I created a Download Icon on the web browser control that turns green when the data is displayed. Do you somehow create your own web browser instance? or? I am new to PHP and have figured out a few things. I created this here in PHP http://www.sdowney717.byethost16.com/mysearch2first.php In a text box I put in some code snippets that I want to refer to later, but when I display it, it displays it in html. How do I convert the code into pure text so it displays the actual code? If I try strip_tags, that just strips it (and it doesn't work with php). And I can't use strip_tags with conditions, because I don't want to list all codes! Also, the code has both html tags and php code - I just want that whole thing to be displayed as is, without havign the browser try to "read" it. Also if I put this into the text box, <td><?php echo date ("m/d/Y", strtotime($row['code_date']));?></td> then nothing is displayed. If I put this in the box: <input class="button" style="display:inline" name="whatever" type="submit" value="Press here" /> then I get an actual submit button instead of the code! |