alternative to strpos

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

alternative to strpos

Post 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
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: alternative to strpos

Post 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
# 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/
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: alternative to strpos

Post by Don Chambers »

Awesome - will give that a try and report back.
=Don=
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: alternative to strpos

Post 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' ?
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: alternative to strpos

Post 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
# 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/
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: alternative to strpos

Post 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.
=Don=
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: alternative to strpos

Post 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.
=Don=
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: alternative to strpos

Post by garvinhicking »

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/
Don Chambers
Regular
Posts: 3652
Joined: Mon Feb 13, 2006 2:40 am
Location: Chicago, IL, USA
Contact:

Re: alternative to strpos

Post by Don Chambers »

No reason to be sorry. Could not have done it without your initial input! 8)
=Don=
hbarel
Regular
Posts: 8
Joined: Sun Apr 25, 2021 7:22 pm

Re: alternative to strpos

Post 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...
onli
Regular
Posts: 2822
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: alternative to strpos

Post 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.
hbarel
Regular
Posts: 8
Joined: Sun Apr 25, 2021 7:22 pm

Re: alternative to strpos

Post 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...
Post Reply