views:

153

answers:

3

How to get a html page source code without htl tags? For example:

<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<meta http-equiv="content-language" content="hu"/> 
<title>this is the page title</title>
<meta name="description" content="this is the description" />
<meta name="keywords" content="k1, k2, k3, k4" />
start the body content
<!-- <div>this is comment</div> -->
<a href="open.php" title="this is title attribute">open</a>
End now one noframes tag.
<noframes><span>text</span></noframes>
<select name="select" id="select"><option>ttttt</option></select>
<div class="robots-nocontent"><span>something</span></div>
<img src="url.png" alt="this is alt attribute" />

I need this result:

this is the page title this is the description k1, k2, k3, k4 start the body content this is title attribute open End now one noframes tag. text ttttt something this is alt attribute

I need too the title and the alt attributes. Idea?

A: 

This cannot be done in an automated way. PHP cannot know which node attributes you want to omit. You'd either had to create some code that iterates over all attributes and textnodes which you can feed a map, defining when to use a node's content or you just pick what you want with XPath one by one.

An alternative would be to use XMLReader. It allows you to iterate over the entire document and define callbacks for the element names. This way, you can define what to do with what element. See

Gordon
A: 

You could do it with a regex.

$regex = '/\<.\>/';

would be a very simple start to remove anything with < and > around it. But in order to do this, you're going to have to pull in the HTML as a file_get_contents() or some other function that will turn the code into text.

Addendum:

If you want individual attributes pulled as well, you're going to have to write a more complex regex to pull that text out. For instance:

$regex2 = '/\<.(?<=(title))(\=\").(?=\")/';

Would pull out (I think... I'm still learning RegEx) any text between < and title=", assuming you had no other matching expressions before title. Again, this would be a pretty complicated regex process.

dclowd9901
he wants to preserve attribute content. if he wanted to remove elements, he could just use strip_tags
Gordon
Right, but it's just a matter of matching the attributes he wants. It's not easy, but it's certainly possible to do it in an automated fashion through PHP.
dclowd9901
Handcrafting this is not automated. Also using regex for parsing XML is the way to madness. Regex have no clue about nodes or attributes. XML Parsing is not what Regex is for.
Gordon
I completely agree. I'd personally probably use jQuery or something else if I really wanted to do such a thing, but nevertheless, I'm trying to give him an answer to his question.
dclowd9901
A: 

My solution is a bit more complicate but it worked fine for me.

If you are sure that you have XHTML, you can simply consider the code as XML (but you have to put everything in a proper wrapping).

Then with XSLT you can define some basic templates that do what you need.

Giovanni Di Milia