Yii framework extension to replace context of text with whatever you like.
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:
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.
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]."\' />";', ));
I would like to thank rawtaz, SJFriedl, ciss & robert___ for the regexp help.
Total 3 comments
@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
Thanks for the comment
I just stumble upon the problem using '[]' as bag. But of course '{}' is working. Overall it is a great extension :)
Demo site and documentation will be created soon. Thanks in advanced.
Leave a comment
Please login to leave your comment.