PHP - Replace Html 5 Tag Contents
Hello there,
Using PHP, what's the best way of replacing the contents between different HTML 5 element tags on the fly? Some of the elements I'm looking to change can be found below. I have looked into the possibility of using Regular Expressions, but have found lots of material on the internet that try to prevent you from using such a method for parsing HTML due to speed and other HTML code related issues. Any help, example code, and ideas on this is greatly appreciated. Code: [Select] <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Some Title</title> </head> <body> <div>Replace contents of div tag.</div> <p class="someClass">Replace contents of p tag.</p> <table> <tr> <th>Static Table Heading 1</th> <th>Static Table Heading 2</th> <th>Static Table Heading 3</th> </tr> <tr> <td>Replace Table Data 1 Contents</td> <td>Replace Table Data 2 Contents</td> <td>Replace Table Data 3 Contents</td> </tr> <tr> <td>Replace Table Data 4 Contents</td> <td>Replace Table Data 5 Contents</td> <td>Replace Table Data 6 Contents</td> </tr> </table> </body> </html> Similar TutorialsHi Everyone, I have a problem displaying some information from an array (selected from my database). The array is below. Array ( [0] => Array ( [ssc_skill_categories] => Web [sc_skill_categories] => Programming ) [1] => Array ( [ssc_skill_categories] => Actionscript [sc_skill_categories] => Programming ) [2] => Array ( [ssc_skill_categories] => C# [sc_skill_categories] => Programming ) [3] => Array ( [ssc_skill_categories] => CSS [sc_skill_categories] => Programming ) [4] => Array ( [ssc_skill_categories] => Graphic [sc_skill_categories] => Designers ) [5] => Array ( [ssc_skill_categories] => Logo [sc_skill_categories] => Designers ) [6] => Array ( [ssc_skill_categories] => Illistration [sc_skill_categories] => Designers ) [7] => Array ( [ssc_skill_categories] => Animation [sc_skill_categories] => Designers ) ) What i would like to to is display this information in a table like so: <html> <body> <table> <tr> <td>Programming</td><td>Web</td><td>Actionscript</td><td>C#</td><td>CSS</td> <tr> <tr> <td>Designers</td><td>Graphic</td><td>Logo</td><td>Illistration</td><td>Animation</td> <tr> <table> </body> </html> I have been trying and failing all day to do this. Posting my "progress" will clog up the thread, so for now i wont post it. Does anyone have an idea how i would achieve this? Regards, -Ben Folks, I need to Find and Replace some words in Source HTML, which is saved in a PHP Variable. Example: Quote $html = '<img src="some_source" title="paintball mask Picture" alt="Image for Paintball Mask"> <strong> Best Paintball Mask</strong>'; Find Condition: I want to FInd word "Paintball Mask" in vairiable $html, only where if the word "Paintball Mask" is there in Htaml Tag <Strong> </Strong>. As you can observe in $html, we have "Paintball Mask" in <IMG> Tag also, i want to Skip this because I only Need to COnsider if the word "Paintball Mask" there in <strong></strong> or not. One the words is found in <strong></strong> tag, i need to replace that word, to make it a hyperlink. (I think this bit can be done with strreplace()) But how can i restict my search to only <strong></strong> tag? Let me give Data Again: Quote $html = '<img src="some_source" title="paintball mask Picture" alt="Image for Paintball Mask"> <strong> Best Paintball Mask</strong>'; Find What: Paintball Mask Find in Which Tag: <strong> </strong> Replace it with: <a href="/">Paintball Mask</a> can someone help me with the code? Cheers Natasha Thomas Hello I'm making a guestbook in PHP and I'm having trouble displaying the posts. The user fills a form and sends it, it goes into the database (mysql) and I can then fetch the data. So far so good. I'm then going to use HTML to display the posts to the user. It's code looking like this: Code: [Select] <p> <br /> ID: ---replaceid--- <br /><br /> Time: ---replacetime--- <br /> From: <a href="---replacehomepage---">---replacefrom---</a> <br /> Email: ---replacemail--- <br /> <br /> Comment: ---replacecomment--- <br /><hr> </p> I want to use str_replace in PHP to replace the values in HTML... like this: Code: [Select] $numquery = mysql_query("SELECT * FROM guestbook", $dbconnect); $num = mysql_num_rows($numquery); $i=1; while ($i < $num) { header('Content-type: text/html'); $html = file_get_contents("test.html"); $query = mysql_query("SELECT * FROM guestbook WHERE id='$i'", $dbconnect); $rows = mysql_fetch_row($query); echo str_replace('---replaceid---', nl2br($rows[0]), $html); echo str_replace('---replacetime---', nl2br($rows[1]), $html); echo str_replace('---replacefrom---', nl2br($rows[2]), $html); echo str_replace('---replacemail---', nl2br($rows[3]), $html); echo str_replace('---replacehomepage---', nl2br($rows[4]), $html); echo str_replace('---replacecomment---', nl2br($rows[5]), $html); $i++; } But this only replaces ---replaceid---, leaves the rest and outputs it. Then, only ---replacetime--- is replaced and so on. I hope you understand. It means that 1 guestbook entry is displayed like 6, with only 1 string replaced at each one. What do you think I should do? Worth saying is that I'm not allowed to mix PHP-code with HTML-code The situation is that I have a large string with some identifiers to say "replace me" with something else. My goal is to replace the values, but if one replacement includes one of the other keys, I don't want to replace it.
For example:
<?php $str = 'some blob ~abc~ followed by ~def~'; $arr_orig = array( '~abc~', '~def~' ); $arr_new = array( 'value~def~', 'blah' ); echo str_replace( $arr_orig, $arr_new, $str ); ?>This will echo: some blob valueblah followed by blah But that's not what I'm trying to accomplish. Basically, I want ~abc~ to be replaced with the literal: value~def~ and for the ~def~ that happens to be part of that string to NOT be replaced. I hope what I'm asking is clear. Basically, preg_replace/str_replace with arrays seems no different than just doing a FOR loop and replacing ~abc~ and then ~def~. But that's not what I need. I'm hoping that if ~abc~ is replaced with text that happens to be another identifier -- ~def~, ~xyz~, whatever -- that this already replaced text is not again replaced. Is there a built-in function to do something like this? If not, should I just parse the whole string and count characters or something? Seems like a pain (and slow!) Hi i wrote a find and replace code but my code replace last value of array and skip the first, second.... values in the array. Please give me an idea because i stack with this code. Regards $KeyWord = explode("\n", $RowGetWords['KEYWORDS']); $Replace = explode("\n", $RowGetWords['ReplaceTo']); for($i=0; $i<count($KeyWord); $i++){ $pattern = $KeyWord[$i]; $replace = "<a href=\"" .$URL. "\" target=\"_blank\" >" .$Replace[$i]. "</a>"; $html = str_replace($pattern, $replace, $Row['MessageBody']); } I want to parse some tiny lines from a webpage. I have few questions: 1. What is faster and better: cURL or file_get_contents? 2. Is it better to run preg_match commands form every line I want OR first get a shorter part (e.g. between <head> tag) then running preg_match for the lines? 3. Actually, I need to load the whole page before parsing. Is there a way to stop loading the whole page. For example, when I want something between <head> tag; stop loading <body>? Thank you in advance! I have this script below that makes an XML which displays the contents of a directory which contains image files. After running the .php, the XML output looks like this: Code: [Select] <gallery> <content src="./uploaded/images/Desert.jpg"/> <content src="./uploaded/images/Penguins.jpg"/> <content src="./uploaded/images/tux.png"/> <content src="./uploaded/images/Jellyfish.jpg"/> <content src="./uploaded/images/Chrysanthemum.jpg"/> <content src="./uploaded/images/Tulips.jpg"/> <content src="./uploaded/images/Koala.jpg"/> </gallery> I would like the output to be like: Code: [Select] <gallery> <photos> <photo> <content src="./uploaded/images/Desert.jpg"/> </photo> <photo> <content src="./uploaded/images/Penguins.jpg"/> </photo> <photo> <content src="./uploaded/images/tux.png"/> </photo> <photo> <content src="./uploaded/images/Jellyfish.jpg"/> </photo> <photo> <content src="./uploaded/images/Chrysanthemum.jpg"/> </photo> <photo> <content src="./uploaded/images/Tulips.jpg"/> </photo> <photo> <content src="./uploaded/images/Koala.jpg"/> </photo> </photos> </gallery> Here is the script: Code: [Select] <?php $path_to_image_dir = "./uploaded/images"; // path to your image directory $xml_string = <<<XML <?xml version="1.0" encoding="UTF-8"?> <gallery> </gallery> XML; $xml_generator = new SimpleXMLElement($xml_string); if ( $handle = opendir( $path_to_image_dir ) ) { while (false !== ($file = readdir($handle))) { if ( is_file($path_to_image_dir.'/'.$file) ) { $image = $xml_generator->addChild('content'); $image->addattribute('src', $path_to_image_dir.'/'.$file); } } closedir($handle); } header("Content-Type: text/xml"); echo $xml_generator->asXML(); ?> I used this code $limit = 120; if (strlen($summary) > $limit) $summary = substr($summary, 0, strrpos(substr($summary, 0, $limit), ' . . .')); This code ok in English Language it counts letters fine but it is not work fine if I insert Arabic text. It counts Arabic encoding characters. Hello friends, if i have the following $path = "http://*site*.com"; why can't i write the following $ORGtext= file_get_contents('$path'); it works only if i write $ORGtext= file_get_contents('http://*site*.com'); but i want to put $path instead of the url at file_get_contents is it possible and if yes then how ? thank you Hi all
So I'm back to being a php newbie after not touching the stuff for years....and straight back to being pretty sure I'm just incredibly stupid.
I want to grab the contents of a url onto a string so that I can search the string and use the contents. I think this should work:
<?php $homepage = file_get_contents('http://www.example.com/'); echo "Homepage = " . $homepage; ?>But it doesn't. What am I doing wrong, or could there be an issue with my server that means it's not able to perform this maybe? (It's working fine for everything so far) Or is there another way of achieving this? Thanks!! how do i make this part of the script delete the folder and contents. Code: [Select] <?php $path = "../../gallery/gallery_files/gallery/"; if(isset($_POST['file']) && is_array($_POST['file'])) { foreach($_POST['file'] as $file) { rmdir($path. "/" . $file) or die("Failed to delete file"); } } ?> Hey there, I have an image gallery running on my website right now and I want to give the site admins access to add/delete the images from the different gallery directories. But to be honest I'm totally lost on how to do it lol I know my way around PHP but I've never had to code something to pull up the dir contents with an option to delete the files. I've found scandir which will get the contents of a dir and unlink which deletes a file but I have no clue how to get them to work together, anyone got any ideas or a different function if I'm looking in the wrong direction with those functions. Any help would be much appreciated. Thank you! eg http://www.xxxxxxx.com/index.php?action=viewarti&artid=5 How can I write the content of this link into file. I have a purchase order displaying on a site that the user can edit quantities for. I want them to be able to hit submit and it takes that form and submits it to some code that auto creates a PDF with it. The PDF part is easy, I use DOMPDF to create that and it works fine, I just can't seem to get the values passed through. I can easily pass the values of just the input boxes through the $_POST but I don't want to have to recreate the PDF setup after the post. I'd rather just use some Javascript or something to grab the innerHTML of a <div> element and somehow make it available after the form is submitted so the PHP code can use it to send to DOMPDF. The form submits to itself and I have a piece of code at the top of the page which will create the PDF, I just need the <div> contents and I'm done. How do I get it? I could set it into a hidden variable but the reason I'm not doing that is because for some reason when I assign the big table which contains the whole purchase order into the hidden variable and then try to print it, it comes out all messed up. I don't know why and I'd rather just find another way than attempt to figure that out. How does a web server, with a database connect to the emails it receives? How does the web server get the contents of the emails it receives so I can parse it? Thanks Hi guys. This is my first post here, so excuse me if i am posting this in the wrong forums. I am making a website, and having a huge problem. The script is used for orders of different services. And i am using sessions to store the information through serveral pages. But my problem is that on my final page, where the script sends an email with i can not get it to view the contents of one session. I am using the same session on a different page and it works like a charm. When i try debugging the session it won't print the contents, so i am guessing something is very wrong? My code of the page is bellow, let me know if you need anything else: http://pastebin.com/YHh3bGWS Please help me I have made a file explorer for my site so i can upload files, delete them and rename them. Everythings working fine except when i want to delete a folder. I realise you cant use ftp_delete, coz' that only works for files. I have tried rmdir, and even though I am getting no errors, it seems to do nothing :s So what is the best way to delete a folder and its contents using PHP FTP commands? Thanks guys Hello, I have a simply query that will if a simple two different things: Code: [Select] $cssyn = mysql_query("SELECT `12` FROM `12345` WHERE `abc` = '$_SESSION[number]'"); $cssy = mysql_fetch_row($cssyn); if($cssy[0] == "YES") { echo "Yes"; } else { echo "No"; } The YES or NO result is stipulated earlier on so thats not the problem. But basically before I added the if statement simply would have echoed a table with all the rows etc from another query. However I would like to give the user a choice between echoeing the results into one table or a neater two. I have separated the code for the table and the queries into two separate files but say if I use: file_get_contents then it just prints the HTML and doesnt actually populate it will the queried parts. Nor if I use something like include() then it wont echo anything, which it wouldnt unless called so what I am asking is: Is there a way to get EVERYTHING from the file if the answer is YES or the other file if NO. Hi guys! Could you give me advice about this issue: index.php Code: [Select] <?php $page = $_GET['page']; if ($page == NULL) { $page = "main"; } ?> <div>Header</div> <div> <?php require("view/$page.php");?> </div> <div>Footer</div> main.php Code: [Select] <?php require "func.php"; ?> <form method="post" action="view/add.php"> <input type="text" name="from" value=""><br /> <input type="submit" value="Add"> </form> add.php Code: [Select] <?php require "../func.php"; $from = $_POST['from']; $a = add($from); echo $a; ?> func.php Code: [Select] <?php function add($a) { $c = $a+100; return $c; } ?>I need output result (echo $a) in new windows (target= _blank) and output must be same like with main.php. In other words content of add.php output in index.php This code is just simplified of real code. Thank you in advice I have 2 pages...1 one that has something like 500 checkboxes that list all of the active users in a database. When any number of checkboxes are selected it passes the variables from the checkboxes to the next page, asking for confirmation.
Right now I have the 2nd page displaying numbers for the people, but doesn't display the correct entries.
The code is below, hopefully it helps:
if(isset($_POST['Update'])) //if the update button was pressed { if (isset($_POST['Player_number'])) //checks to see if any checkboxes are selected { //$Player_number = IMPLODE(',',$_POST['Player_number']); //putting the comma in between the array items echo $Player_number; ?> <input type="hidden" name="<?PHP $Player_number; ?>" value="<?PHP $Player_number; ?>"> $Player_number = IMPLODE(',',$_POST['Player_number']); //putting the comma in between the array items $result = mysql_query("SELECT * FROM `players` WHERE `Player_number` = '$Player_number' ORDER BY Player_Last_Name") or die(mysql_error()); while ($row = mysql_fetch_array($result)) //while ($row = mysql_fetch_assoc($result)) { // here is your data echo "FName: ".$row['Player_First_Name']; echo "lName: ".$row['Player_Last_Name']; echo "email: ".$row['player_email']; } foreach($_POST['Player_number'] as $row) { $result = mysql_query("SELECT `Player_First_Name`,`Player_Last_Name`,`player_email`, `Player_number` FROM `players` WHERE `Player_number` = '$Player_number' ORDER BY Player_Last_Name") or die(mysql_error()); $Player_number = IMPLODE(',',$_POST['Player_number']); //putting the comma in between the array items while($rows=mysql_fetch_array($result)) { echo "Player Number: ".$Player_number; //echo "pNumber: ".$row; //echo $rows['Player_First_Name']; ?> <tr bgcolor='<?PHP echo $bkcolor; ?>'> <td width = '20%' height="20"><div align="center"><input type="hidden" name="Player_number[]" value="<?php echo $row; ?>"></div></td> <td width= '20%' headers="20"><div align="center"><?php echo $rows['Player_First_Name']; ?></div></td> <td width= '20%' headers="20"><div align="center"><?php echo $rows['Player_Last_Name']; ?></div></td> <td width= '20%' headers="20"><div align="center"><?php echo $rows['player_email']; ?></div></td> <?PHP }//close of the while loop }//close of the foreach loop }//close of if (isset($_POST['Player_number'])) } //close of (isset($_POST['Update']
|