Reputation: 7416
I was wondering if I had an HTML page, how would I delete an element with php, by the name or id of it. so I could have HTML code like this
<div>
<span id='todelete' name='todelete'>This should be deleted</span>
<?php
delete('todelete');
?>
</div>
or something like that, and the web browser would just show an empty div without the span. Is that possible, or is it more useful to just use javascript for this.
Basically, what I'm saying is before the page is compiled, can you delete elements from it, based on some conditionals, not after the page has loaded
Upvotes: 1
Views: 19948
Reputation: 270
Just an idea...
have you thought about PHP XPath Query?
// CSS not important
<style>
div { border:1px solid rgba(255,0,0,0.5); background:rgba(255,0,0,0.1) }
span{ border:1px solid rgba(0,255,0,0.5); margin:0px 2px; padding:0 20px;
background:rgba(0,255,0,0.5)}
</style>
// PHP your HTML, in this example a string.
<?php
$string = '
<div>
<span id="keep"> John </span>
<span id="keep"> Susane </span>
<span id="todelete"> Tree </span>
<span id="keep"> Karin </span>
<span id="keep"> Jack </span>
</div>
';
echo $string;
// PHP start new Class DOMDocument and make the query
$doc = new DOMDocument;
@$doc->loadHTML($string);
$xpath = new DOMXPath($doc);
$found = $xpath->query("//span[contains(@id, 'todelete')]");
// more PHP I can't explain this well. I hope someone else can?
// But The query found the span that contains attribute id with 'todelete'
// I guess it output it as HTML
$element = $found[0]->ownerDocument->saveXML($found[0]);
/* ##### <span id="todelete"> Tree </span> ##### */
echo '<p>delete:' . $element . '<p>';
//Finally PHP Replace in your string what you found with nothing
echo str_replace($element, '' , $string);
?>
I hope its useful. XPath is very powerful and often forgotten. Give it a try. I hope it's enough for your needs. you can copy all the code above and paste it in a .php document.
Upvotes: 4
Reputation: 135
You can't use php for removing DOM elements. You need to do it with javascript or jQuery.
With javascript it will be so
var element = document.getElementById("todelete");
element.parentNode.removeChild(element);
And with jQuery (after including the library)
$(document).ready(function() {
$("#todelete").remove();
});
Upvotes: 3
Reputation: 53
You can't really use php to delete the html once it has been rendered, but you can use conditions in your php code to selectively render certain sections of html depending on the outcome of tests you perform in php...
Upvotes: 2
Reputation: 8102
It appears you are getting confused between server side and client side scripting languages.
PHP is a server side language meaning that once it has been compiled that's it, it can't be changed dynamically in the users browser.
JavaScript on the other hand is a client side scripting language and can manipulate what is on the page dynamically.
Therefore what you are asking to do is impossible using pure PHP once the page has been rendered.
You can use AJAX technology to call a PHP script from within a page already in a browser using JavaScript. You can then use the PHP script being called by the AJAX request to return something that you can use to update a particular section of your page, but there is no pure PHP way that doesn't involve JavaScript at all to do this.
Upvotes: 6