943,939 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 26230
  • PHP RSS
Sep 21st, 2005
0

Capture text between tags

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 7
Posting Whiz in Training
zippee is offline Offline
294 posts
since Jan 2005
Sep 21st, 2005
0

Re: Capture text between tags

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_
Reputation Points: 27
Solved Threads: 6
Posting Whiz in Training
J_Search is offline Offline
284 posts
since Aug 2005
Sep 21st, 2005
0

Re: Capture text between tags

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?
Reputation Points: 10
Solved Threads: 7
Posting Whiz in Training
zippee is offline Offline
294 posts
since Jan 2005
Sep 21st, 2005
0

Re: Capture text between tags

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.
Reputation Points: 38
Solved Threads: 25
Posting Shark
chrisbliss18 is offline Offline
902 posts
since Aug 2005
Sep 21st, 2005
0

Re: Capture text between tags

That's what I want... Thanks chris. I also found http://www.amk.ca/python/howto/regex/regex.html from google search which may be useful to others.
Reputation Points: 10
Solved Threads: 7
Posting Whiz in Training
zippee is offline Offline
294 posts
since Jan 2005
Sep 27th, 2005
0

Re: Capture text between tags

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:

[PHP]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);
}
}[/PHP]

This can be used with a string like:

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

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

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

or even the action url:

[PHP]$match = ExtractString($html_content, 'action="', '"');[/PHP]

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
Last edited by digital-ether; Sep 27th, 2005 at 4:07 am. Reason: typo
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Aug 24th, 2011
0
Re: Capture text between tags
Thank you so much, it saved a lot of time for me.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tahiryasin is offline Offline
1 posts
since Aug 2011
Nov 3rd, 2011
0
Re: Capture text between tags
Thank you very much, it saved a lot of time for me too.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jpdeleon is offline Offline
4 posts
since Nov 2011

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: Show time taken on top of page
Next Thread in PHP Forum Timeline: Transfer Longblob from one mysql table to another





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


Follow us on Twitter


© 2011 DaniWeb® LLC