...

View Full Version : Pulling a substring out of the middle of a string



mrlab
02-18-2010, 10:00 PM
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
http://i369.photobucket.com/albums/oo133/hwystar67/Picture1.png?t=1266525081
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,

Fou-Lu
02-18-2010, 10:12 PM
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:


$vn = '';
$vp = 0.0;
$vd = '';

sscanf($vehicle, "%s~%s|%f", $vn, $vd, $vp);

Try that.

mrlab
02-18-2010, 10:17 PM
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


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

Fou-Lu
02-18-2010, 10:20 PM
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


$vd = substr($vehicle, strpos($vehicle,'~')+1, strpos($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.

mrlab
02-18-2010, 10:27 PM
$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.


$vehicle=$this->input->post('vehicle');
$vn = substr($vehicle, 0, strpos($vehicle, '~'));

$vd = substr($vehicle, strpos($vehicle,'~')+1, strpos($vehicle, '|'));


this is the code that's trying to pull it out...

Fou-Lu
02-18-2010, 10:40 PM
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:


$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:


$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.


Changed the option text, I didn't notice that it was a different variable.

mrlab
02-18-2010, 10:40 PM
I did this

$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

Fou-Lu
02-18-2010, 10:46 PM
I did this

$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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum