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 
$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

Recommended Answers

All 6 Replies

Member Avatar for iamthwee

There might be, but it probably is grounded on regex

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

Member Avatar for iamthwee

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

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

Member Avatar for iamthwee

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.

commented: thanks +1

<h2> ...<h3> ...<h329>.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.