can I get a title from an html header tag?

Thread Solved
Reply

Join Date: Mar 2007
Posts: 156
Reputation: Dsiembab is an unknown quantity at this point 
Solved Threads: 2
Dsiembab's Avatar
Dsiembab Dsiembab is offline Offline
Junior Poster

can I get a title from an html header tag?

 
0
  #1
Jun 23rd, 2007
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 <h1> 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
  1. <?php
  2. $y ="<h1>some content</h1> this is about some content";
  3. ?>
Now in the php page I have
  1. <?php include("phpfile1.php"); ?>
  2. //this includes the doctype and the <html> and <head> tag
  3. <?php include("content.php"); ?>
  4. <?php $t=strip_tags($y);
  5. echo "<meta=\"description\" description=\"".$t."\">";
  6. ?>
  7. //this puts a description of my content
  8. <?php include("top.php"); ?>
  9. //this includes the </head>, <body> and header html and what not
  10. <?php echo $y; ?>
  11. <?php include("footer.php"); ?>
  12. // 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
Last edited by Dsiembab; Jun 23rd, 2007 at 4:06 am.
current personal projects The H8ers Club
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: can I get a title from an html header tag?

 
0
  #2
Jun 23rd, 2007
There might be, but it probably is grounded on regex
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 156
Reputation: Dsiembab is an unknown quantity at this point 
Solved Threads: 2
Dsiembab's Avatar
Dsiembab Dsiembab is offline Offline
Junior Poster

Re: can I get a title from an html header tag?

 
0
  #3
Jun 23rd, 2007
I finally figured it out here is the code that works for me
  1. $g=strpos($y, "</h1>",1);
  2. $i=strpos($y,"<h1>",1);
  3. $f=substr($y,$i,$g);
  4. $title=strip_tags($f);
  5. echo "<title>".$title."</title>"."\n";
Hey thank for your reference it kind of blew my mind. I hope this helps sme people
Last edited by Dsiembab; Jun 23rd, 2007 at 10:04 am.
current personal projects The H8ers Club
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: can I get a title from an html header tag?

 
0
  #4
Jun 23rd, 2007
If you're serious about html parsing etc, regex is the only way to fly.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 156
Reputation: Dsiembab is an unknown quantity at this point 
Solved Threads: 2
Dsiembab's Avatar
Dsiembab Dsiembab is offline Offline
Junior Poster

Re: can I get a title from an html header tag?

 
0
  #5
Jun 24th, 2007
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
current personal projects The H8ers Club
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: can I get a title from an html header tag?

 
1
  #6
Jun 24th, 2007
Originally Posted by Dsiembab View Post
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 <h1> and it probably works just fine. But what happens for all the other header tags <h2> ...<h3> ...<h329>.

Of course you could still code that without regular expressions, but it is more of a pain.
Last edited by iamthwee; Jun 24th, 2007 at 9:30 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 156
Reputation: Dsiembab is an unknown quantity at this point 
Solved Threads: 2
Dsiembab's Avatar
Dsiembab Dsiembab is offline Offline
Junior Poster

Re: can I get a title from an html header tag?

 
0
  #7
Jun 24th, 2007
Originally Posted by iamthwee View Post
<h2> ...<h3> ...<h329>.
wow <h329> is that even visible? or is that xml? just kidding, hey thanks appreciate it.
current personal projects The H8ers Club
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC