473,609 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preg_match_all returns an empty array

loriann
3 New Member
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 into $page variable):
Expand|Select|Wrap|Line Numbers
  1. preg_match ('/<div\s+?id="srNum_\d+?"\s+?class="number">(.*?)<\/div>/', $page, $results);
a sample string which should be matched by the above is:
Expand|Select|Wrap|Line Numbers
  1. <div id="srNum_0" class="number">1.</div>
Funny that preg_match_all also doesn't work if I give a direct regex to match the line above:
Expand|Select|Wrap|Line Numbers
  1. preg_match_all ('/<div id="srNum_0" class="number">1\.<\/div>/', $page, $results);
On the other hand this one works fine:
Expand|Select|Wrap|Line Numbers
  1. preg_match_all ('/<title>(.*?)<\/title>/',$page, $results);
Tried hard to find the answer, to add backslashes, change single quotes to double quotes etc.
Can you see a mistake? Should any characters be escaped?

cheers
Sep 28 '08 #1
2 8246
Atli
5,058 Recognized Expert Expert
Hi.

I tried your regular expression and it worked fine on my test server.

This is the code I used:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $str = ' <div id="srNum_0" class="number">Contents of the div</div>';
  3. $regex = '/<div\s+?id="srNum_\d+?"\s+?class="number">(.*?)<\/div>/';
  4.  
  5. if(preg_match_all($regex, $str, $patterns))
  6. {
  7.     echo "Success!\n". print_r($patterns, true);
  8. }
  9. else
  10. {
  11.     echo "Failed!";
  12. }
  13. ?>
  14.  
Which gave me:
Expand|Select|Wrap|Line Numbers
  1. Success!
  2. Array
  3. (
  4.     [0] => Array
  5.         (
  6.             [0] => 
  7. Contents of the div
  8.  
  9.         )
  10.  
  11.     [1] => Array
  12.         (
  13.             [0] => Contents of the div
  14.         )
  15.  
  16. )
  17.  
Could it be that your are simply reading the return array incorrectly?

I'm running PHP 5.2.4 by the way.
Sep 29 '08 #2
loriann
3 New Member
Hi,

thank you for your reply.
It turns out that the problem was not actually the regexps itself. As you said this should be working I started to look for a problem elsewhere. It looks that the page content wasn't loaded properly into the variable - now it's sorted out and regexes are returning what they are supposed to.

Thank you for help.

cheers
Oct 1 '08 #3

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

Similar topics

4
3797
by: Han | last post by:
Determining the pattern below has got my stumped. I have a page of HTML and need to find all occurrences of the following pattern: score=9999999999&amp; The number shown can be 5-10 characters in length. I would like to extract only the number, stripping off the "score=" and "&amp;".
2
4690
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
7290
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>
5
6090
by: Han | last post by:
Using preg_match_all, I need to capture a list of first and last names plus an optional country code proceeding them. For example: <tr><td>AU</td><td>Jane Smith</td></tr> <tr><td></td><td>Bill Johnson</td></tr> <tr><td>GB</td><td>Larry Brown</td></tr> <tr><td>US</td><td>Mary Jordon</td></tr> <tr><td></td><td>Peter Jones</td></tr>
2
2380
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
2
3917
by: kevinC | last post by:
Hello, I'm trying to parse out the properties of a class definition from a css file and am running into issues trying to write the reg. expression: h1 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 16px; font-weight: bold; color: #003399;
10
2031
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 simple patterns but when i use preg_match_all, i either get unintended results or incomplete matches.
0
1061
by: dimpie | last post by:
I've created a form where visitors can input more than one value. They have to use a space as a delimiter. For example: 'mozart beethoven grieg'. And redundant spaces should be converted to one space. The only exception is when a value is followed by a comma and a space. Then the search string should be taken literally. So if for example you filled in: mozart amadeus, then this should be split into: 'mozart' and 'amadeus'. But if you...
1
1941
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\((?:'(+)'(?(?!\)),))+\)$/"
0
8139
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8091
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8232
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7024
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6064
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4032
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.