Page 1 of 1

alternative to strpos

Posted: Mon Mar 09, 2015 8:04 pm
by Don Chambers
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

Re: alternative to strpos

Posted: Tue Mar 10, 2015 3:18 pm
by garvinhicking
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

Re: alternative to strpos

Posted: Tue Mar 10, 2015 4:23 pm
by Don Chambers
Awesome - will give that a try and report back.

Re: alternative to strpos

Posted: Tue Mar 10, 2015 9:46 pm
by Don Chambers
garvinhicking wrote: And then use

Code: Select all

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

Re: alternative to strpos

Posted: Tue Mar 10, 2015 10:01 pm
by garvinhicking
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

Re: alternative to strpos

Posted: Tue Mar 10, 2015 10:27 pm
by Don Chambers
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.

Re: alternative to strpos

Posted: Wed Mar 11, 2015 1:13 am
by Don Chambers
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.

Re: alternative to strpos

Posted: Wed Mar 11, 2015 3:35 pm
by garvinhicking
Ah! Sorry. Basic copy&paste mistake! :(

Re: alternative to strpos

Posted: Wed Mar 11, 2015 4:36 pm
by Don Chambers
No reason to be sorry. Could not have done it without your initial input! 8)

Re: alternative to strpos

Posted: Fri May 14, 2021 2:37 pm
by hbarel
I actually tried my own function for replacing strpos.

I need it to strip the entire URL that comes as $tag from the tags plugin, and leave me with a simple text-only tag that I can add to the keyword metadata of the HTML header.
So I defined the following:

Code: Select all

function tag_from_link($tag) {
  $ttag='';
  if (strpos($tag,'title=')) {
    $tit_s = strpos($tag,'title=')+7;
    $tit_e = strpos($tag,'"',$tit_s+7);
    $ttag = substr($tag,$tit_s,$tit_e-$tit_s);
  }
  return $ttag;
}
$serendipity['smarty']->registerPlugin('modifier', 'tag_from_link', 'tag_from_link');
I put this in the serendipity_config_local.inc.php, but all I get is an error: "Call to member function registerPlugin() on null"...

I sense that my mistake is trivial, but being a newbie I have no idea what it is...

Re: alternative to strpos

Posted: Sat May 15, 2021 12:06 am
by onli
Wouldn't really call this a replacement for strpos, more a function using it.
hbarel wrote:
Fri May 14, 2021 2:37 pm
I put this in the serendipity_config_local.inc.php, but all I get is an error: "Call to member function registerPlugin() on null"...
It's likely that `$serendipity['smarty']` is still null when you run your code. serendipity_config_local.inc.php is executed very early, far before the moment the templates get interpreted. Put the code instead in the config.inc.php of your theme, there smarty will already be initialized.

Re: alternative to strpos

Posted: Sat May 15, 2021 11:27 am
by hbarel
Excellent, thank you!
Indeed, I thought serendipity_config_local.inc.php might be a bit too early and causes the bug, but I did not realize there is a "later" config.inc.php with the template... Truthfully, I should have searched better...
P.S. You're right, the topic is not really strpos, but I figured I'd better post in the thread where the idea came from, rather than open a new thread...