Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

I want to modify a string. Pls help

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
vinoykr
Smarty Rookie


Joined: 19 Oct 2011
Posts: 6

PostPosted: Wed Oct 19, 2011 5:37 am    Post subject: I want to modify a string. Pls help Reply with quote

Hi,

I am very new to Smarty and I am loving it. I would like to modify or truncate a string based on a pattern. I have a string on a variable $processor which contains something like this for eg.

eg: Dual-core 1.2GHz Samsung Exynos processor, Mali-400MP GPU, Orion chipset, 3D Graphics

What I want is to extract only the processor Speed alone from this. That is 1.2GHz here. This can come anywhere in the string for different phone models and any Speed like 600MHz. Only pattern to recognize is "Hz"

So what i want is a modifier which can remove all unwanted text in begining and after "1.2GHz" .

I will be very thankfull if anyone help me out Question Rolling Eyes

Thanks in advance,
Amy Joseph
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Wed Oct 19, 2011 7:34 am    Post subject: Reply with quote

You'll want to have a look at Extenting Smarty with Plugins, specifically modifiers.

Then you'll want to have a look at preg_match() and learn yourself some regex.

Then you'll come up with this very simple modifier plugin:
Code:
<?php

function smarty_modifier_herz($string) {
    preg_match("#(\d+\.)?\d+[gm]hz#i", $string, $matches);
    return $matches[0];
}


which you drop into your plugins_dir and use in a template by {$foobar|herz}
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
vinoykr
Smarty Rookie


Joined: 19 Oct 2011
Posts: 6

PostPosted: Wed Oct 19, 2011 8:40 am    Post subject: Reply with quote

Thanks a lot rodneyrehm..

Let me try this and let you know..

one more thing is that this is custom for "Hz" and will work only in that case. Can we make it as a universal one which can take any word we specify, like suppose if I give a word like "Pixel"
for a string "Screen 400x600 pixel"

It will return "400x600 pixel"


Thanks in Advance.

Amy Smile
Back to top
View user's profile Send private message
vinoykr
Smarty Rookie


Joined: 19 Oct 2011
Posts: 6

PostPosted: Wed Oct 19, 2011 8:57 am    Post subject: Reply with quote

rodneyrehm wrote:
You'll want to have a look at Extenting Smarty with Plugins, specifically modifiers.

Then you'll want to have a look at preg_match() and learn yourself some regex.

Then you'll come up with this very simple modifier plugin:
Code:
<?php

function smarty_modifier_herz($string) {
    preg_match("#(\d+\.)?\d+[gm]hz#i", $string, $matches);
    return $matches[0];
}


which you drop into your plugins_dir and use in a template by {$foobar|herz}


This worked...... Very Happy

Thanks a lot..

Amy Embarassed
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Wed Oct 19, 2011 8:58 am    Post subject: Reply with quote

As I said, learn yourself some regex. http://php.net/pcre knows everything you need for the job.

You could change the plugin to
Code:
function smarty_modifier_match($string, $pattern, $capture=0) {
    if (preg_match($pattern, $string, $matches)) {
        return isset($matches[$capture]) ? $matches[$capture] : null;
    }
   
    return null;
}


test it with
Code:
$string = "Dual-core 1.2GHz Samsung Exynos processor, Mali-400MP GPU, Orion chipset, 3D Graphics, 123x321 Pixel Display";

var_dump(
    smarty_modifier_match($string, '#(\d+\.)?\d+\s?[mg]hz#i'), // 1.2GHz
    smarty_modifier_match($string, '#([^\s]+) Pixel#i'), // 123x321 Pixel
    smarty_modifier_match($string, '#([^\s]+) Pixel#i', 1) // 123x321
);


use it with
Code:
{$string = "Dual-core 1.2GHz Samsung Exynos processor, Mali-400MP GPU, Orion chipset, 3D Graphics, 123x321 Pixel Display"}

{$string|match:'#(\d+\.)?\d+\s?[mg]hz#i'} {* 1.2GHz *}
{$string|match:'#([^\s]+) Pixel#i'} {* 123x321 Pixel *}
{$string|match:'#([^\s]+) Pixel#i':1} {* 123x321 *}

_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
vinoykr
Smarty Rookie


Joined: 19 Oct 2011
Posts: 6

PostPosted: Wed Oct 19, 2011 9:01 am    Post subject: Reply with quote

Thanks..

Shall start learning those..

cheers

Very Happy
Back to top
View user's profile Send private message
dam
Smarty n00b


Joined: 23 Mar 2008
Posts: 2

PostPosted: Tue Jan 17, 2012 1:16 pm    Post subject: Reply with quote

I wrote a plugin for Smarty which allows to use the php preg_match function inside a template. http://www.venturin.net/2012/01/17/smarty-v-3-preg_match-plugin/

Ciao!
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP