Capture text between tags

Reply

Join Date: Jan 2005
Posts: 294
Reputation: zippee is an unknown quantity at this point 
Solved Threads: 6
zippee's Avatar
zippee zippee is offline Offline
Posting Whiz in Training

Capture text between tags

 
0
  #1
Sep 21st, 2005
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?
Ecommerce-Web-Store.com Building Your e-Business.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 279
Reputation: J_Search is an unknown quantity at this point 
Solved Threads: 6
J_Search J_Search is offline Offline
Posting Whiz in Training

Re: Capture text between tags

 
0
  #2
Sep 21st, 2005
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_
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 294
Reputation: zippee is an unknown quantity at this point 
Solved Threads: 6
zippee's Avatar
zippee zippee is offline Offline
Posting Whiz in Training

Re: Capture text between tags

 
0
  #3
Sep 21st, 2005
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?
Ecommerce-Web-Store.com Building Your e-Business.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 902
Reputation: chrisbliss18 is an unknown quantity at this point 
Solved Threads: 23
chrisbliss18's Avatar
chrisbliss18 chrisbliss18 is offline Offline
Posting Shark

Re: Capture text between tags

 
0
  #4
Sep 21st, 2005
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.
Did we help you? Did we miss the point entirely? Update your thread and let us know.
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 294
Reputation: zippee is an unknown quantity at this point 
Solved Threads: 6
zippee's Avatar
zippee zippee is offline Offline
Posting Whiz in Training

Re: Capture text between tags

 
0
  #5
Sep 21st, 2005
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.
Ecommerce-Web-Store.com Building Your e-Business.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,073
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: Capture text between tags

 
0
  #6
Sep 27th, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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