/    Sign up×
Community /Pin to ProfileBookmark

php preg_match_all

I have a string that contains the following:

[CODE]<div class=”listing”>
<a name=”something”></a>
SOME STUFF HERE
</div>

<div class=”listing”>
<a name=”something”></a>
SOME STUFF HERE
</div>

//more divs of the same type here
[/CODE]

I am trying to match each individual div using preg_match_all, but my regular expression:
<div class=”listing”>[s]*<a name[/s/S]*</div>
returns the ENTIRE html code (since it begins with div class=”listing” and ends with </div>

So, I tried to use the following so that it would return a match that did NOT contain </div> in it:
<div class=”listing”>[s]*<a name[^</div>]*</div>
but that results in NO matches ?

How can I get each <div> separated into an array?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@sstalderDec 16.2008 — Here is some code I used to get those results:

[code=php]<?PHP

$string = '<div class="listing">';
$string .= '<a name="something"></a>';
$string .= 'SOME STUFF HERE 1';
$string .= '</div>';
$string .= '<div class="listing">';
$string .= '<a name="something"></a>';
$string .= 'SOME STUFF HERE 2';
$string .= '</div>';
$string .= '<div class="listing">';
$string .= '<a name="something"></a>';
$string .= 'SOME STUFF HERE 3';
$string .= '</div>';
$string .= '<div class="listing">';
$string .= '<a name="something"></a>';
$string .= 'SOME STUFF HERE 4';
$string .= '</div>';

preg_match_all('/<div class="listing">(.*?)</div>/',$string,$result);
?>
<textarea rows="30" cols="150"><?PHP print_r($result[0]); ?></textarea>[/code]


Returns: http://www.stevestalder.com/reg.php
Copy linkTweet thisAlerts:
@irf2kauthorDec 16.2008 — I dont have the luxury of removing the spaces myself, so I altered the regex to look like this:

<div class="listing">[s]*<a([sS]*?)</div>/

and it worked perfect...[B]THX[/B]

also, the PHP Manual states that preg_match_all returns a multidimensional array, with match[0][0] containing the matched string, and match[0][1] to [0][x] containing any parts that were defined.

BUT, instead, the matches are kept in match[0], and the parts are distributed to match[1]-match[x].... so while match[0][0] returns the matched string, the parts are returned in match[1][0] to match[x][0]...no idea why it works this way ?
Copy linkTweet thisAlerts:
@NogDogDec 16.2008 — $match[0] will be an array of each match for the [i]entire[/i] pattern. $match[1] will be an array of each match for the first sub-pattern in the search (i.e. parenthesized portion), if any; with additional elements for each successive sub-pattern. However, you can alter that by using PREG_SET_ORDER for the optional 4th parameter, in which case [0] is the first match with its elements being the full pattern match followed by each sub-pattern match, then [1]-[n] for each additional match. I find this latter option useful when I'm interested in the sub-pattern matches as it seems to make it easier to keep things organized, at least in my mind.
Copy linkTweet thisAlerts:
@irf2kauthorDec 17.2008 — ahh, i missed the PREG_SET_ORDER on php.net...NOW it makes sense

but i agree, its easier to keep track of things using PREG_SET_ORDER (for me at least)...

thx for the clarification
×

Success!

Help @irf2k spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...