5

I wrote this PHP code to make some substitutions:

function cambio($txt){
    $from=array(
        '/\+\>([^\+\>]+)\<\+/', //finds +>text<+
        '/\%([^\%]+)\%/',   //finds %text%
    );

    $to=array(
        '<span class="P">\1</span>',
        '<span>\1</span>',
    );

    return preg_replace($from,$to,$txt);
}

echo cambio('The fruit I most like is: +> %apple% %banna% %orange% <+.');

Resulting into this:

The fruit I most like is: <span class="P"> <span>apple</span> <span>banna</span> <span>orange</span> </span>.

However I needed to identify the fruit's span tags, like this:

The fruit I most like is: <span class="P"> <span class="t1">apple</span> <span class="t2">banana</span> <span class="t3">coco</span> </span>.

I'd buy a fruit to whom discover a regular expression to accomplish this :-)


Whit the Xavier Barbosa's help, I came to this final sollution:

function matches($matches){
    static $pos=0;
    return sprintf('<span class="t%d">%s</span>',++$pos,$matches[1]);
}

function cambio($txt){//Markdown da Atípico : Deve ser usado depois do texto convertido para markdown
    $from=array(
        '/\=>(.+?)<\=/', //finds: =>text<=
        '/\+>(.+?)<\+/', //finds +>text<+
    );

    $to=array(
        '<span class="T">\1</span>',
        '<span class="P">\1</span>',
    );

    $r=preg_replace($from,$to,$txt);
    return preg_replace_callback('/%(.*?)%/','matches',$r);//finds %text%
    //'/%((\w)\w+)%/'   //option
}
6
  • the class of the inner span(s) (i.e. class="b") is always equal to the first letter of the fruit ? Commented Jan 13, 2011 at 19:43
  • Are you writing your own templating language in PHP? Beware of BobX. Commented Jan 13, 2011 at 20:05
  • Ass3mbler, sorry, I did not intend to make a relation with the content's first letter. I edited the class names to something like: t1, t2 and t3. Commented Jan 13, 2011 at 21:16
  • 1
    Nathan, I wouldn't dare to go so far :-) Commented Jan 13, 2011 at 21:17
  • @Roger: No need to edit your question title to say it's solved. The accepted answer will indicate that for you (in most cases) ;) Commented Jan 14, 2011 at 0:20

2 Answers 2

2
<?php


function cambio($txt){
    $from=array(
        '/\+>(.+?)<\+/', //finds +>text<+
        '/%((\w)\w+)%/',   //finds %text%
    );

    $to=array(
        '<span class="P">\1</span>',
        '<span class="\2">\1</span>',
    );

    return preg_replace($from,$to,$txt);
}

echo cambio('The fruit I most like is: +> %apple% %banna% %orange% <+.');

And a stateful version for PHP5.3

function cambio($txt) {
    return preg_replace_callback('/\+>(.+?)<\+/', function ($matches) {
        $txt = sprintf('<span class="P">%s</span>', $matches[1]);

        return preg_replace_callback('/%(\w+)%/', function ($matches) {
            static $pos = 0;
            return sprintf('<span class="t%d">%s</span>', ++$pos, $matches[1]);
        }, $txt);

    }, $txt);
}

echo  cambio('The fruit I most like is: +> %apple% %banna% %orange% <+.');
Sign up to request clarification or add additional context in comments.

3 Comments

Obrigado/Gracias, Xavier! Going a little deeper, is it possible to get this?: The fruit I most like is: <span class="P"> <span class="t1">apple</span> <span class="t2">banna</span> <span class="t3">coco</span> </span>.
Well for this you will need to use the preg_replace_callback()function. But this function is stateless, so you'll need to cheat. See the example, but beware, it works only on PHP5.3
Perfect! Unretouchable. Thank you.
1

Try this:

function cambio($txt){
    $from=array(
        '/\+\>([^\+\>]+)\<\+/', //finds +>text<+
        '/\%(^\%)([^\%]+)\%/',   //finds %text%
    );

    $to=array(
        '<span class="P">\1</span>',
        '<span class="\1">\1\2</span>',
    );

    return preg_replace($from,$to,$txt); }

echo cambio('The fruit I most like is:
+> %apple% %banna% %orange% <+.');

1 Comment

Well, your code echos: The fruit I most like is: <span class="P"> %apple% %banna% %orange% </span>... Note that I changed a bit what I first wished. I am picturing how I can name the inner span class with a string not related to the content...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.