PHP - Get Content Between Tags
Hi guys I need to be able to extract (into an array) the content between two tags "<!--" and "-->" in a string ($string).
I have $string = "<!--HELLO WORLD-->"; $tags = "/<!--(.*?)--!>/"; preg_match($tags, $string, $matches); return $matches[1]; but it does not seem to work, (no error code, nothing). Any help much appreciated, 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 am asking out of curiosity, how does this problem usually get solved, when using a tag system to sort content, and users enter similar tags. For example: flower and flowers... which means the difference between singular and plural. Judging by other sites which are around, this problem is not solved at all, they simply let you add as many tags as you want and then rank the most used tags, it indeed is a solution, but seems more like a walk-around solution to me. Any ideas how somebody could approach to solve this one? EDIT: My suggestion would be an analysis function, which analyzes the entered tag and then suggests the user that similar tags have been already entered and perhaps he wants to choose one of those which have been already entered. This does work though with a high traffic websites what can happen is the following: php and php5 You can end up with two similar tags which are both widely used though have different meanings to the user base. This one could be solved by simply prohibiting similar tags with little additions or changes, and so to speak "forcing" the user to choose something which is already there. May be a way, though it can turn out as a bad solution as well. 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] 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. Hi, I will start off trying to explain what I am trying to make the best I can. What I want to create is a script that gets the gold value from this website: http://www.lbma.org.uk/pages/index.cfm?page_id=46&title=current_statistics and then save it to a variable which I will use to calculate values of different gold karats. Here is the content in bold I need on the website I linked: Quote LONDON GOLD FIXING USD GBP EUR AM 1588.00 1005.127 1251.083 PM 1589.50 1004.741 1249.803 So what help do I need? Well, I don't expect you to figure out the calculating part for me but some help how to get that content pointed out above and save it to a variable is what I would appreciate getting some help with. I don't know much PHP, only some and I have been trying to figure this out for a day now without any success. I suppose php get contents and/or curl should be used here but I don't know how really. I would very much appreciate the help I can get on this. Thank you! This is the Code i am using. This is show error: Fatal error: Call to a member function item() on a non-object in /home/domain/public_html/forum/file/Test.php on line 35 Code: [Select] <?php $xml=("http://www.vn-zoom.com/external.php?type=RSS2&forumids=77"); $xmlDoc = new DOMDocument(); $xmlDoc->load($xml); //get elements from "<channel>" $channel=$xmlDoc->getElementsByTagName('channel')->item(0); $channel_title = $channel->getElementsByTagName('title') ->item(0)->childNodes->item(0)->nodeValue; $channel_link = $channel->getElementsByTagName('link') ->item(0)->childNodes->item(0)->nodeValue; $channel_desc = $channel->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue; //output elements from "<channel>" echo("<p><a href='" . $channel_link . "'>" . $channel_title . "</a>"); echo("<br />"); echo($channel_desc . "</p>"); //get and output "<item>" elements $x=$xmlDoc->getElementsByTagName('item'); $i=1; // $i = 1 to n (I use For here). { $item_title=$x->item($i)->getElementsByTagName('title') ->item(0)->childNodes->item(0)->nodeValue; $item_link=$x->item($i)->getElementsByTagName('link') ->item(0)->childNodes->item(0)->nodeValue; $item_desc=$x->item($i)->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue; // If i remove this, it will work....//////////// $item_content=$x->item($i)->getElementsByTagName('content') ->item(0)->childNodes->item(0)->nodeValue; ///////////////////////////////////////////////////// echo ("<p><a href='" . $item_link . "'>" . $item_title . "</a>"); echo ("<br />"); echo ($item_desc . "</p>"); echo ("<br />"); echo ($item_content . "</p>"); } ?> Please help me Fix this Code to Get content of Tag Name <content:encoded> Thanks alright I've seen and use Code: [Select] <?php Code: [Select] <?but never before do I recall seeing Code: [Select] <?= Nor do I even know where to look that up, so.. anyone ever see that used before, if so, any insight as to why someone would choose that? I have just been given some code that is live on a web site. I pulled it down to my local linux machine and the php will not run. I have worked out the issue the php tags are <? scripting ?> it is missing the php. I have a couple of questions how do I get my local linux machine get <? scripting ?> this to work and I have a lot of code that is like this and I am sure that it is no longer secure is this right? I found this code on a website: <?php //Reads ID3v1 from a MP3 file and displays it $mp3 = "1.mp3"; //MP3 file to be read //make a array of genres $genre_arr = array("Blues","Classic Rock","Country","Dance","Disco","Funk","Grunge", "Hip-Hop","Jazz","Metal","New Age","Oldies","Other","Pop","R&B", "Rap","Reggae","Rock","Techno","Industrial","Alternative","Ska", "Death Metal","Pranks","Soundtrack","Euro-Techno","Ambient", "Trip-Hop","Vocal","Jazz+Funk","Fusion","Trance","Classical", "Instrumental","Acid","House","Game","Sound Clip","Gospel", "Noise","AlternRock","Bass","Soul","Punk","Space","Meditative", "Instrumental Pop","Instrumental Rock","Ethnic","Gothic", "Darkwave","Techno-Industrial","Electronic","Pop-Folk", "Eurodance","Dream","Southern Rock","Comedy","Cult","Gangsta", "Top 40","Christian Rap","Pop/Funk","Jungle","Native American", "Cabaret","New Wave","Psychadelic","Rave","Showtunes","Trailer", "Lo-Fi","Tribal","Acid Punk","Acid Jazz","Polka","Retro", "Musical","Rock & Roll","Hard Rock","Folk","Folk-Rock", "National Folk","Swing","Fast Fusion","Bebob","Latin","Revival", "Celtic","Bluegrass","Avantgarde","Gothic Rock","Progressive Rock", "Psychedelic Rock","Symphonic Rock","Slow Rock","Big Band", "Chorus","Easy Listening","Acoustic","Humour","Speech","Chanson", "Opera","Chamber Music","Sonata","Symphony","Booty Bass","Primus", "Porn Groove","Satire","Slow Jam","Club","Tango","Samba", "Folklore","Ballad","Power Ballad","Rhythmic Soul","Freestyle", "Duet","Punk Rock","Drum Solo","Acapella","Euro-House","Dance Hall"); $filesize = filesize($mp3); $file = fopen("1.mp3", "r"); fseek($file, -128, SEEK_END); $tag = fread($file, 3); if($tag == "TAG") { $data["song"] = trim(fread($file, 30)); $data["artist"] = trim(fread($file, 30)); $data["album"] = trim(fread($file, 30)); $data["year"] = trim(fread($file, 4)); $data["comment"] = trim(fread($file, 30)); $data["genre"] = $genre_arr[ord(trim(fread($file, 1)))]; } else die("MP3 file does not have any ID3 tag!"); fclose($file); while(list($key, $value) = each($data)) { print("$key: $value<br>\r\n"); } ?> How would i edit this to look in a directory, and sub-directories and get information for every mp3 and add the data to a mysql database (the latter i can do easily) Hi guys, i have not started this as of yet, however i will be given a project to make a micro-blogging website similar to that of Twitter or Facebook but obviously on a much smaller scale I have been searching on google however i have not found anything. Maybe i haven't been using the right words in my query, not sure :-/ I have been trying to do some research, however i can't seem to find how to incorporate @ tags found in Twitter / Facebook. e.g. If i want to tag a person in a Status Update: Hey @John Smith, Football in the park at 3pm? If anyone is able to help i would be grateful Thank You How does PHP continuity exist in different php tags? for example, if i connect to a database in a header, and close the tag, can I still work off of the same database in another tag? Hello i said to replace the short php tags (<??>) with long php tags (<?php ?>) but it is giving me Parse errors -.- the parse error apears if i have <?php//NOTE ?> With <?//NOTE?> Is all ok and it dosnt give me that error .. could anyone please tell me why dos it give the error and some more details about the tags etc Thanks in advance 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 made a script to grab the data between these tags on my page: Code: [Select] <td class="numbers"> </td> It shows only the numbers and that's what I want (see Ex: ) but if I view the source code it shows this: Code: [Select] Andreea - <td class="numbers">111</td><br /> Claudia - <td class="numbers">30</td><br /> Daniel - <td class="numbers">37</td><br /> Adela - <td class="numbers">4</td><br /> Is there a way to display the result without those tags ? Ex: Andreea - 111 Claudia - 30 ............ As you can see, I only need the numbers, there's nothing else inside those tags. I wish to have all my products show in an XML for googles product search. Also when I try to view this in internet explorer I get an error about quote marks (") being invalid. Code: [Select] <? header('Content-Type: text/xml; charset=UTF-8'); include("includes/functions.php"); $connected = dbConnect(); // the DATE_FORMAT is to make the date RFCwhatsit compliant $datetocheck = date("Y-m-d H:i:s"); $sql = "SELECT * FROM `products` ORDER BY `productTitle` ASC"; $resultset = mysql_query($sql); //echo("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ? >\n"); echo("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); ?> <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> <? echo('<atom:link href="http://www.site.com/rss.php" rel="self" type="application/rss+xml" />'); ?> <channel> <ttl>1</ttl> <title>Welcome - RSS Feed</title> <link>http://www.site.com?s=rssimg</link> <description>RSS Feed</description> <language>en-us</language> <pubdate><?=gmdate("D, d M Y H:i:s \G\M\T");?></pubdate> <lastbuilddate><?=date("D, d M Y H:i:s \G\M\T");?></lastbuilddate> <generator>OnTheFly</generator> <image> <title>Welcome - RSS Feed</title> <url>http://www.site.com/images/header.png</url> <link>http://www.site.com?s=rssimg</link> <width>88</width> <height>31</height> </image> <?php while ($result = @mysql_fetch_assoc($resultset)) { $submitteddatetime = date("D, d M Y H:i:s \G\M\T", strtotime($datetocheck)); ?> <item> <title><![CDATA[<? echo(htmlentities(stripslashes($result[productTitle]))); ?>]]></title> <guid>http://www.site.com<? echo("/?v=" . $result[Pid] . "&h=r"); ?></guid> <pubdate><? echo($submitteddatetime); ?></pubdate> </item> <?php } ?> </channel> </rss> Im using this code right here for to explode titles in my search engine into individual click-able tags <?php $tobeOmitted=array('stuff'); $tagss = explode(" ", str_replace('-', ' ', $title1)); foreach( $tagss as $key => $value){ if(!in_array($value,$tobeOmitted)) echo "<a href='/search.php?query=$value&search=1' target='rel' title='View search results in new window'> $value</a>"; } } ?> Is there a way I can limit the number of tags for each title to 3? I dont want over 100 links on my page because it looks bad in google so I am trying to cut down some pages that have up to 200 links because of long titles and this would cut things down drastically. Any help is appreciated and thank you. hi freaks, still working on my email bot. im currently trying to escape <>-tags to be displayed- but not the browser wont exectute it (i.e. xls-tags) i want them as chars. so whin my $content is like <xsl:stylesheet> <xsl:template match="/"> <more xls...> it shows a stylesheet. i just want the chars. Here is what ive been trying so far: but its bad Code: [Select] <?php $string='<tag> > < asdasf'; $bomb=array( '<', '>', ); $defusal=array( '-', '!', ); $string= preg_replace($bomb,$defusal,$string); echo $string; ?> error: Warning: preg_replace() [function.preg-replace]: No ending matching delimiter '>' found in C:\xampp1\htdocs\code.php on line 13 donno any further now Hi all This probarly looks really bad but im trying to replase tags that change. So far it only replaces the last tag and not well either. I will try to show you Code: [Select] function testfunc($value) { if ($value=='gallery'){$return .= "this gallery has worked = --$value--";} if ($value=='email'){$return .= "this email has worked = --$value--";} return $return; } $test = "this is a test {%email%} message {%gallery%} rtfgdsfgdsfg"; $test = preg_replace("#(.*){%(.*?)%}(.*)#is",'$1' . testfunc('$2') . '$3', $test); echo $test; this outputs this is a test {%email%} message rtfgdsfgdsfg but im trying to get this this is a test this email has worked = --email-- message this gallery has worked = --gallery-- rtfgdsfgdsfg Im sure im way off but I hope you can understand. Thank you for taking the time to read this post. This works fine with a couple of exceptions... how do I add zero borders, alt tags etc... plus it is displaying like this htt:/// in the URL... Code: [Select] <?php echo '<a href="http://'.$row['url'] .'"\" target=\"_BLANK\""><img src="images/ads/'.$row['photo'].'" /></a>'; ?> hey guys, I am wondering how to style the below code snippet using css? the code itself is working - i just am trying to figure out how to style it :( <div class="section"> <?php if(isset($_SESSION['uid'])) { echo 'Hello,'; echo $_SESSION["uid"]; } ?> </div>
|