View previous topic :: View next topic |
Author |
Message |
vinoykr Smarty Rookie
Joined: 19 Oct 2011 Posts: 6
|
Posted: Wed Oct 19, 2011 5:37 am Post subject: I want to modify a string. Pls help |
|
|
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
Thanks in advance,
Amy Joseph |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Wed Oct 19, 2011 7:34 am Post subject: |
|
|
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 |
|
vinoykr Smarty Rookie
Joined: 19 Oct 2011 Posts: 6
|
Posted: Wed Oct 19, 2011 8:40 am Post subject: |
|
|
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 |
|
Back to top |
|
vinoykr Smarty Rookie
Joined: 19 Oct 2011 Posts: 6
|
Posted: Wed Oct 19, 2011 8:57 am Post subject: |
|
|
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......
Thanks a lot..
Amy |
|
Back to top |
|
rodneyrehm Administrator
Joined: 30 Mar 2007 Posts: 674 Location: Germany, border to Switzerland
|
Posted: Wed Oct 19, 2011 8:58 am Post subject: |
|
|
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 |
|
vinoykr Smarty Rookie
Joined: 19 Oct 2011 Posts: 6
|
Posted: Wed Oct 19, 2011 9:01 am Post subject: |
|
|
Thanks..
Shall start learning those..
cheers
|
|
Back to top |
|
dam Smarty n00b
Joined: 23 Mar 2008 Posts: 2
|
|
Back to top |
|
|