I need some help. I have "name" and "id", I've display is as "id"-"name" (suggestion) I have a function that convert it to "name-id" (displayed in textbox after picking the suggestion).
The problem is, how can I properly explode if the name = Backlink-Spider and id = 25, the result
will be 25-Backlink-Spider and converted it to Backlink-Spider-25 . I tend to return it to unconverted , the result is Spider-25-Backlink.
This is my code.
$xpldName = explode("-", $posted['name'], 2);
$cdata = $C->loadByName($xpldName[1]);
$_POST['name'] = $xpldName[1]."-".$xpldName[0];
Explode the whole string.
Implode all the strings except the last one
Print last string - imploded string
Demo Code(Not tested):
$xpldName = explode("-", $posted['name'], 2);
$name = array_slice($xpldName, 0, -1);
$name = implode("-", $name);
$id = end($xpldName);
echo $id . " " . $name;
Related
So this is my code for set the date from selected date to auto set date excluding weekend (sat&sun) and holiday ( i put it in database ) :
function number_of_working_dates($from, $days) {
/* get holiday from database */
include('includes/config.php');
$sss = "SELECT tanggal_awal FROM libur_nasional GROUP BY tanggal_awal ASC";
$qqq = mysqli_query($konek,$sss);
$arr = array();
while ( $tam = mysqli_fetch_array($qqq)) {
$date = $tam['tanggal_awal'];
$reformat_date = date("Y-m-d", strtotime($date));
$arr[] = $reformat_date;
}
$array_date_2 = '"'.implode('", "' , $arr) . '"';
The output of implode() is a list of dates:
"2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17"
Then the continuation:
$holidayDays = [$array_date_2];
//$holidayDays = ["2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17"]
$from = new DateTime($from);
$dates = [];
$dates[] = $from->format('Y-m-d');
**echo count($holidayDays);**
while ($days) {
$from->modify('+1 day');
if (!in_array($from->format('N'), $workingDays)) continue;
if (in_array($from->format('Y-m-d'), $holidayDays)) continue;
if (in_array($from->format('*-m-d'), $holidayDays)) continue;
$dates[] = $from->format('Y-m-d');
$days--;
}
return $dates;
}
When I count the "holidayDays" array using "count" it only shows 1 item, but when I hardcoded the date list like in the comment line it works and shows 4 items.
Can someone help me?
A variable containing a string contains that string and it will continue to be treated as a string unless you do something like evaluating it as source code (which is very dangerous).
So if you have:
$foo = ["1", "2", "3"];
Then you have an array with three things in it.
But if you have:
$string = '"1", "2", "3"';
$foo = [$string];
Then you have an array with one thing in it: a string.
It is the same as writing:
$foo = ['"1", "2", "3"'];
If you want to have an array of strings then look at this line of your code:
$array_date_2 = '"'.implode('", "' , $arr) . '"';
That converts the array of strings into a single string.
Do not do that because that is not what you want.
Just use the value you already have in $arr.
You have a variable named "$array_date_2" and are trying to assign a String to it with the following code :
$array_date_2 = '"'.implode('", "' , $arr) . '"';
This assignment , after the "implode" has been executed , would result in the following :
$array_date_2 = ""2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17"";
Notice the quotes at the start and end of the String. Even though you do not see them when "echoing" the String is wrapped around those quotes. PHP does not permit a String without it being enclosed in quotes , it will throw a Parse Error.
So in the end you do not have a String like :
"2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17"
But more like
""2018-08-09", "2018-08-10", "2018-08-15", "2018-08-17""
Now you are also trying to create an array by concatenating Strings and Special Characters together. This is not how PHP works though.
If you want to "create" a function out of a String , or generally evaluate a String as PHP Code , then you need to use "eval" but i would strongly suggest against it.
For reference only , if you went with eval your code should be like this :
eval("\$holidayDays = [".$array_date_2 ."];");
I have a string and I would want to get the values/fields from it. However, the values are # separated.
Also, from one copy to the next it is comma separated.
As shown below;
$transaction = "
[2018-01-10 12:50:07.822#SAMUEL#TITUS],
[20120605152613#KEN#NAUGH],
[20120705152645#JOHHY#BRAVO]";
I need to loop through this string getting the values separated by the # for one record the to the next record separated by a comma.
The order of the fields is [TIME#FIRST_NAME#SECOND_NAME].
I can't think of a way to get this done.
Anyone?
Use explode to parse string into array
<?php
$transaction = "[2018-01-10 12:50:07.822#SAMUEL#TITUS],[20120605152613#KEN#NAUGH],[20120705152645#JOHHY#BRAVO]";
$parsed = explode(",", $transaction);
foreach($parsed as $val){
$val = explode("#", $val);
$first_name = $val[1];
$last_name = str_replace("]", '', $val[2]);
echo $first_name." ".$last_name."<br>"; // get firstname & lastname
}
?>
You can use the following solution using explode and array_map:
$transaction = "
[2018-01-10 12:50:07.822#SAMUEL#TITUS],
[20120605152613#KEN#NAUGH],
[20120705152645#JOHHY#BRAVO]";
//normalize the string and remove the unnecessary chars.
$transaction = str_replace(['[', ']', "\n"], '', $transaction);
//get all the rows as array.
$rows = explode(',', $transaction);
//create the columns in rows.
$row_arr = array_map(function ($row) {
return explode('#', $row);
}, $rows);
//info of the first row.
echo $row_arr[0][0]; // time
echo $row_arr[0][1]; // firstname
echo $row_arr[0][2]; // lastname
//run through the rows to output.
foreach ($row_arr as $row_item) {
echo 'Time: '.$row_item[0].', Firstname: '.$row_item[1].', Lastname: '.$row_item[2]."<br>";
}
demo: https://ideone.com/3uYcSw
i have one variable like this :
$variable = "0904201600000123";
i want to make the first 8 digits become like this :
2016-04-09
have tried to use this code :
<?php
$name = "$variable;
$explode = explode("", $name);
echo $explode[0];
echo "-";
echo $explode[1];
echo "-";
echo $explode[2];
?>
but it does not work.
May you know where is the problem?
Thank you so much for your help.
You can access a variable as you can access an array, if you were to echo:
$string = "test";
echo $string[0];
It would echo t, and onwards.
You could concat the string and make a new variable like:
$v = "0904201600000123";
$date = $v[0].$v[1].'-'.$v[2].$v[3].'-'.$v[4].$v[5].$v[6].$v[7];
echo $date;
I tested it on my local machine and it worked
Note: If you want to use the method using the explode function as I saw you did earlier, you could use the str_split function which will instead make the result an array.
Substr() can break a string in parts.
$FirtsPart = Substr($variable, 0, 8);
$SecondPart = Substr($variable, 8, 8);
$FirstPart = strtotime('Y-m-d', $FirstPart);
$variable = $FirstPart . $SecondPart;
$variable = "0904201600000123";
preg_match("/([0-9]){2}([0-9]){2}([0-9]){4}/", $variable, $datematches);
$newdate = $datematches[3] . "-" . $datematches[2] . "-" . $datematches[1];
Chances are I've more than likely massively over complicated that. Not tested but should do the trick.
$variable = "0904201600000123";
Note: The date format of $variable didnt understand. If its not a standard format, considering it as a string and separate using substr
Option 1
$date = substr($variable, 0, 2).'-'.substr($variable, 2, 2).'-'.substr($variable, 4, 4);
echo $date;
Option 2
$dd = substr($variable, 0, 2);
$mm = substr($variable, 2, 2);
$yy = substr($variable, 4, 4);
$date = $dd.'-'.$mm.'-'.$yy;
echo $date;
So, my problem is: I capture data from my database, part of this date is inside a json array. Then, I put all of the json information into an array. Like this:
foreach ($corporativos->lista as $value):
$classe_row = '';
$input = $value["info_adicionais"];
$data = json_decode($input,true);
echo $data['andamentos'];
$arr1 = explode(',',$data['andamentos']);
endforeach;
Now, I need to populate a table with this information. But I can't explode the string.
The strings are like this:
" Name name name, 123123/uf; Name name, 123123/uf " and so on.
First trim the string and explode on the ";" sign and on the "," sign as:
$str = " Name name name, 123123/uf; Name name, 123123/uf ";
$str = trim($str);
$temp = explode(";",$str);
if (count($temp) > 0) {
foreach ($temp as $key=>$value) {
$result[] = explode(",",$value);
// OR
$result[whatever the id you want here] = explode(",",$value);
}
}
Wondering if I could get some advice on tokenizing a string in php since Im relatively new to the language.
I have this:
$full_name = "John Smith"
I want to use a string function that will extract the first name and last name into indices of an array.
$arr[0] = "John"
$arr[1] = "Smith"
However, the function should also be able to handle the situation:
$full_name = "John Roberts-Smith II"
$arr[0] = "John"
$arr[1] = "Roberts-Smith II"
or
$full_name = "John"
$arr[0] = ""
$arr[1] = "John"
any suggestions on where to begin?
Use explode() with the optional limit param:
$full_name = "John Roberts-Smith II"
// Explode at most 2 elements
$arr = explode(' ', $full_name, 2);
// Your values:
$arr[0] = "John"
$arr[1] = "Roberts-Smith II"
Your last case is special though, placing the first name into the second array element. That requires special handling:
// If the name contains no whitespace,
// put the whole thing in the second array element.
if (!strpos($full_name, ' ')) {
$arr[0] = '';
$arr[1] = $full_name;
}
So a complete function:
function split_name($name) {
if (!strpos($name, ' ')) {
$arr = array();
$arr[0] = '';
$arr[1] = $name;
}
else $arr = explode(' ', $name, 2);
return $arr;
}
You should explode() function for this purpose.
$name_splitted = explode(" ", "John Smith", 2);
echo $name_splitted[0]; // John
echo $name_splitted[1]; // Smith
From the documentation -
array explode ( string $delimiter , string $string [, int $limit ] )
Returns an array of strings, each of which is a substring of "string" formed by splitting it on boundaries formed by the string "delimiter". If "limit" is set and positive, the returned array will contain a maximum of "limit" elements with the last element containing the rest of string. If the "limit" parameter is negative, all components except the last -"limit" are returned. If the "limit" parameter is zero, then this is treated as 1.