I was wondering if there is a way to layer my preg_matches?

I have a code like this

preg_match("/\<div class=\"dealsListS\"\>(.*?)\<\/div\>(.*)\<\/form\>.*?\<\/div\>/is", $res[1], $bycontainer );

preg_match_all("/\<h2>\s*(\<div.*?\<\/div>\s*)?(.*?)\<\/h2\>/is", $bycontainer[0], $level1 );
preg_match_all("/\<div class=\"thumbWrapper\"\>(.*?)\<\/div\>/is", $level1[0], $matches);
foreach($matches[0] as $travelzooTitles)
	echo "<div class='titles' id='travelzooTitles'>".$travelzooTitles."</div>";

I'm getting error, but can I even do something like that?

Lets say I have contents like this

<div class="dealsListS">
some contents

This is where I have the first preg match to get this div tag.

Then I wanted to obtain something within dealsListS

<div class="dealsListS">

Then I wanted to get more inside dealsListS

<div class="dealsListS">
<div class="thumbWrapper">images</div>

I wanted to have different layers because I wanted to output or set these two objects differently. One would be context, other would be images so I can style them however I want to.

Thank you

Recommended Answers

All 4 Replies

It is not clear which part you want eventually to match. Do you want to replace css classes?
The first three preg_match statements are better put in one single statement to clarify the code. But first tell us which part you want to have extracted.

sorry for the confusion,

I wanted to extract titles and thumbs, but im not sure how to put it in 1 foreach and set thumbs and titles in a different variable?

Is that possible?


I assume that with "title" you refer to the contents of the h2 tag.

$text = '<div class="dealsListS">
<div class="thumbWrapper">images</div>

preg_match( '~<div class="dealsListS">\s*<h2>(.*?)</h2>\s*<div class="thumbWrapper">(.*?)</div>\s*</div>~is'
	, $text, $matches 
$title = $matches[1];
$thumbs = $matches[2];

I got it. Thanks. I did two foreach loops and set them into an array and then outputted it out into a single string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.