...

View Full Version : PHP insert using preg_match



stormcancer
11-07-2010, 04:36 PM
I am trying to insert a div element into my websites content using preg_match, however I am new to regular expressions and preg_match and I just cant get it to work.

My content would put out:

<style type='text/css'>
.gallery-item {width: 33%;}
</style>
<div class='gallery gallery1'>
<script type="text/javascript">
...
</script>
<dl class="gallery-item">
<dt class="gallery-icon">
...
</dt>
</dl><dl class="gallery-item">
<dt class="gallery-icon">
...
</dt>
</dl><dl class="gallery-item">
<dt class="gallery-icon">
...
</dt>
</dl><dl class="gallery-item">
<dt class="gallery-icon">
...
</dt>
</dl><dl class="gallery-item">
<dt class="gallery-icon">
...
</dt>
</dl><div style="clear: both;"></div>

What I am trying to do is to find the first </dl><dl class="gallery-item"> and insert <div class="hide"> resulting in: </dl><div class="hide"><dl class="gallery-item"> but I cant get it to work.

My PHP code looks like this:

// Search for the first dl class
$searchfor = '/(<\/dl>)(<dl class="gallery-item">)/';

// Insert hidden div
$replacewith = '$1<div class="hidden">$2';

// Replace it, but just once
$content = preg_replace($searchfor, $replacewith, $content, 1);

// Return the result
return $content;

Any help or guidance on what I am doing wrong would be much appreciated.

poyzn
11-07-2010, 04:40 PM
may be you should try:

$content = preg_replace('/<\/dl><dl class=\"gallery\-item\">/', '</dl><div class="hidden"><dl class="gallery-item">', $content);

instead of


// Search for the first dl class
$searchfor = '/(<\/dl>)(<dl class="gallery-item">)/';

// Insert hidden div
$replacewith = '$1<div class="hidden">$2';

// Replace it, but just once
$content = preg_replace($searchfor, $replacewith, $content, 1);

stormcancer
11-07-2010, 05:00 PM
thanks poyzn for the quick reply. Unfortunately it does not work.
I changed my php to:


// Find the first dl><dl and insert a div element.
$content = preg_replace('/<\/dl><dl class=\"gallery\-item\">/', '</dl><div class="hidden"><dl class="gallery-item">', $content);

// Return the result
return $content;

poyzn
11-07-2010, 05:03 PM
oh, sorry, try this


$content = preg_replace('/<\/dl>.<dl class=\"gallery\-item\">/sU', '</dl><div class="hidden"><dl class="gallery-item">', $content);
or this


$content = preg_replace('/<\/dl>[^<]+<dl class=\"gallery\-item\">/m', '</dl><div class="hidden"><dl class="gallery-item">', $content);

stormcancer
11-07-2010, 05:23 PM
thanks for your help mate but none of them seem to work.

poyzn
11-07-2010, 05:34 PM
have forgotten "+" :)


$content = preg_replace('/<\/dl>.*<dl class=\"gallery\-item\">/sU', '</dl><div class="hidden"><dl class="gallery-item">', $content);

checked

stormcancer
11-07-2010, 05:43 PM
very strange. It just refuses to work.

poyzn
11-07-2010, 05:49 PM
strange, check your $content variable.

create php-file with this code and try to open it

<?php
$content = '</dl>
<dl class="gallery-item">
<dt class="gallery-icon">
...
</dt>
</dl>
<dl class="gallery-item">
<dt class="gallery-icon">
...
</dt>
</dl>';
echo $content = preg_replace('/<\/dl>.*<dl class=\"gallery\-item\">/sU', '</dl><div class="hidden"><dl class="gallery-item">', $content);

I'm checking with this part of code.

stormcancer
11-07-2010, 07:34 PM
Does it make a difference if there is a new line between </dl> and <dl class...?

I just noticed that there is actually no new line between them.

MattF
11-08-2010, 01:46 AM
Shouldn't make any difference whatsoever. You could change that regex to a str_replace though.



$content = str_replace('<dl class="gallery-item">', '<div class="hidden"><dl class="gallery-item">', $content);


How are you inserting the closing </div> btw?

stormcancer
11-08-2010, 11:50 AM
Thanks that worked perfectly.
I found it strange because if I would for example look for the first character in the content and wrap it in a span element it works perfectly but for some reason it did not wanted to work for this example.

I close the div element with:


if(in_category('fashion')) { echo'</div>'; }
Since I only apply this filter to specific content.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum