PHP - How To I Accept Only <a Href> Tag But Strip All Other Html Tags?
I have been searching on google for a while, but i couldn't find it. So i thought may be you could direct me to some tutorial or steps if you knew.
Basically, i am working on a articles directory and the big text area where the main article will be entered, i want to allow all the links (link tag) on it but not any other html tags. Currently i am using strip tags and so its cutting down the tags and all the links are being displayed naked on it. So can you please tell me how do i do it? Thanks.. Similar Tutorialshi everyone, did not know what to make the subject, but here is what I want to do: I have a string, which gets returned to me from a linux app on my server, it looks something like this: Code: [Select] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8"> <TITLE> </TITLE> <META NAME="GENERATOR" CONTENT="OpenOffice.org 3.2 (Linux)"> <META NAME="AUTHOR" CONTENT="Administrator"> <META NAME="CREATED" CONTENT="20110106;14170000"> <META NAME="CHANGEDBY" CONTENT="HOD"> <META NAME="CHANGED" CONTENT="20110522;16540000"> <STYLE TYPE="text/css"> <!-- @page { margin: 0.26in } P { margin-bottom: 0.15in; direction: ltr; color: #000000; line-height: 0.15in; text-align: justify; widows: 2; orphans: 2 } P.western { font-family: "Arial", sans-serif; font-size: 10pt; so-language: en-US } P.cjk { font-family: "Batang", "바탕", serif; font-size: 10pt } P.ctl { font-family: "Times New Roman", serif; font-size: 10pt; so-language: ar-SA } A:link { color: #0000ff } --> </STYLE> </HEAD> <BODY LANG="en-US" TEXT="#000000" LINK="#0000ff" DIR="LTR" STYLE="border: 5.05pt double #000000; padding: 0.67in 0.92in"> <P>I want this and the tags around it, just not the html, head, body and their closing tags.</P> </BODY> </HTML> within the body, the tags are each styled for example: <p style="color: red"></p> so I cannot just get rid of all html, I want to get only all the content within the body tags, but without the body tags obviously strip_tags does not work as i need, I only want to strip certain tags. If someone can help me with this i will much appreciate it. I need to strip some html tags out of an uploaded string of code. I need to keep the <td> tags...but some code that is being uploaded include <p> tags INSIDE the <td> tag.
How would I go about stripping ALL other tags inside these allowed tags: <td> <tr><table>
In Wordpress, the hyperlinked tags are displayed by <?php the_tags(); ?> I want to de-link the tags to show simple text (instead of link). I tried Code: [Select] ereg_replace to replace hyperlink elements; strip_tags, but none of them worked. 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. So I have an interesting one for you guys this AM, I first want to make it very clear that I am not scraping code, rather I am scraping data that is needed to import into a shopping cart system for someone. I have a URL that I am trying to scrape required data off of, however it is not returning all the data that I want. I have created a function that uses preg_match_all() and regex and I am still having issues striping what I want. here is a link to my test what I am wanting to strip from http://visualrealityink.com/dev/clients/rug_src/scrapeing/Rugsource/www.vendio.com/stores/Rugsource1/item/other/tribal-wool-3x5-shiraz-persian/lid=10363581.html I am wanting to grab all this data: Quote Item Number: K-686 Style : Shiraz Province : Fars Made In : Iran Foundation : Wool Pile : 100% Wool Colors : Red, Navy Blue, Ivory, Forest Green, Light Blue, Orange Size (feet) : 4' 11" x 3' 4" Size (Centimeter) : 155 x 103 Age : 20-25 Years Old Condition : Very Good KPSI (knots per sq. inch) : 130 knots per square inch Woven : Hand Knotted Shipping and Handling : Free Shipping(For Mainland USA) Est. Retail Value : $2,700.00 Here is the code note that $url holds the link above. Code: [Select] $html = file_get_contents($url); $newlines = array("\t","\n","\r","\x20\x20","\0","\x0B"); $html = str_replace($newlinews, "", html_entity_decode($html)); preg_match_all('/<tr><td width="50%" align="right"><font color="#800000"><b>[^\s ](.*?)<\/b><\/font><\/td><td width="50%" align="left">[^\s ](.*?)<\/td><\/tr>/', $html, $matches, PREG_SET_ORDER); foreach($matches_label as $match){ $count = 0; echo $match[$count]; echo "<br>"; $count++; } echo $count; This returns the following Quote Style : Shiraz Province : Fars Foundation : Wool Colors : Red, Navy Blue, Ivory, Forest Green, Light Blue, Orange Size (feet) : 4' 11" x 3' 4" Size (Centimeter) : 155 x 103 Age : 20-25 Years Old Condition : Very Good Est. Retail Value : $2,700.00 1 it is missing: Quote Inventory Number : xxxxxxx Made In: xxxxxxxx Pile : xxxxxxxxxx KPSI(Knots Per Inch) : xxxxxxxxxx Woven : xxxxxxxxx Shopping : xxxxxxxxxxx You can see the script in action here -> http://visualrealityink.com/dev/clients/rug_src/scrapeing/scrape_tst.php Thanks in advance for all of your help 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. Hey all, I'm working on a website to show various products. I am using PHP, HTML, MySQL. I have a products page which shows a table of products (either all or just products from a specific category). Currently, the page works fine, shows an image, brand, name, and price of each product. However, when I have tried to make the image and the brand and name a link, it doesn't fully work. In the page source it will show correctly, but on the webpage itself I cannot click on the links. I have scoured various forums and manuals and have tried all the various ways to code the <a href=> but it still does not work right. Here's a snippet of the code with the link... Code: [Select] /*Other code above for MySQL queries, HTML table etc*/ <td> <?php echo '<a href="details.php?id=' . $row['id'] . '"><img src="resize.php?id=' . $row['id'] . '"/></a>'; ?> </td> <td> <?php echo "<a href=\"details.php?id=" . $row['id'] . "\">" . $row['brand'] . "<br>" . $row['name'] . "</a><br>"; echo "Price: $" . $row['price'] . "<br>"; ?> </td> I have even tried putting the <a href="details.php?id=<?php echo $row['id']; ?> ... </a> just between the <td> and </td>. I still can't click the links on the page, even though they are technically there. The page source will show <a href="details.php?id=1"> ... =2"> ... =3"> ... etc for each product that is displayed. If I actually type details.php?id=1 in my browser and go back to the products page the text will change color to the "visited link" color. Any insight into my dilemma? Thanks in advance... Hi guys; I've managed to find my way into something of a maze. <td> <a href="'Details/21/index.php'"?id= . $id .'>" . $row['id'] . "</a> </td> I'm looking at this line in the code. I realise it's currently in the wrong syntax but I'm just trying multiple different variations. At the moment I'm actually just trying to make it go to a static link but my real goal is to - get it to pick up the id number, of the id row I click, and concatenate that with the rest of my address string. Then use that as the href. Something like this: - "'Details/' + $id + '/index.php'" It's really confusing me though inside this loop and for some reason the id number is being picked up as an int, by the looks of things. It's getting above my level of understanding. Any chance one of you masters would through a n00b a lifeline?
This is my code below
<!DOCTYPE html> <html> <head> <title>LifeSaver DB</title> <h1> LifeSaver Database </h1> </head> <body> <table> <tr> <th>Id</th> <th>Location</th> <th>Initials</th> <th>TimeStamp</th> <th>Notes</th> </tr> <?php $conn = mysqli_connect("localhost", "meh", "pas", "DB"); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM LifeSaver1 ORDER BY id DESC"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { //for href row $id = $row['id']; $Footage = ['Footage']; echo "<tr> <td> <a href="'Details/21/index.php'"?id= . $id .'>" . $row['id'] . "</a> </td> <td>" . $row["Location"] . "</td> <td>" . $row["Initials"]. "</td> <td>" . $row["TimeStamp"]. "</td> <td>" . $row["Notes"] . "</td> </tr>";} //show table echo "</table>"; } else { echo "0 results"; } $conn->close(); ?> </table> </body> <style> table, td, th { border: 1px solid black; margin: auto; } table { border-collapse: collapse; color: #000; <!--font colour --> font-family: monospace; font-size: 18px; text-align: center;} th { background-color: #337AFF; color: white; font-weight: bold; } tr:nth-child(odd) {background-color: #add8e6} </style> </html> Edited February 8, 2020 by JonnyDriller 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?? 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? 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! 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 :-) 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 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'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 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. 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""; 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>"; } ?> 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 |