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): -
preg_match ('/<div\s+?id="srNum_\d+?"\s+?class="number">(.*?)<\/div>/', $page, $results);
a sample string which should be matched by the above is: -
<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: -
preg_match_all ('/<div id="srNum_0" class="number">1\.<\/div>/', $page, $results);
On the other hand this one works fine: -
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
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: -
<?php
-
$str = ' <div id="srNum_0" class="number">Contents of the div</div>';
-
$regex = '/<div\s+?id="srNum_\d+?"\s+?class="number">(.*?)<\/div>/';
-
-
if(preg_match_all($regex, $str, $patterns))
-
{
-
echo "Success!\n". print_r($patterns, true);
-
}
-
else
-
{
-
echo "Failed!";
-
}
-
?>
-
Which gave me: -
Success!
-
Array
-
(
-
[0] => Array
-
(
-
[0] =>
-
Contents of the div
-
-
)
-
-
[1] => Array
-
(
-
[0] => Contents of the div
-
)
-
-
)
-
Could it be that your are simply reading the return array incorrectly?
I'm running PHP 5.2.4 by the way.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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&
The number shown can be 5-10 characters in length. I would like to extract
only the number, stripping off the "score=" and "&".
|
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&"
preg_match_all ("|product=(\d{5,10})&|i", $html, $out);
$out = 3456789
preg_match ("|product=(\d{5,10})&|i", $html, $out);
|
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&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>
|
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>
|
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
| |
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;
|
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.
|
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...
|
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\((?:'(+)'(?(?!\)),))+\)$/"
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |