Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    me2
    me2 is offline
    New Coder
    Join Date
    Oct 2007
    Posts
    88
    Thanks
    8
    Thanked 3 Times in 3 Posts

    how to check if a string is present in a portion of a string

    i looked this up and found strpos..

    [code]
    if($rights >= $page['rights'] && strpos("/images/", $url) != 0)
    [code]

    this statement always returns false no matter what.. i have tried to use the === operator but it doesnt seem to work..

    I am trying to see if the page has enough rights to be displayed but i want to ignore all the pages in the images subfolder..

    Thanks in advance for all your help

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by me2 View Post
    i looked this up and found strpos..

    [code]
    if($rights >= $page['rights'] && strpos("/images/", $url) != 0)
    [code]

    this statement always returns false no matter what.. i have tried to use the === operator but it doesnt seem to work..

    I am trying to see if the page has enough rights to be displayed but i want to ignore all the pages in the images subfolder..

    Thanks in advance for all your help
    what values you have in $rights and $page['rights'] ?

    best regards

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by me2 View Post
    i looked this up and found strpos..

    [code]
    if($rights >= $page['rights'] && strpos("/images/", $url) != 0)
    [code]

    this statement always returns false no matter what.. i have tried to use the === operator but it doesnt seem to work..

    I am trying to see if the page has enough rights to be displayed but i want to ignore all the pages in the images subfolder..

    Thanks in advance for all your help
    strpos() returns false if the string is not found, so if you're trying to make sure the needle is in the haystack you want something like this:
    PHP Code:
    if($rights >= $page['rights'] && strpos("/images/"$url) !== false

  • #4
    me2
    me2 is offline
    New Coder
    Join Date
    Oct 2007
    Posts
    88
    Thanks
    8
    Thanked 3 Times in 3 Posts
    i tried putting that in but it doesnt seem to do what i want it to do..
    Code:
    if(0 >= 0 && strpos("/images/", "/images/authimage.php") !== false)
    is how it turns out when i translate all the values a file is in the images folder and it returns false but
    if(0 >= 0 && strpos("/images/", "/unit/minutes.php") !== false)
    this time the statement also equals false.. is there a way that i can make the first one false but the second one true??

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by me2 View Post
    i tried putting that in but it doesnt seem to do what i want it to do..
    Code:
    if(0 >= 0 && strpos("/images/", "/images/authimage.php") !== false)
    is how it turns out when i translate all the values a file is in the images folder and it returns false but
    if(0 >= 0 && strpos("/images/", "/unit/minutes.php") !== false)
    this time the statement also equals false.. is there a way that i can make the first one false but the second one true??
    Is this what you're looking for?
    PHP Code:
    if(>= && strpos("/images/""/images/authimage.php") === false// false

    if(>= && strpos("/images/""/unit/minutes.php") === false// true 
    If not be more descriptive with your desired result.

  • #6
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    I suggest using substr

    Code:
    if (strstr($string, "string"))
    {
      // ITS THERE
    }

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by Iszak View Post
    I suggest using substr
    You mean strstr(), like in the code you posted. If you check out the strstr() manual page you'll see this note:
    Note: If you only want to determine if a particular needle occurs within haystack , use the faster and less memory intensive function strpos() instead.

  • #8
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Inigoesdr is correct, though I suggest using stripos() to avoid case issues, and remember to use the Identical operator as others mentioned.
    If I'm postin here, I NEED YOUR HELP!!

  • #9
    New Coder
    Join Date
    Nov 2007
    Location
    Thane-Vashi,Mumbai,India.
    Posts
    44
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Why don't you use PHP regular expressions? You can find a very useful tutorial at http://www.sitepoint.com/article/reg...xpressions-php

    Though learning regular expressions may take a slighty longer time, its is well worth it

  • #10
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    There's no need to load the regex engine for something as trivial as this.
    Quote Originally Posted by http://php.net/preg_match
    Tip

    Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.

  • #11
    me2
    me2 is offline
    New Coder
    Join Date
    Oct 2007
    Posts
    88
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Sorry about the late reply

    Quote Originally Posted by Inigoesdr View Post
    Is this what you're looking for?
    PHP Code:
    if(>= && strpos("/images/""/images/authimage.php") === false// false

    if(>= && strpos("/images/""/unit/minutes.php") === false// true 
    If not be more descriptive with your desired result.
    now they both return true

    if(0 >= 0 && stripos("/images/", "/unit/minutes.php") === false) // i want that to return false

    and for

    if(0 >= 0 && stripos("/images/", "/images/authimage.php") // to return true..

    thanks

  • #12
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    if($rights >= $page['rights'] && preg_match("/\/images\//",$url)){

    best regards

  • Users who have thanked oesxyl for this post:

    me2 (02-19-2008)

  • #13
    me2
    me2 is offline
    New Coder
    Join Date
    Oct 2007
    Posts
    88
    Thanks
    8
    Thanked 3 Times in 3 Posts
    thank you for your help now it does work and if anyone else can come up with a solution that doesn't have to load regex i will be happier..

  • #14
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by me2 View Post
    if anyone else can come up with a solution that doesn't have to load regex i will be happier..
    You've already been shown two solutions that don't involve loading the regex engine.

  • #15
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Inigoesdr View Post
    You've already been shown two solutions that don't involve loading the regex engine.
    this strpos("/images/", "/images/authimage.php") return 0, because it find '/images/' to position 0, php assume is false.

    me2 is right, both return true.

    best regards


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •