Yii 1.1: ereplacer

Extension to replace context of text with whatever you like.
4 followers

Yii framework extension to replace context of text with whatever you like.

Description and use cases

You can use this extension to replaces 'standar' parts for a string to whatever you like. I will describe my personal use case. In my blog I used to add posts with title like:

  • {yii}{ext} EReadmore
  • {yii}{wiki} A simple...
  • {php} New to ...

This was to categorize the Posts. So one day i wanted to replace all this with some icons. The code below does this work for me. Other use cases can be to display emoticons in text, tags, etc.

Code example

Notice that there is not constrain to use a key=>value array as data. What $data will be depends entirely to you and its related to who you will access that data in the replace eval phrase.

$this->beginWidget('ext.EReplacer', array(
        'bag'=>'{}',
        'data'=>array(
           'yii'=>'http://image src...',
           'wiki'=>'http:///image src...',
           'php'=>'http://image src...',
        ),
        'replace'=>'"<img src=\'".$data[$el]."\' />";',
    ));
    echo $post->title;
    $this->endWidget();

You can as well use the text property like:

$this->widget('ext.EReplacer', array(
        'bag'=>'{}',
        'text'=>$post->title,
        'data'=>array(
           'yii'=>'http://image src...',
           'wiki'=>'http:///image src...',
           'php'=>'http://image src...',
        ),
        'replace'=>'"<img src=\'".$data[$el]."\' />";',
    ));

Thanks

I would like to thank rawtaz, SJFriedl, ciss & robert___ for the regexp help.

Total 3 comments

#3627 report it
tydeas_dr at 2011/04/25 08:47am
Check documentation

@junxiong If you noticed at the $bag comment in the file it says " ...if you want to use regexp special chars be sure you escape them." What this means is that such a problem was expected. Please try

...
    'bag'=>'\[\]',
    ...

Thanks for the comment

#3624 report it
junxiong at 2011/04/25 03:49am
Can't use [] as bag

I just stumble upon the problem using '[]' as bag. But of course '{}' is working. Overall it is a great extension :)

#3510 report it
tydeas_dr at 2011/04/16 01:29pm
documentation

Demo site and documentation will be created soon. Thanks in advanced.

Leave a comment

Please to leave your comment.

Create extension