Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Came across a line in some Wordpress code I am modifying and just flat out don't know what the $2.$3 is? Maybe it's a PHP thing that I just never learned? Can anyone explain this to me?

Here's the plugin I'm modifying:

<?php
    /*
    Plugin Name: Pinterest Plugin
    Plugin URI: http://www.WordPressPinterestPlugin.com
    Description: Display a Pinterest "Pin It" button on top of your images, only when people move their mouse over the image.
    Version: 1.0 
    Author: Promotioner.com
    Author URI: http://www.WordPressPinterestPlugin.com    
    /*============================================================================================================ */    

    class pin_success
    {
        function pin_success() 
        {
            add_filter('the_content',  array('pin_success', 'nosn_pinterest'));
        }
        function nosn_pinterest($content) {
            global $post;
            $posturl = urlencode(get_permalink()); //Get the post URL
            $pindiv = '<div class="sn_pinterest">';
            $pinurl = '<a href="http://pinterest.com/pin/create/button/?url='.$posturl.'&media=';
            $pindescription = '&description='.urlencode(get_the_title());
            $pinfinish = '" target="_blank" class="sn_pin"></a>';
            $pinend = '</div>';
            $pattern = '/<img(.*?)src="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?) \/>/i'; 
            $replacement = $pindiv.$pinurl.'$2.$3'.$pindescription.$pinfinish.'<img$1src="$2.$3" $4 />'.$pinend;
            $content = preg_replace( $pattern, $replacement, $content );                    
            return $content;
        }   
    }    



    $successnexus = new pin_success;


    wp_enqueue_style('sn_pinterest', plugins_url('sn_pinterest.css', __FILE__ ));
?>
share|improve this question

closed as off topic by chrisguitarguy, Milo, Chris_O, Brian Fegter, toscho Sep 29 '12 at 2:58

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
You need to post more of the surrounding code. But it looks like those are back references for something like preg_replace. Also, this isn't a WordPress question. :) –  chrisguitarguy Sep 29 '12 at 0:09
    
Wasn't sure if it was a wordpress function or a php function. Ok, there's the surrounding code. –  Dave Sep 29 '12 at 0:10
    
Yeah... I guess I don't hardly ever use preg_replace and now that I'm looking at it, I believe you are right. And yeah, this is no longer a wordpress question, whoops. –  Dave Sep 29 '12 at 0:21
add comment

1 Answer 1

up vote 1 down vote accepted

Agree this isn't really a WordPress question.

However, I believe the $1, $2, and $3 refer to the capture groups determined by the (.*?) wildcards. Now, regex is totally voodoo to me so I'm sorry I can't explain it 100%. However if you look at the $pattern definition:

$pattern = '/<img(.*?)src="(.*?).(bmp|gif|jpeg|jpg|png)"(.*?) \/>/i'; 

Then $1 is the match immediately following the opening <img.

$2 is the image's src attribute

$3 is probably anything that comes after the src attribute

$4 doesn't look defined to me

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.