Hi, is there any php function can read text between tags or quotes.

For example, I have <h1>This is heading 1</h1> and "This is text in Quote", how can I use php to read the text between <h1></h1> tags or the text between quotes? I know I can use substr but is there any function that can directly read the text?

Recommended Answers

All 7 Replies

zippee,

I would also like to know how to do this.
---
I don't know the answer, but I think I was looking for the exact same thing. I wanted a php script that would visit http://www.whatismyip.com and extract my IP address which was written in the HTML as follows: <h1>Your IP Is xxx.xxx.xxx.xxx</h1>

Is this similar to what you're looking for?

J_

No. Your application can be solved using substr alone.

What I try to do is when I use fget() to read a file or a webpage, I can retrieve text in heading. For example, when I used fget("http://www.whatismyip.com") to read the web page, I can get <h1>Your IP - xxx.xxx.xxx.xx</h1>. What I want to do is the php script can read "Your IP - xxx.xxx.xxx.xx" directly for me and stored in db. But since I'm not only want to read the heading text, but also <title> etc, I have to write many line to retrive each data. If there is any function can read something within a specifc tags (say between <h1></h1> or <title></title>) then it would be great.

Anyone?

zippee, you'll want to learn how to use Regular Expressions in PHP. You can find a good Regular Expression tutorial that includes examples for what you are wanting here.

generally, its better to user string functions, like substr and strpos rather then regular expressions (whenever you can) as the regular expression will take longer to evaluate.

The following function (http://us3.php.net/preg_match) will do the trick:

function ExtractString($str, $start, $end)
{
   $str_low = strtolower($str);
   $pos_start = strpos($str_low, $start);
   $pos_end = strpos($str_low, $end, ($pos_start + strlen($start)));
   if ( ($pos_start !== false) && ($pos_end !== false) )
   {
       $pos1 = $pos_start + strlen($start);
       $pos2 = $pos_end - $pos1;
       return substr($str, $pos1, $pos2);
   }
}

This can be used with a string like:

$html_content = '
<body>
<form method="post" action="script.php">
</form>
</body>';

to find the string between <body> and </body>

$match = ExtractString($html_content, '<body>', '</body>');

or even the action url:

$match = ExtractString($html_content, 'action="', '"');

note: the function searches for the end tag in a position after the start tag, so it will work in the second example fine.

www.digital-ether.com

Thank you so much, it saved a lot of time for me.:cool:

Thank you very much, it saved a lot of time for me too.

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.