473,549 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

about preg_match_all statement

118 New Member
hi,

i write the below code to capture the images from the website when i submit the url.In the same way i want to capture the Text information from the website.plz tell that whats the code for that.plz help me.


[php]

<?php

$content= file_get_conten ts($url);
preg_match_all( "/<img(.*)src=(\" |')(.*)(\"|\')( .*)[\/]?>/siU", $content, $match, PREG_PATTERN_OR DER);

echo "<b>Capture Images :</b><br>";
echo "<br>";
print_r($match[0]);
echo "<br>";
echo "<br>";
echo "<b>Capture Images URLS :</b><br><br>";
preg_match_all( "/<img(.*)src=(\" |')(.*)(\"|\')( .*)[\/]?>/siU", $content, $match, PREG_PATTERN_OR DER);
print_r($match[3]);
[/php]
Jul 27 '08 #1
10 2673
pbmods
5,821 Recognized Expert Expert
Heya, Swethak.

What is your code doing now that is different from what you want it to do?
Jul 27 '08 #2
swethak
118 New Member
Heya, Swethak.

What is your code doing now that is different from what you want it to do?

It is for capture the images from website. i Want capture the text information from website.
Jul 28 '08 #3
Gulzor
27 New Member
If you are working with PHP5, you can use the DOM API for that.

Adapt this to your needs :
[php]
<?php
$htmlString = file_get_conten ts('url_or_path _to_html_file') ;
$htmlDoc = DOMDocument::lo adHTML($htmlStr ing);
$xpath = new DOMXPath($htmlD oc);

/* fetch the content of all <p> tags */
$pNodesList = $xpath->query('//p');
for ($i=0; $i<$pNodesList->length; $i++) {
$pNode = $pNodesList->item($i);
echo $pNode->nodeValue, "\n";
}

?>
[/php]

May not be the best method but I prefer handling HTML document with the DOM API instead of knocking my head on the walls with regex :P
Jul 28 '08 #4
swethak
118 New Member
If you are working with PHP5, you can use the DOM API for that.

Adapt this to your needs :
[php]
<?php
$htmlString = file_get_conten ts('url_or_path _to_html_file') ;
$htmlDoc = DOMDocument::lo adHTML($htmlStr ing);
$xpath = new DOMXPath($htmlD oc);

/* fetch the content of all <p> tags */
$pNodesList = $xpath->query('//p');
for ($i=0; $i<$pNodesList->length; $i++) {
$pNode = $pNodesList->item($i);
echo $pNode->nodeValue, "\n";
}

?>
[/php]

May not be the best method but I prefer handling HTML document with the DOM API instead of knocking my head on the walls with regex :P
I used like that way i got below errors.plz tell that whats the mistake.

Warning: DOMDocument::lo adHTML() [function.DOMDoc ument-loadHTML]: htmlParseEntity Ref: expecting ';' in Entity, line: 34 in C:\wamp\www\tes t\textdata.php on line 3

Warning: DOMDocument::lo adHTML() [function.DOMDoc ument-loadHTML]: htmlParseEntity Ref: expecting ';' in Entity, line: 34 in C:\wamp\www\tes t\textdata.php on line 3

Warning: DOMDocument::lo adHTML() [function.DOMDoc ument-loadHTML]: htmlParseEntity Ref: expecting ';' in Entity, line: 34 in C:\wamp\www\tes t\textdata.php on line 3

Warning: DOMDocument::lo adHTML() [function.DOMDoc ument-loadHTML]: htmlParseEntity Ref: expecting ';' in Entity, line: 34 in C:\wamp\www\tes t\textdata.php on line 3
Jul 28 '08 #5
Gulzor
27 New Member
These are "just" warnings resulting in wrong or unsupported html entities or something else. It's just impossible to parse a html document without getting these warnings...

If your texts are not between <p></p>, you can replace //p by //td. Like I said, you need to adapt it to your needs.
Jul 28 '08 #6
swethak
118 New Member
These are "just" warnings resulting in wrong or unsupported html entities or something else. It's just impossible to parse a html document without getting these warnings...

If your texts are not between <p></p>, you can replace //p by //td. Like I said, you need to adapt it to your needs.

