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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Pulling a substring out of the middle of a string

    hey,
    I'm trying to pull a substring out of this string I have created based off of my query...
    What I want is to pull the three different variables and set them each to strings.
    here's a photo of said string

    I can get the first and the last portions by using the following:

    $vn = substr($vehicle, 0, strpos($vehicle, '~'));
    &
    $vp = substr($vehicle, strpos($vehicle, '|'));

    but not the middle variable...
    any help would be most appreciated
    Thanks,

  • #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
    O.o
    Why not just extract from you're $nt array? In any case, I'd just try to use an sscanf to get it in:
    PHP Code:
    $vn '';
    $vp 0.0;
    $vd '';

    sscanf($vehicle"%s~%s|%f"$vn$vd$vp); 
    Try that.
    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
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    O.o
    Why not just extract from you're $nt array?
    the string is being passed through an option value and needs to be broken up on the next page...

    I tried to do it like this

    PHP Code:
    $vd substr($vehiclestrpos($vehicle,'~')+1strpos($vehicle'|')); 
    however it didn't work...

  • #4
    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
    Quote Originally Posted by mrlab View Post
    the string is being passed through an option value and needs to be broken up on the next page...

    I tried to do it like this

    PHP Code:
    $vd substr($vehiclestrpos($vehicle,'~')+1strpos($vehicle'|')); 
    however it didn't work...
    Did the sscanf work? I can't test it atm so I can't be certain.
    If you're in control and you're passing along the data, I'd just serialize the array and pass that instead. Then when retrieved, you can unserialize to refetch the original array. Save yourself the hassle of string parsing.
    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 ;)

  • #5
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $query="SELECT vehicle_name,id,price,vehicle_description FROM vehicle";

    $result mysql_query ($query);

    echo 
    "<select name='vehicle' value=''>vehicle</option>";

    while(
    $nt=mysql_fetch_array($result)){
    $vn=$nt['vehicle_name'];
    $vd=$nt['vehicle_description'];
    $vp=$nt['price'];
    $v=$vn'~' .$vd .'|' .$vp;

    echo 
    "<option value='$v'>$vn</option>";


    }

    echo 
    "</select>"
    is the query, I'm using a table off the server to populate a dropdown list then taking all the data from the row and throwing it into the option value...I'm not sure where to put the sscanf statement.

    PHP Code:
    $vehicle=$this->input->post('vehicle');
        
    $vn substr($vehicle0strpos($vehicle'~'));

        
    $vd substr($vehiclestrpos($vehicle,'~')+1strpos($vehicle'|')); 
    this is the code that's trying to pull it out...
    Last edited by mrlab; 02-18-2010 at 09:30 PM.

  • #6
    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
    sscanf would replace the entire second block in order to populate the $vn, $vd and $vp variables.
    I'd just do the above, assuming that this is non-sensitive data since you're passing it through a form:
    PHP Code:
    $query="SELECT vehicle_name,id,price,vehicle_description FROM vehicle"

    $result mysql_query ($query); 

    echo 
    "<select name='vehicle'>"

    while(
    $nt=mysql_fetch_array($result)){ 
    $v serialize($nt);

    echo 
    "<option value='$v'>" $nt['vehicle_name'] ."</option>"




    echo 
    "</select>"
    And retrieved on you're posted form:
    PHP Code:
    $nt unserialize($_POST['vehicle']); 
    Now $nt will be an array consisting of the entries associated with the selected item (this will also contain the 'id' since it was included with you're query). I've corrected you're select statement as well; there is no value attribute on a select element and you cannot have an unstarted option tag.

    Edit:
    Changed the option text, I didn't notice that it was a different variable.
    Last edited by Fou-Lu; 02-18-2010 at 09:45 PM.
    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 ;)

  • #7
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did this
    PHP Code:
        $vehicle=$this->input->post('vehicle');
        
    sscanf($vehicle"%s~%s|%f"$vn$vd$vp); 
        
    $vehicle_name=$vp;
        
    $vehicle_descrip=$vd;
        
    $vehicle_price=$vp
    however, it's still not spliting it up

  • #8
    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
    Quote Originally Posted by mrlab View Post
    I did this
    PHP Code:
        $vehicle=$this->input->post('vehicle');
        
    sscanf($vehicle"%s~%s|%f"$vn$vd$vp); 
        
    $vehicle_name=$vp;
        
    $vehicle_descrip=$vd;
        
    $vehicle_price=$vp
    however, it's still not spliting it up
    Then go with the serialized route.
    I presume you're getting partials in each of those variables at least? They are not just null?
    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 ;)


  •  

    Posting Permissions

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