alternative to strpos

Discussion corner for Developers of Serendipity.
Post Reply
User avatar
Don Chambers
Regular
Posts: 3628
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

alternative to strpos

Post by Don Chambers » Mon Mar 09, 2015 9:04 pm

I want to know of a var begins with "blah". What I tried to use was:

Code: Select all

{if $var|strpos:'blah'===0}
Produces this error:

modifier 'strpos' not allowed by security setting <-- thrown in /home/content/html/bundled-libs/Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php on line 17

User avatar
garvinhicking
Core Developer
Posts: 30014
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: alternative to strpos

Post by garvinhicking » Tue Mar 10, 2015 4:18 pm

Hi!

Only certain PHP functions are allowed in the core (see include/serendipity_smarty_class.inc.php, $php_modifiers).

The best way is in your config.inc.php file to register your own substring filter:

Code: Select all

function is_in_string($string, $search) {
  if (strpos($string, $search) === 0) {
    return true;
  }
  return false;
}
$serendipity['smarty']->registerPlugin('modifier', 'is_in_string', 'is_in_string');
And then use

Code: Select all

{if $var|is_in_string:'blah'}
HTH,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

User avatar
Don Chambers
Regular
Posts: 3628
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: alternative to strpos

Post by Don Chambers » Tue Mar 10, 2015 5:23 pm

Awesome - will give that a try and report back.

User avatar
Don Chambers
Regular
Posts: 3628
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: alternative to strpos

Post by Don Chambers » Tue Mar 10, 2015 10:46 pm

garvinhicking wrote: And then use

Code: Select all

{if $var|is_in_string:'blah'}
Does that support multiple values? like 'blah','food','drink' ?

User avatar
garvinhicking
Core Developer
Posts: 30014
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: alternative to strpos

Post by garvinhicking » Tue Mar 10, 2015 11:01 pm

No. One of those could:

Code: Select all

function is_in_string($string, $search) {
  foreach($search AS $idx => $searchval) {
  if (strpos($string, $searchval) === 0) {
    return true;
  }
  }
  return false;
}
Smarty usage: {$var|is_in_string:array('one','two','three')}

If that doesn'T work:

Code: Select all

function is_in_string($string, $search) {
  $array = explode(',', $search);
  foreach($array AS $idx => $searchval) {
  if (strpos($string, $search) === 0) {
    return true;
  }
  }
  return false;
}
Smarty usage: {$var|is_in_string:'one,two,three'}

HTTH,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

User avatar
Don Chambers
Regular
Posts: 3628
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: alternative to strpos

Post by Don Chambers » Tue Mar 10, 2015 11:27 pm

Thanks again Garvin. I'm trying to determine if an entry property contains an embedded video, in case you were wondering. :wink:

First suggestion error: "PHP function 'array' not allowed by security setting"

Second suggestion:

Code: Select all

{if $var|is_in_string:'<object,<embed,<iframe'}. 
Doesn't return an error, but also does not evaluate as true.

In my very first attempt, prior to asking if it would support multiple values, I used this which worked:

Code: Select all

{if $var|is_in_string:'<object' || $var|is_in_string:'<embed || $var|is_in_string:'<iframe'}
Just seemed to be doing something three times that should be able to be done just once.

User avatar
Don Chambers
Regular
Posts: 3628
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: alternative to strpos

Post by Don Chambers » Wed Mar 11, 2015 2:13 am

I found the problem. here is the corrected version:

Code: Select all

function is_in_string($string, $search) {
  $array = explode(',', $search);
  foreach($array AS $idx => $searchval) {
  if (strpos($string, $searchval) === 0) {
    return true;
  }
  }
  return false;
}
in strpos(), $search needed to be $searchval.

User avatar
garvinhicking
Core Developer
Posts: 30014
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: alternative to strpos

Post by garvinhicking » Wed Mar 11, 2015 4:35 pm

Ah! Sorry. Basic copy&paste mistake! :(
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/

User avatar
Don Chambers
Regular
Posts: 3628
Joined: Mon Feb 13, 2006 3:40 am
Location: Chicago, IL, USA
Contact:

Re: alternative to strpos

Post by Don Chambers » Wed Mar 11, 2015 5:36 pm

No reason to be sorry. Could not have done it without your initial input! 8)

Post Reply