Jamroom Logo Jamroom 5 Core
is now Open Source!
User Support Forum Archive (Read Only)
Jamroom Developers:
strstr to trim?
djmerlyn
Jamroom Ustad


Joined: 18 Dec 2003
Posts: 13497
Location: Behind You

Posted: 05/12/09 12:45 
I'm trying to test a variable to see if it starts with xx

I'm doing this;

$var=myModule
$slim=my

if(strstr($var,$slim) == TRUE) {

$var might be myModule or myStuff or a number of different things... I just want to "catch all" $var that starts with "my" and false everything else.

Is this the most efficient way of doing this? And, although I've read the docs for strstr about 50 times, I still don't understand- is it only going to test the beginning of $var? Or is it going to see if "my" is anywhere in $var? I want it to only test the beginning in case $slim=ohmy

From what I've read, I think it will "search" for "my" anywhere in $var, so maybe I need to trim $var to just the first 2 letters first then see if $vartrim == $slim which I don't yet know how to do.

I'm just a little confused Confused

Thanks!


_________________
Pro JR Hosting, now 50% off!
-100% Guaranteed

"more server and network power than any host, dedicated to your jamroom site"

Last edited by djmerlyn on 05/12/09 12:48; edited 1 time in total
Back to top
Brian
Jamroom Team


Joined: 09 Jul 2003
Posts: 37583
Location: Seattle, WA

Posted: 05/12/09 12:48 

djmerlyn:
I'm trying to test a variable to see if it starts with xx

I'm doing this;

$var=myModule
$slim=my

if(strstr($var,$slim) == TRUE) {

$var might be myModule or myStuff or a number of different things... I just want to "catch all" $var that starts with "my" and false everything else.

Is this the most efficient way of doing this? And, although I've read the docs for strstr about 50 times, I still don't understand- is it only going to test the beginning of $var? Or is it going to see if "my" is anywhere in $var? I want it to only test the beginning in case $slim=ohmy

I'm just a little confused Confused

Thanks!


You want to use strpos() for this - i.e.

if (strpos($var,'my') === 0) {
.. it matches ..
}

Hope this helps!

- Brian


_________________
Make sure and check out:
* The Jamroom FAQ
* The Jamroom Documentation
Back to top
djmerlyn
Jamroom Ustad


Joined: 18 Dec 2003
Posts: 13497
Location: Behind You

Posted: 05/12/09 12:55 
oh my, thats a whole new set of confusing documentation Confused

It looks like it also "searches" the entire $var - but you can set the start position not limit the extent of the search (to say the first 2 characters).

This stuff is a little out of my league. I'll keep google searching. I think ultimately I'm going to just take $var and trim everything after the first 2 letters off and give it a new variable, then check $vartrim against $slim and see if they == or not- at least that makes sense to me even if it is the long way Sad

Thanks!


_________________
Pro JR Hosting, now 50% off!
-100% Guaranteed

"more server and network power than any host, dedicated to your jamroom site"
Back to top
Brian
Jamroom Team


Joined: 09 Jul 2003
Posts: 37583
Location: Seattle, WA

Posted: 05/12/09 13:04 

djmerlyn:
oh my, thats a whole new set of confusing documentation Confused

It looks like it also "searches" the entire $var - but you can set the start position not limit the extent of the search (to say the first 2 characters).

This stuff is a little out of my league. I'll keep google searching. I think ultimately I'm going to just take $var and trim everything after the first 2 letters off and give it a new variable, then check $vartrim against $slim and see if they == or not- at least that makes sense to me even if it is the long way Sad

Thanks!


I would _highly_ recommend using strpos - it is exactly what you want. the code I showed basically says "if the first 2 letters of $var are 'my', then be true"

Hope this helps!

- Brian


_________________
Make sure and check out:
* The Jamroom FAQ
* The Jamroom Documentation
Back to top
djmerlyn
Jamroom Ustad


Joined: 18 Dec 2003
Posts: 13497
Location: Behind You

Posted: 05/12/09 13:15 
I'm sitting here tinkering with it- and you're right, it does do what I want it to do, but the tough part is that I need to understand what its doing and I don't Smile Especially if I find out doing this broke something else, I'll need to be able to return to it with some understanding of what exactly I did.

http://www.php.net/manual/en/function.strpos.php

The documentation there doesn't really say "if it doesn't find a match to $slim immediately in the first few characters, it stops searching and returns false".

I usually learn the most by the user replies below the docs with the examples, but the examples are more confusing then the docs on this one Laughing!

The $needle and $haystack example in the docs is meaningless to me Laughing I can certainly trust that you telling me it will do what I want is likely true, but I really want to understand what its doing and I don't Sad

Thanks for your help man!


_________________
Pro JR Hosting, now 50% off!
-100% Guaranteed

"more server and network power than any host, dedicated to your jamroom site"
Back to top
Brian
Jamroom Team


Joined: 09 Jul 2003
Posts: 37583
Location: Seattle, WA

Posted: 05/12/09 13:34 
strpos does not search the entire string - it only looks at a specific offset in the variable for an exact string match. So in our example, when we have:

if (strpos($var,'my') === 0)

what PHP does is look at the very first byte in the value (the byte at location zero (0) - remember, strings and arrays in PHP start with 0 - it is also why we use the triple equals here) - if that is an "m", then it checks that position 1 is a "y" - if it is, then it is true. $var could be a million bytes, and only the first 2 would ever be checked. This is why strpos is the very fastest PHP string checking function you can use.

Hope this helps!

- Brian


_________________
Make sure and check out:
* The Jamroom FAQ
* The Jamroom Documentation
Back to top
djmerlyn
Jamroom Ustad


Joined: 18 Dec 2003
Posts: 13497
Location: Behind You

Posted: 05/12/09 15:04 
Ah, ok that makes sense Smile Why can't they just say that? lol!

Now, if I'm understanding correct, this;

if (strpos($var,'my') === 0)

And this;

if (strpos($var,'my') === TRUE)

Effectively do the same thing?

Thanks again!


_________________
Pro JR Hosting, now 50% off!
-100% Guaranteed

"more server and network power than any host, dedicated to your jamroom site"
Back to top
Brian
Jamroom Team


Joined: 09 Jul 2003
Posts: 37583
Location: Seattle, WA

Posted: 05/12/09 15:09 

djmerlyn:
Ah, ok that makes sense Smile Why can't they just say that? lol!

Now, if I'm understanding correct, this;

if (strpos($var,'my') === 0)

And this;

if (strpos($var,'my') === TRUE)

Effectively do the same thing?

Thanks again!


No - that is incorrect. You need to use === 0, since you are saying, make sure the STRING is in position 0. If you did this:

if (strpos($var,'my') === 6) {

then it would be TRUE for the following string:

thisismyvar

strpos() returns the integer position of the substring within the string (if found), else FALSE if not found. Thus we do an integer test to see if we match. TRUE will not be part of your test.

Hope this helps!

- Brian


_________________
Make sure and check out:
* The Jamroom FAQ
* The Jamroom Documentation
Back to top
djmerlyn
Jamroom Ustad


Joined: 18 Dec 2003
Posts: 13497
Location: Behind You

Posted: 05/12/09 15:13 
Now I see it. I was throwing a bunch of stuff to it in a test script, but I was still using ===TRUE and wasn't getting what I expected.

Using your first example still using === TRUE, I plugged it in to my module and (of course) got an error, but now that I understand what it does it was easy to fix and actually works right now.

I totally get it now. Thanks so much for your help with this!


_________________
Pro JR Hosting, now 50% off!
-100% Guaranteed

"more server and network power than any host, dedicated to your jamroom site"
Back to top
Brian
Jamroom Team


Joined: 09 Jul 2003
Posts: 37583
Location: Seattle, WA

Posted: 05/12/09 16:17 
No problem Wink

- Brian


_________________
Make sure and check out:
* The Jamroom FAQ
* The Jamroom Documentation
Back to top
Display posts from previous:   
User Support Forum Archive (Read Only)
Jamroom Developers

 
Solutions
• Social Media Platform
• Social Networking Software
• Musician Website Manager
• Community Builder
Products
• Jamroom Core
• Jamroom Addons
• Jamroom Modules
• Jamroom Marketplace
Support
• Support Forum
• Documentation
• Support Center
• Contact Support
Community
• Community Forum
• Member Sites
• Developers
Company
• About Us
• Contact Us
• Privacy Policy
©2003 - 2010 Talldude Networks, LLC.