954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

can I get a title from an html header tag?

Good morning,
Is there a php function that will take the text out of a tag in a variable that has HTML in it? What I want to do is get the text from in-between the tags and use it as a title in my meta tags.
Let's say I have all my content in separate .php files like this

<?php 
$y ="<h1>some content</h1> this is about some content";
?>

Now in the php page I have

<?php include("phpfile1.php"); ?> 
//this includes the doctype and the <html> and <head> tag
<?php include("content.php"); ?>
<?php $t=strip_tags($y);
echo "<meta=\"description\" description=\"".$t."\">";
?>
//this puts a description of my content
<?php include("top.php"); ?>
//this includes the </head>, <body> and header html and what not
<?php echo $y; ?> 
<?php include("footer.php"); ?>
// this includes the footer html and the </body and </html>tags

Is their any easier way? Or is this a decent way? Just trying to make it easier to add new content to my pages. thanks

Dsiembab
Junior Poster
156 posts since Mar 2007
Reputation Points: 18
Solved Threads: 2
 

There might be, but it probably is grounded on regex

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

I finally figured it out here is the code that works for me

$g=strpos($y, "</h1>",1);
$i=strpos($y,"<h1>",1);
$f=substr($y,$i,$g);
$title=strip_tags($f);
echo "<title>".$title."</title>"."\n";

Hey thank for your reference it kind of blew my mind. I hope this helps sme people

Dsiembab
Junior Poster
156 posts since Mar 2007
Reputation Points: 18
Solved Threads: 2
 

If you're serious about html parsing etc, regex is the only way to fly.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

god thanks for the help. I don't know maybe I was hit in the head to many times as a child. I think what I'm hearing is, If I parse my content into arrays I can use different functions and switches and all that other funny stuff to manipulate the arrays to do my bidding. Is this so? Thanks, Dave

Dsiembab
Junior Poster
156 posts since Mar 2007
Reputation Points: 18
Solved Threads: 2
 
god thanks for the help. I don't know maybe I was hit in the head to many times as a child. I think what I'm hearing is, If I parse my content into arrays I can use different functions and switches and all that other funny stuff to manipulate the arrays to do my bidding. Is this so? Thanks, Dave

Yes, it is just that regular expressions provide greater flexibility when extracting stuff from html files. In your example you have used the tag and it probably works just fine. But what happens for all the other header tags ... ....

Of course you could still code that without regular expressions, but it is more of a pain.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

... ...

.


wow is that even visible? or is that xml? just kidding, hey thanks appreciate it.

Dsiembab
Junior Poster
156 posts since Mar 2007
Reputation Points: 18
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You