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

cutting string

 
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 -> General
View previous topic :: View next topic  
Author Message
Demerzel
Smarty Rookie


Joined: 31 Jan 2010
Posts: 26

PostPosted: Sun Sep 26, 2010 3:38 pm    Post subject: cutting string Reply with quote

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
View user's profile Send private message
scuzzy
Smarty Regular


Joined: 31 Aug 2003
Posts: 84

PostPosted: Sun Sep 26, 2010 11:04 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Demerzel
Smarty Rookie


Joined: 31 Jan 2010
Posts: 26

PostPosted: Sun Sep 26, 2010 11:19 pm    Post subject: Reply with quote

So how to repleace everything before ABC including ABC and everything after new line?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sun Sep 26, 2010 11:55 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Demerzel
Smarty Rookie


Joined: 31 Jan 2010
Posts: 26

PostPosted: Mon Sep 27, 2010 7:17 am    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Sep 27, 2010 1:51 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
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 -> General 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