If i use the condition as if the data is in between <p> tags it shows the data otherwise it didn't give any error.How i use the condition for that .Plz help me.
Jul 28 '08 #7
Gulzor
27 New Member
If i use the condition as if the data is in between <p> tags it shows the data otherwise it didn't give any error.How i use the condition for that .Plz help me.
I don't understand what your problem is now... not only <p> tag hold texts. <li>, <td>, <span> and more also do.
Jul 28 '08 #8
mobs
1 New Member
Say that I just wanted to retrieve the number 30735 from the following code, how would you go about doing that?

Expand|Select|Wrap|Line Numbers
  1. <a href="/?item=30735">River Runner</a>
Aug 6 '08 #9
pbmods
5,821 Recognized Expert Expert
Heya, Mobs. Welcome to Bytes!

The only part that we really care about is:
Expand|Select|Wrap|Line Numbers
  1. <a href="/?item=30735
Now, we have to make a couple of assumptions:
  • The URL might have a path and/or other query variables prepended. E.g.:
    Expand|Select|Wrap|Line Numbers
    1. <a href="/path/to/some.php?file=test&item=123456"
  • The URL might have some stuff after it. E.g.,:
    Expand|Select|Wrap|Line Numbers
    1. <a href="/?item=654321&amp;visitor=1"
  • The anchor tag might have attributes before the href attribute. E.g.,:
    Expand|Select|Wrap|Line Numbers
    1. <a target="_blank" href="/?item=13579"

We are going to assume that the tag is well-formed (ends with a '>' and the href attribute is properly-quoted with any quotes inside of it percent- or ampersand-escaped).

With that in mind, we need to be able to skip over anything we don't care about and focus only on what we want:

Expand|Select|Wrap|Line Numbers
  1. /<a[^>]*href="[^"]+item=(\d+)/
  2.  
This should be enough to harvest item IDs from anchor tags on the page.
Aug 6 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4687
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&amp;" preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out); $out = 3456789 preg_match ("|product=(\d{5,10})&amp;|i", $html, $out);
3
7287
by: Han | last post by:
I know this is possible (because preg can do almost anything!), but can't get a handle on the syntax. I have an HTML string: <font size="3"><a href="http://www.example.com?product=3456789&amp;company=3528"> Mickey Mouse</a></font><br><img width="200" height="200" border="0" src="http://www.example.com/images/3456789.jpg"><b>$8.00</b>
2
2377
by: Han | last post by:
The following pattern (which is one subpattern in a string of several) looks for the following $xxx,xxx.xx (with the dollar sign) or xxx,xxx.xx (space in replace of missing dollar sign) It works great WITHOUT a ? quantifier
0
1472
by: petrovitch | last post by:
While using the following loop to extract images from the google search engine I discovered that preg_match_all works much faster parsing small strings in a loop than extracting all of the urls at once from a much larger string. This surprised me because I expected the preg_match_all to perform the task much faster. Why is this, and is...
10
2026
by: greatprovider | last post by:
i'm starting with a string such as "Na**3C**6H**5O**7*2H**20" im attempting to match all **\d+ ...once i can match all the double asterix \d i intend to wrap the \d in "<sub>" tags for display purposes. i have been trying to write the correct pattern for 2 weeks now without any success...i can get preg_replace() to work, with several...
1
1937
by: ngmr80 | last post by:
Hi, I'm experiencing a problem when trying to capture substrings with preg_match_all() from strings like "set('Hello','World')" using the following Regular Expression (PERL syntax): "/^set\((?:'(+)'(?(?!\)),))+\)$/"
6
2436
by: PaulB | last post by:
Hello, as a newbie I'm requesting some help in understanding the regular expression below preg_match_all("|<tr(.*)</tr>|U",$table,$rows); Would anybody please just run through ("|<tr(.*)</tr>|U" from left to right and tell me what it means? Thanks Paul Bird
2
1237
by: swethak | last post by:
hi, In the below mentioned statement i did understand if subject part matchs to content part then it display the output.But i want know about the subject part as "|<+>(.*)</+>|U" .And what purpose it is used and also about each operator in subject part what pupose it is used.Could you please explain about that. ...
2
8222
loriann
by: loriann | last post by:
hi, I have a problem with preg_match function returning empty arrays for my wonderful regexes. However, I can't see what I am doing wrong - maybe one of you could help? I'm loading the source of a website into variable and then using preg_match_all to extract all occurrences of a string. it looks like this (where source code is loaded...
0
7459
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7967
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7485
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7819
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.