944,159 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 5619
  • PHP RSS
Jun 23rd, 2007
0

can I get a title from an html header tag?

Expand Post »
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
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $y ="<h1>some content</h1> this is about some content";
  3. ?>
Now in the php page I have
PHP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 18
Solved Threads: 2
Junior Poster
Dsiembab is offline Offline
156 posts
since Mar 2007
Jun 23rd, 2007
0

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

There might be, but it probably is grounded on regex
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jun 23rd, 2007
0

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

I finally figured it out here is the code that works for me
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 18
Solved Threads: 2
Junior Poster
Dsiembab is offline Offline
156 posts
since Mar 2007
Jun 23rd, 2007
0

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

If you're serious about html parsing etc, regex is the only way to fly.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jun 24th, 2007
0

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

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
Reputation Points: 18
Solved Threads: 2
Junior Poster
Dsiembab is offline Offline
156 posts
since Mar 2007
Jun 24th, 2007
1

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

Click to Expand / Collapse  Quote originally posted by Dsiembab ...
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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Jun 24th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by iamthwee ...
<h2> ...<h3> ...<h329>.
wow <h329> is that even visible? or is that xml? just kidding, hey thanks appreciate it.
Reputation Points: 18
Solved Threads: 2
Junior Poster
Dsiembab is offline Offline
156 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Need help with If and While Clauses
Next Thread in PHP Forum Timeline: how do i solve the below error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC