[ create a new paste ] login | about

Link: http://codepad.org/wfzsmpAW    [ raw code | output | fork ]

PHP, pasted on Jan 12:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?
$str = 'Test 123 <comment>Some comment here</comment> abc 456';
$dom = new DOMDocument;
// Wrap $str in a div, so we can easily extract the HTML from the DOMDocument
@$dom->loadHTML("<div id='string'>$str</div>");  // It yells about <comment> not being valid
$comments = $dom->getElementsByTagName('comment');
foreach($comments as $c){
   $c->parentNode->removeChild($c);
}
$domXPath = new DOMXPath($dom);
// $dom->getElementById requires the HTML be valid, and it's not here
// $dom->saveHTML() adds a DOCTYPE and HTML tag, which we don't need
echo $domXPath->query('//div[@id="string"]')->item(0)->nodeValue; // "Test 123  abc 456"


Output:
1
Test 123  abc 456


Create a new paste based on this one


Comments: