| | |
Capture text between tags
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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?
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.
•
•
Join Date: Aug 2005
Posts: 279
Reputation:
Solved Threads: 6
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_
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?
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.
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
Don't like the answers you are getting?
Did you try searching?
Clean up and optimize Windows 2000/XP
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.
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
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
![]() |
Similar Threads
- How do I excute Programs IN VB and Capture Text (Visual Basic 4 / 5 / 6)
- Showing HTML tags from database (JSP)
- Tkinter Text tags (Python)
Other Threads in the PHP Forum
- Previous Thread: problem with sessions
- Next Thread: generating txt using php
| Thread Tools | Search this Thread |
# 5.2.10 alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dropdown dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube






