vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Testing to see if one string is contained in another (https://vborg.vbsupport.ru/showthread.php?t=63171)

CyberRanger 03-30-2004 02:20 AM

Testing to see if one string is contained in another
 
This seems so simple but I can't figure out the syntax -

In my php script, I have the variables $animal and $shelter set.

Let's say animal = 'black cat' and shelter = 'brown dog, black cat, pig'

In my vbulletin template I want to do a test to see if $animal contains a phrase that is in $shelter.

I have:
Quote:


<if condition="$shelter == $animal">

This is stuff only a black cat member will see...

</if>

How can I modify the above code to be CONTAINS instead of IS?

filburt1 03-30-2004 02:25 AM

Ideally you want to use arrays, but given that PHP is hardly an ideal language:
PHP Code:

if (strpos($haystack$needle) !== false

See http://www.php.net/strpos for details. If you happen to have PHP5, use stripos (http://www.php.net/stripos).

CyberRanger 03-30-2004 11:33 AM

Failed:
PHP Code:

$checkladder strpos('$onladder''$ladder'); 

I'd being trying strpos without success. Ends up my problem was I forgot to double quote the variables ... arghhh....

Worked:
PHP Code:

$checkladder strpos("$onladder""$ladder"); 

thanks!

Boofo 03-30-2004 11:43 AM

I'm curious here. What are you doing with this code? I mean, is it a hack? ;)

Velocd 03-30-2004 05:36 PM

I think his examples are just that, examples. People often provide the simplest example of their problem in order to get the point across. He probably doesn't intend to create some kind of farm-animal hack. :\

Boofo 03-30-2004 05:48 PM

LOL No, I meant how would you use something like that in a hack or somewhere else?

Velocd 03-30-2004 06:54 PM

Ah. ;)

Well, strpos and stripos (case insensitive) are good for finding a substring withing a string.

www.php.net/strpos

It could be used for many things, like.. finding a username inside a post, etc.

For general iteration in a loop, it's best to parse the string into an array (if it's a list) with explode() and then use in_array(), since it's faster.

Boofo 03-30-2004 06:56 PM

Ok, I'm not quite sure I understand completely but the part about finding a username inside a post makes sense. Just for yucks, how would you set it up to do something like that then? ;)


All times are GMT. The time now is 01:06 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01087 seconds
  • Memory Usage 1,729KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (8)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete