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.
Results 1 to 14 of 14
  1. #1
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts

    split() function help

    I have a string like "[section1][section2]" which i take the first and last charecters off so im left with "section1][section2"

    But I cant seem to split it using this split code:
    PHP Code:
    <?php
    $str 
    "[section1][section2]";
    $str substr(substr($str,0,-1),1);
    print_r(split('[\]\[]'$str));
    ?>
    How could I do this? if anyone could help Thanks in advance.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Don't bother, use the trim command instead:
    PHP Code:
    $result trim($str']['); 
    Optionally, you can just remove the first and last characters.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    This should do it:

    PHP Code:
    $str "[section1][section2]";
    $str{0} = $str{strlen($str) - 1} = null;
    $str trim($str); 
    edit: Fou-Lu's example is more readable. We both posted at the same time.
    Last edited by xconspirisist; 03-30-2010 at 11:53 PM.
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    both very good solutions thank you I have done this but is it a good idea or is there a better way?
    PHP Code:
    <?php
    $str 
    "[section1][section2]";
    $str substr(substr($str,0,-1),1);
    print_r(split("\]\["$str));
    ?>
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #5
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    I suggest you use Fou-Lu's example, as it is more readable and therefore, more maintainable!
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #6
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    would that put the string into an array such as array(0 => 'section1', 1 => 'section2');?
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #7
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Oh sorry! I think we both missed that you wanted them split in two at the end. Here:

    PHP Code:
    $str "[section1][section2]";
    $str trim($str"][");
    $str explode("]["$str);
    print_r($str); 
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #8
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    damn I know about explode just didn't think about using that lol

    You wouldn't happen to know how I could do this would you?

    I have a string like (first|1|N)||(first|2|N)&&(go|1|N)&&(new|!1|N) which is like a more advanced if statement to break it down this is how its used (query|value|type) where the type can equal N, L or NL which mean numeric, letters or numeric and letters what im trying to do is make it so all I have to do is this callif("(first|1|N)||(first|2|N)&&(go|1|N)&&(new|!1|N)"); and it will check the url to see if the php query equals ?first=1&go=1&new=2 or ?first=2&go=1&new=2. And because the value for new says !1 then it will equal true if new=2 or new=3 but if new=1 then it will equal false.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #9
    Regular Coder xconspirisist's Avatar
    Join Date
    Jun 2006
    Location
    Great Britain.
    Posts
    138
    Thanks
    1
    Thanked 6 Times in 6 Posts
    You need regex!
    If I have been helpful, use the "thank" button - It makes me happy!

    xconspirisist.co.uk - homepage of my online alias
    technowax.net - a community for people interested in all forms of modern technology.

  • #10
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    ok and would i need to use any foreach functions because I was thinking about splitting it by the '&&' and then if it contains '||' then split that with-in the foreach and check both against the url array.. would that be a good idea along with regex querying?
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #11
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    If it's a GET, use parse_url().

  • #12
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    thats a good idea MattF but with a normal if statement if you did if($_GET['first']=="1" || $_GET['first']=="2" && $_GET['go']=="1" && $_GET['new']!="1"); it would equal true if the url query equals ?first=1 or ?first=2&go=1&new=2 but I want mine to only be true when all prams are met such as ?first=1&go=1&new=2 or ?first=2&go=1&new=2. I was asking how I could do that.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #13
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Separate your match code. For example:

    Code:
    if(($_GET['first']=="1" && $_GET['go']=="1") || ($_GET['first']=="2" && $_GET['go']=="2"))
    Matches if first and go are both 1 or first and go are both 2 but not on any other combination. Adapt to suit.
    Last edited by MattF; 03-31-2010 at 01:16 AM.

  • Users who have thanked MattF for this post:

    DJCMBear (03-31-2010)

  • #14
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    I have created the function callif does it look ok?
    PHP Code:
    <?php
    function callif($str){
      
    $C split('&&',$str);
      foreach(
    $C As $_V => $_C){
        
    $check strpos($_C,'||')?true:false;
        if(
    $check === true){
          
    $new_query split("[||]",$C[$_V]);
          foreach(
    $new_query As $_S => $_G){
            
    $G split(':',substr(substr($_G,1),0,-1));
            if(
    strtolower($G[2])=="n"){
              
    $GF is_numeric($_GET[$G[0]])?true:false;
            }elseif(
    strtolower($G[2])=="l"){
              
    $GF preg_match('/^[a-zA-Z]+$/'$_GET[$G[0]])?true:false;
            }elseif(
    strtolower($G[2])=="nl" || strtolower($G[2])=="ln"){
              
    $GF preg_match('/^[a-zA-Z0-9]+$/'$_GET[$G[0]])?true:false;
            }
            if(
    $G[0]!="" && $_GET[$G[0]]==$G[1] && $GF === true
            
    || $G[0]!="" && $G[1]=="all" && $GF === true){
              
    $_FCall .= 1;
            }elseif(
    substr($G[1],0,1)=="!" && $_GET[$G[0]]!=substr($G[1],1) && $GF === true){
              
    $_FCall .= 1;
            }
          }
        }else{
          
    $G split(':',substr(substr($_C,1),0,-1));
          if(
    strtolower($G[2])=="n"){
            
    $GF is_numeric($_GET[$G[0]])?true:false;
          }elseif(
    strtolower($G[2])=="l"){
            
    $GF preg_match('/^[a-zA-Z]+$/'$_GET[$G[0]])?true:false;
          }elseif(
    strtolower($G[2])=="nl" || strtolower($G[2])=="ln"){
            
    $GF preg_match('/^[a-zA-Z0-9]+$/'$_GET[$G[0]])?true:false;
          }
          if(
    $G[0]!="" && $_GET[$G[0]]==$G[1] && $GF === true
          
    || $G[0]!="" && $G[1]=="all" && $GF === true){
            
    $_FCall .= 1;
          }elseif(
    substr($G[1],0,1)=="!" && $_GET[$G[0]]!=substr($G[1],1) && $GF === true){
            
    $_FCall .= 1;
          }
        }
      }
      
    $matches strlen($_FCall);
      
    $pattern count($C);
      if(
    $matches==$pattern){
        return 
    1;
      }else{
        return 
    0;
      }
    }
    print 
    callif("(first:1:N)||(first:2:N)||(second:2:N)&&(go:1:N)&&(new:all)");
    ?>
    It seems to work 100% idk if anyone will spot anything wrong if you do please let me know, thank you.
    Last edited by DJCMBear; 03-31-2010 at 05:19 PM. Reason: Added extra option for "all" so you can allow the value to equal anything in the defined pram
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

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