Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question PHP insert using preg_match

    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:
    Code:
    <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:
    PHP Code:
        // 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$content1);
     
        
    // Return the result
        
    return $content
    Any help or guidance on what I am doing wrong would be much appreciated.
    Last edited by stormcancer; 11-07-2010 at 05:27 PM.

  • #2
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    may be you should try:
    PHP Code:
    $content preg_replace('/<\/dl><dl class=\"gallery\-item\">/''</dl><div class="hidden"><dl class="gallery-item">'$content); 
    instead of
    PHP Code:
        // 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$content1); 
    Last edited by poyzn; 11-07-2010 at 04:42 PM.

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks poyzn for the quick reply. Unfortunately it does not work.
    I changed my php to:

    PHP Code:
    // 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

  • #4
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    oh, sorry, try this
    PHP Code:
    $content preg_replace('/<\/dl>.<dl class=\"gallery\-item\">/sU''</dl><div class="hidden"><dl class="gallery-item">'$content); 
    or this
    PHP Code:
    $content preg_replace('/<\/dl>[^<]+<dl class=\"gallery\-item\">/m''</dl><div class="hidden"><dl class="gallery-item">'$content); 
    Last edited by poyzn; 11-07-2010 at 05:05 PM.

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks for your help mate but none of them seem to work.

  • #6
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    have forgotten "+"
    PHP Code:
    $content preg_replace('/<\/dl>.*<dl class=\"gallery\-item\">/sU''</dl><div class="hidden"><dl class="gallery-item">'$content); 
    checked
    Last edited by poyzn; 11-07-2010 at 05:46 PM.

  • #7
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    very strange. It just refuses to work.

  • #8
    Regular Coder poyzn's Avatar
    Join Date
    Nov 2010
    Posts
    266
    Thanks
    2
    Thanked 61 Times in 61 Posts
    strange, check your $content variable.

    create php-file with this code and try to open it
    PHP Code:
    <?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.
    Last edited by poyzn; 11-07-2010 at 05:52 PM.

  • #9
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Shouldn't make any difference whatsoever. You could change that regex to a str_replace though.

    Code:
    $content = str_replace('<dl class="gallery-item">', '<div class="hidden"><dl class="gallery-item">', $content);
    How are you inserting the closing </div> btw?

  • Users who have thanked MattF for this post:

    stormcancer (11-08-2010)

  • #11
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
    if(in_category('fashion')) { echo'</div>'; } 
    Since I only apply this filter to specific content.
    Last edited by stormcancer; 11-08-2010 at 12:00 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •