|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Demerzel Smarty Rookie
Joined: 31 Jan 2010 Posts: 26
|
Posted: Sun Sep 26, 2010 3:38 pm Post subject: cutting string |
|
|
It's on of these things you should do in 5 minutes with one and but I always have troubles with regular expresions.
Code: | {assign var=temp value=$a.description|regex_replace:"/*ABC/":" "}
{assign var=temp value=$temp|regex_replace:"/[/n]*/":" "}
{$temp} |
I think code above says it all. Question is how should first line look because *ABC removes everything. |
|
Back to top |
|
scuzzy Smarty Regular
Joined: 31 Aug 2003 Posts: 84
|
Posted: Sun Sep 26, 2010 11:04 pm Post subject: |
|
|
the first one isn't valid regex? (can't start with *)
and your second one is replacing any character that is / or n unlimited times?[/i] |
|
Back to top |
|
Demerzel Smarty Rookie
Joined: 31 Jan 2010 Posts: 26
|
Posted: Sun Sep 26, 2010 11:19 pm Post subject: |
|
|
So how to repleace everything before ABC including ABC and everything after new line? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sun Sep 26, 2010 11:55 pm Post subject: |
|
|
Demerzel wrote: | So how to repleace everything before ABC including ABC and everything after new line? |
/.*ABC/ replace with ""
/\n.*/ replace with "\n" |
|
Back to top |
|
Demerzel Smarty Rookie
Joined: 31 Jan 2010 Posts: 26
|
Posted: Mon Sep 27, 2010 7:17 am Post subject: |
|
|
Thanks
Code: | {if substr_count($a.description,"ABC")}
{assign var=temp value=$a.description|regex_replace:"/.*ABC/":""}
{assign var=tempb value=$temp|regex_replace:"/\n.*/":""}
{$tempb} |
It looks like first one don't work and after seconf regex I get first line from description not part of second like I would like. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Sep 27, 2010 1:51 pm Post subject: |
|
|
the "." does not include newlines by default. If you want to include those, you need the /s modifier. see the PCRE manual for examples of how to use regular expressions.
Also, you could build yourself a plugin and do all your regex work in PHP instead of in-template, if that helps. |
|
Back to top |
|
|
|
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
|