I have a comma separated string stored in a column of data type blob ,with values as given below.
date,time,A,B,C,D
11/31/2013,11:00,20,17,18,11
12/31/2013,14:00,18,16,18,14
10/31/2013,17:00,15,17,10,22
09/31/2013,19:00,19,17,20,17
I want the string parsed and the string containing date and time removed corresonding to its values finally i require it like this
A,B,C,D
20,17,18,11
18,16,18,14
15,17,10,22
19,17,20,17
I tried using
$exp = explode(',',$arr[0]);
$arrayOfReplacements = array(':' => '','/'=>'');
$clean = strtr($arr[0], $arrayOfReplacements);
print_r($clean);
it needs to remove the date,time and its values also.
what more needs to be done ?
You could use something like:
$exp = explode(',',$arr[0]);
array_shift($exp);
array_shift($exp);
$output = implode(',',$exp);
Assuming $arr[0] contains the comma separated values:
echo join(',', array_slice(explode(',', $arr[0]), 2));
See also: array_slice()
Alternatively, use substr() with strpos():
echo substr($arr[0], strpos($arr[0], ',', strpos($arr[0], ',') + 1) + 1);
The above assumes that the format of the blob column can be trusted to have at least two commas.
If the format of date and time columns is not going to change, it can be further simplified:
echo substr($arr[0], 17);
Related
I have a text box.
I am enter the value in text box like 12 13 14.
and i am want to convert this into 12,13,14 and then convert it into array and show each separate value.
If your form field asks for the values without a comma, then you will need to explode the POST data by space. What you're doing now is imploding it by comma (you can't implode a string to begin with), and then trying to pass that into a foreach loop. However, a foreach loop will only accept an array.
$ar = explode(' ',$da);
That simple change should fix it for you. You will want to get rid of the peculiar die() after your foreach (invalid syntax, and unclear what you're trying to do there!), and validate your data before the loop instead. By default, if you explode a string and no matching delimiters are found, the result will be an array with a single key, which you can pass into a loop without a problem.
Are you sure you want to expect the user enters data in that particular format? I mean, what if the user uses more than one space character, or separate the numbers actually with commas? or with semicolons? or enters letters instead of numbers? Anyway.. at least you could transform all the spaces to a single space character and then do the explode() as suggested:
$da = trim(preg_replace('/\s+/', ' ', $_POST['imp']));
$ar = explode(' ', $da);
before your foreach().
use explode instead of implode as
The explode() function breaks a string into an array.
The implode() function returns a string from the elements of an array.
and you cannot do foreach operation for a string.
$da=$_POST['imp'];
$ar = explode(' ',$da);
foreach($ar as $k)
{
$q="insert into pb_100_fp (draw_3_fp) values ('".mysqli_real_escape_string($conn, $k)."')";
$rs=mysqli_query($conn, $q);
echo $k.",";
}
then you will get this output
o/p : 12,13,14,
I have following issue:
I import WKT dynamicaly from DB into WKT Wicket Javascript library. I must do some substitutions to fit the WKT correctly. Since mysql fetches WKT AsText(SHAPE) i recive several arrays e.g. POLYGON((xxxx)),POLYGON((yyyy)) and so on.
First, I had to remove all "POLYGON" doing
$str = preg_replace('/^POLYGON/', '', $WKT[1]);
and add MULTIPOLYGON before <?php
tag in the wicket. It works.
Second, I must add comma between polygons, preicisely between "))((" brackets:
$str2 = str_replace(array('((', '))'), array('((', ')),'), $str);
It works but last comma remains what "slightly" deforms my multipolygon:
MULTIPOLYGON((xxx)),((yyy)),((zzz)),
How can I remove last comma?
I would be thankful for every regex or some other solution which can solve my problem.
In any string, you can remove the last X if you are sure that no X follows. So, you can use a negative lookahead: (,)(?!.*,), as seen here and replace it with empty string.
$result = preg_replace('/(,)(?!.*,)/', '', $str)
This doesn't look at the context though, it will just remove the last comma of any string, no matter where it is.
Thank you both - your answers were right and very helpful.
The problem was not string replacement. It was more the data fetching from DB.
Mysqli_fetch_array and mysqli_fetch_assoc return stringstringsring or arrayarrayarray for 3 rows fetched. That is why all commas were replaced.
I changed to mysqli_fetch_all ,then did minor replacements for each row (as array) and implode each one as variable. After i merged them into single variable, then I could apply your solutions. It is not sofisticated solution, but if it is packed into function it'll be fine.
($WKT = mysqli_fetch_all($result)) {
$str = preg_replace('/POLYGON/', '', $WKT[0]);
$str1 = preg_replace('/POLYGON/', '', $WKT[1]);
$str2 = preg_replace('/POLYGON/', '', $WKT[2]);
$str3 = implode($str);
$str4 = implode($str1);
$str5 = implode($str2);
$str6 = $str3 . $str4 . $str5;
$str7 = preg_replace('/\)\)/', ')),', $str6);
$str8 = rtrim($str7, ",");
echo $str8;
}
I'm grabbing data out of an API, and every once in a while, a value comes back in one of the fields I'm capturing that has a comma in it, which is causing my results to be offset by 1 each time it happens.
after I've gotten the results from the API, I put it into $answerSearch, then I json_decode it.
$json = json_decode($answerSearch, true);
foreach ($json['Result']['a']['b'] as $i) {
$y = "{$i['number']},{$i['Type']},{$i['c']},{$i['d']},{$i['e']}";
$x = explode("," , $y);
array_push($output, $x);
}
One of these {$i['number']} values may be "1234,123", and because I use comma as a delimiter, it's causing the entire array to screw up.
How do I tell PHP that the , in the middle of this string is part of the string and not a delimeter?
Update
I can't just change $x = explode("," , $y); to use another delimiter because ,, /, ;, etc are all used in the actual strings that are being returned, I was just using , as an example.
There is no for the conversion from array to string back to an array, to add the selected values to the $output array use:
$output[]=array({$i['number']},{$i['Type']},{$i['c']},{$i['d']},{$i['e']});
I'm using a simple explode operation to use array values for inserting records to mysql database.
The code i use is:
// for loop above
$fieldsArr = explode(',', $field_names);
where $field_names is a string like :
'1_gps_update_coordinates','1' // prints out just fine
'1_meter_conf_holiday1_end','2099-01-01' // also fine
but
'1_electricity_unit_price','0,100' gives problem.
How can I overcome this? Any tip is appricated.
Ps $field_names values comes directly from database so I cannot really write an if statement.
if you can't change the input data, you can do this:
change the delimiter from comma to semicolon with str_replace function
$field_name = str_replace("','", "';'", $field_name)
then you can explode by semicolon
$fieldsArr = explode(';', $field_names);
You need to insert float using english number formatting
0.1
Instead of european 0,1
Use number_formatting() or simply str_replace(',', '.', $val);
This question already has answers here:
Fastest way of deleting a value in a comma separated list
(4 answers)
Closed 2 years ago.
I am storing numbers in a database with a string like 5,55,15,17,2,35
I want to remove say number 5 from the string, keeping the comma seperation set like it is.
The problem with using str_replace() is that all of the 5's will be removed. If i use str_replace('5,'', $string) thats fine, but the comma wouldn't be after the 5 if it was in the middle of the string or at the end. That same str_replace would also remove part of 15, and 55,
Am i missing something?
$array = explode(',', $string);
foreach ($array as $k => $v)
if ($v == 5) unset($array[$k]);
$string = implode(',', $array);
You probably shouldn't be storing a comma separated list of values in a single database column in the first place. It looks like a one-to-many association, which should be modeled with a separate table.
Split the string first by comma so you can work with the numbers directly. Remove 5 from the array, then recombine the array into a comma-delimited string.
Here's an example:
<?php
$input = '5,55,15,17,2,35';
echo "Input: $input<br />";
// Split the string by "exploding" the string on the delimiter character
$nums = explode(',', $input);
// Remove items by comparing to an array containing unwanted elements
$nums = array_diff($nums, array(5));
// Combine the array back into a comma-delimited string
$output = implode(',', $nums);
echo "Output: $output<br />";
// Outputs:
// Input: 5,55,15,17,2,35
// Output: 55,15,17,2,35
?>
str_replace([$num.",",",".$num],"",$input);
U can try something like this:
// Convert string into array of numbers
$arrOfNumbers = explode(',', $string);
// Find id of number for del
$numberForDelId = array_search($numberForDel, $arrOfNumbers);
//Delete number
unset($arrOfNumbers[$numberForDelId]);
// Convert back to string
$resultString = implode(',' $arrOfNumbers)
You should:
get the string
then "explode" the values into an array
and re-create the string without the number you want to remove.