How to use preg_split to get the desire result - php

I have a string like this
1 BDP-105159857 2602 BARUN SINHA MTE02 MTE07 MTE08 PHE11 PHE14
I want to split it like
1, BDP, 105159857, 2602, BARUN SINHA, MTE02, MTE07, MTE08, PHE11, PHE14
If I use
preg_split('/[-, ]+/',$string);
Then it splits the name (BARUN SINHA) in two parts as expected. But I want to keep it together. How to overcome it?
This is a screenshot of my data

Related

Not able to get from and to date values from database using php mysql

I am trying to projectname,activityname,duration,employee names with todate,fromdate,projectid but i am not able to get here my code and scheme
select ohrm_project.project_id as ohrmprojectid,ohrm_project.customer_id,ohrm_project.name as ohrm_projectname,ohrm_timesheet.timesheet_id as ohrmtimesheet_id, ohrm_timesheet.employee_id as ohrmtimesheetemployeeid,ohrm_timesheet_item.*, ohrm_project_activity.activity_id as ohrmprojectactivityid, ohrm_project_activity.project_id as ohrmprojectactivityprojectid, ohrm_project_activity.name as activityname,hs_hr_employee.* from ohrm_project,ohrm_timesheet,ohrm_timesheet_item,ohrm_project_activity,hs_hr_employee where ohrm_timesheet_item.project_id=2 and ohrm_timesheet_item.activity_id=ohrm_project_activity.activity_id and ohrm_timesheet_item.employee_id=hs_hr_employee.emp_number and ohrm_timesheet_item.employee_id=hs_hr_employee.emp_number and ohrm_timesheet_item.date between 2016-03-31 and 2016-04-04
click here to see image
click here to see image
Use this:
SELECT ohrm_project.project_id AS ohrmprojectid,
ohrm_project.customer_id,
ohrm_project.name AS ohrm_projectname,
ohrm_timesheet.timesheet_id AS ohrmtimesheet_id,
ohrm_timesheet.employee_id AS ohrmtimesheetemployeeid,
ohrm_timesheet_item.*,
ohrm_project_activity.activity_id AS ohrmprojectactivityid,
ohrm_project_activity.project_id AS ohrmprojectactivityprojectid,
ohrm_project_activity.name AS activityname,
hs_hr_employee.*
FROM ohrm_project,
ohrm_timesheet,
ohrm_timesheet_item,
ohrm_project_activity,
hs_hr_employee
WHERE ohrm_timesheet_item.project_id=2
AND ohrm_timesheet_item.activity_id=ohrm_project_activity.activity_id
AND ohrm_timesheet_item.employee_id=hs_hr_employee.emp_number
AND ohrm_timesheet_item.employee_id=hs_hr_employee.emp_number
AND ohrm_timesheet_item.date BETWEEN '2016-03-31' AND '2016-04-04'
Dates would be treated as strings. Wrap them with quotes.
ohrm_timesheet_item.date between '2016-03-31' and '2016-04-04'

Parsing an array from single field

I have a table that has a field that contains what appears to be an array in it. Is there an easy way to convert this into an array that I can work with?
SELECT data FROM exp_cartthrob_item_options_options
Print_r(data);
Returns the following:
a:19:{i:0;a:6:{s:12:"option_value";s:18:"cp-1202-faraglioni";s:11:"option_name";s:16:"1202 Faraglioni ";s:5:"price";s:0:"";s:5:"color";s:5:"Putty";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:1;a:6:{s:12:"option_value";s:21:"cp-1203-scala-fenicia";s:11:"option_name";s:18:"1203 Scala Fenicia";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:2;a:6:{s:12:"option_value";s:19:"cp-1204-castiglione";s:11:"option_name";s:17:"1204 Castiglione ";s:5:"price";s:0:"";s:5:"color";s:5:"Beige";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:3;a:6:{s:12:"option_value";s:19:"cp-1205-villa-jovis";s:11:"option_name";s:17:"1205 Villa Jovis ";s:5:"price";s:0:"";s:5:"color";s:3:"Tan";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:4;a:6:{s:12:"option_value";s:16:"cp-1207-anacapri";s:11:"option_name";s:14:"1207 Anacapri ";s:5:"price";s:0:"";s:5:"color";s:10:"Warm Beige";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:5;a:6:{s:12:"option_value";s:20:"cp-1208-monte-solaro";s:11:"option_name";s:17:"1208 Monte Solaro";s:5:"price";s:0:"";s:5:"color";s:10:"Warm Beige";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:6;a:6:{s:12:"option_value";s:13:"cp-BLCK-black";s:11:"option_name";s:10:"BLCK Black";s:5:"price";s:0:"";s:5:"color";s:6:"Black ";s:13:"grain_texture";s:5:"Full ";s:6:"finish";s:5:"Light";}i:17;a:6:{s:12:"option_value";s:21:"cd-0061-mexican-ochre";s:11:"option_name";s:18:"0061 Mexican Ochre";s:5:"price";s:0:"";s:5:"color";s:10:"Warm Beige";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:18;a:6:{s:12:"option_value";s:12:"cd-0063-soho";s:11:"option_name";s:9:"0063 SoHo";s:5:"price";s:0:"";s:5:"color";s:6:"Orange";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:19;a:6:{s:12:"option_value";s:13:"cd-0064-cocoa";s:11:"option_name";s:10:"0064 Cocoa";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:20;a:6:{s:12:"option_value";s:18:"cd-0065-brownstone";s:11:"option_name";s:15:"0065 Brownstone";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:21;a:6:{s:12:"option_value";s:13:"cd-0066-tabac";s:11:"option_name";s:10:"0066 Tabac";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:22;a:6:{s:12:"option_value";s:18:"cd-0067-toro-black";s:11:"option_name";s:15:"0067 Toro Black";s:5:"price";s:0:"";s:5:"color";s:5:"Black";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:23;a:6:{s:12:"option_value";s:23:"cd-0068-sun-dried-brick";s:11:"option_name";s:20:"0068 Sun-Dried Brick";s:5:"price";s:0:"";s:5:"color";s:12:"Medium Brown";s:13:"grain_texture";s:5:"Full ";s:6:"finish";s:5:"Light";}i:24;a:6:{s:12:"option_value";s:14:"cd-0069-gothic";s:11:"option_name";s:11:"0069 Gothic";s:5:"price";s:0:"";s:5:"color";s:5:"Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:25;a:6:{s:12:"option_value";s:23:"cd-0070-rembrandt-brown";s:11:"option_name";s:23:"0070 Rembrandt Brown ";s:5:"price";s:0:"";s:5:"color";s:12:"Medium Brown";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:27;a:6:{s:12:"option_value";s:15:"cd-0078-pompeii";s:11:"option_name";s:12:"0078 Pompeii";s:5:"price";s:0:"";s:5:"color";s:3:"Red";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:30;a:6:{s:12:"option_value";s:21:"cd-0082-gulmard-green";s:11:"option_name";s:18:"0082 Gulmard Green";s:5:"price";s:0:"";s:5:"color";s:10:"Dark Green";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}i:31;a:6:{s:12:"option_value";s:21:"cd-0084-prussian-blue";s:11:"option_name";s:18:"0084 Prussian Blue";s:5:"price";s:0:"";s:5:"color";s:9:"Dark Blue";s:13:"grain_texture";s:4:"Full";s:6:"finish";s:5:"Light";}}
Yes, you can use unserialize() to achieve that:
SELECT data FROM exp_cartthrob_item_options_options
$array = unserialize($data);
var_dump($array);

PHP to convert string to array

This may be asked several times but my case is a bit different.
Let me start from the beginning.
$ck_data = db_select('ckeditor_settings', 'cs')
->fields('cs', array('settings'))
->condition('name', 'Advanced', '=')
->execute()
->fetchAssoc();
var_dump($ck_data);
Will give me...
array(1) {
["settings"]=>
string(2144) "a:33:{s:2:"ss";s:1:"2";s:7:"toolbar";s:606:"[
['Source'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','SpellChecker','Scayt'],
['Undo','Redo','Find','Replace','-','SelectAll'],
['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar'],
['Maximize','ShowBlocks'],
'/',
['Format'],
['Bold','Italic','Underline','Strike','-','Subscript','Superscript','-','RemoveFormat'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl'],
['Link','Unlink','Anchor','Linkit','LinkToNode','LinkToMenu']
]";s:6:"expand";s:1:"t";s:7:"default";s:1:"t";s:11:"show_toggle";s:1:"t";s:7:"uicolor";s:7:"default";s:12:"uicolor_user";s:7:"default";s:5:"width";s:4:"100%";s:4:"lang";s:2:"en";s:9:"auto_lang";s:1:"t";s:18:"language_direction";s:7:"default";s:15:"allowed_content";s:1:"t";s:19:"extraAllowedContent";s:0:"";s:10:"enter_mode";s:1:"p";s:16:"shift_enter_mode";s:2:"br";s:11:"font_format";s:35:"p;div;pre;address;h1;h2;h3;h4;h5;h6";s:17:"custom_formatting";s:1:"f";s:10:"formatting";a:1:{s:25:"custom_formatting_options";a:6:{s:6:"indent";s:6:"indent";s:15:"breakBeforeOpen";s:15:"breakBeforeOpen";s:14:"breakAfterOpen";s:14:"breakAfterOpen";s:15:"breakAfterClose";s:15:"breakAfterClose";s:16:"breakBeforeClose";i:0;s:10:"pre_indent";i:0;}}s:8:"css_mode";s:4:"none";s:8:"css_path";s:0:"";s:9:"css_style";s:5:"theme";s:11:"styles_path";s:0:"";s:11:"filebrowser";s:4:"none";s:17:"filebrowser_image";s:0:"";s:17:"filebrowser_flash";s:0:"";s:13:"UserFilesPath";s:5:"%b%f/";s:21:"UserFilesAbsolutePath";s:7:"%d%b%f/";s:21:"forcePasteAsPlainText";s:1:"t";s:13:"html_entities";s:1:"f";s:17:"scayt_autoStartup";s:1:"t";s:15:"theme_config_js";s:1:"f";s:7:"js_conf";s:0:"";s:11:"loadPlugins";a:1:{s:12:"drupalbreaks";a:5:{s:4:"name";s:12:"drupalbreaks";s:4:"desc";s:51:"Plugin for inserting Drupal teaser and page breaks.";s:4:"path";s:25:"%plugin_dir%drupalbreaks/";s:7:"buttons";a:1:{s:11:"DrupalBreak";a:2:{s:5:"label";s:11:"DrupalBreak";s:4:"icon";s:22:"images/drupalbreak.png";}}s:7:"default";s:1:"t";}}}"
}
Now what I want is to get the value of toolbar.
$ck_settings = unserialize($ck_data['settings']);
$ck_plugins = $ck_settings['toolbar'];
var_dump($ck_plugins);
Will return...
string(606) "[
['Source'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-','SpellChecker','Scayt'],
['Undo','Redo','Find','Replace','-','SelectAll'],
['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar'],
['Maximize','ShowBlocks'],
'/',
['Format'],
['Bold','Italic','Underline','Strike','-','Subscript','Superscript','-','RemoveFormat'],
['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl'],
['Link','Unlink','Anchor','Linkit','LinkToNode','LinkToMenu']
]"
My question now is how can I convert $ck_plugins from string to array?
Strange way to be storing that, but it is a string that looks like a PHP array definition:
eval("\$ck_plugins = $ck_plugins;");
print_r($ck_plugins);
If you have control over the data storage, you should probably either store the individual entries in a table or store the entire thing serialized or better in JSON.

PHP code to replace certain values in array, code generator

I am trying to write PHP code as a hobby project to basically create a "possible" code generator. The scenario is that we have a list of 25 valid characters that can be used.
Imagine that you have a 25 character code but you have accidentally scratched off the first two characters or three characters at any location in the code. Now we need to find all the possible combinations to try out. I have put all the valid characters into the array below that can be used in the code.
$valid=array("B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","Z",
"2","3","4","6","7","8","9");
$arraylength=count($valid);
The still available or seen characters are input into a text box and in the place where the character is unreadable is left blank and the variable values are fetched.
$char1= $_POST['code1'];
$char2= $_POST['code2'];
$char3= $_POST['code3'];
$char4= $_POST['code4'];
$char5= $_POST['code5'];
$char6= $_POST['code6'];
$char7= $_POST['code7'];
$char8= $_POST['code8'];
$char9= $_POST['code9'];
$char10= $_POST['code10'];
$char11= $_POST['code11'];
$char12= $_POST['code12'];
$char13= $_POST['code13'];
$char14= $_POST['code14'];
$char15= $_POST['code15'];
$char16= $_POST['code16'];
$char17= $_POST['code17'];
$char18= $_POST['code18'];
$char19= $_POST['code19'];
$char20= $_POST['code20'];
$char21= $_POST['code21'];
$char22= $_POST['code22'];
$char23= $_POST['code23'];
$char24= $_POST['code24'];
$char25= $_POST['code25'];
And put into an array...
$jada = array($char1, $char2, $char3, $char4, $char5, $char6, $char7, $char8, $char9, $char10, $char11, $char12, $char13, $char14, $char15
, $char16, $char17, $char18, $char19, $char20, $char21, $char22, $char23, $char24, $char25);
I have been stumped for a while now, the fiddling I have done at the moment is that if a variable is empty then do something (as a test echo or print the possible combinations)
if(!isset($char1) || trim($char1) == ""){
for($x=0;$x<$arraylength;$x++) {
echo $valid[$x];
echo "<br>";
} }
else{
echo ($char1);
}
Can you guys help out?
Saw this still in an open status after many years of hiatus, I figured that I may as well share some information.
In the end I figured it out, you can grab the source here and test it in your own server: https://github.com/Masterkriz/XBOX_Pre-paid_code_fixer

MySQL SubString Returns Integer

I've got a table with a varchar(128) field called identifier. It is supposed to get a 3 letter identifier followed by an indexing number:
ABC-1234
At some point in the past, there was a bug and about 5,000 records were input as:
ABC-12345
Our numbers do not go that high, so these values are padded with zeros (0).
I wrote out a script to make sure my Microsoft T-SQL lingo was going to work correctly in this MySQL database, but I was shocked to see, what looks like, MySQL adding my values together:
SELECT
identifier,
SUBSTR(cast(identifier as char),1,4) as 'p',
SUBSTR(cast(identifier as char),6) as 'q',
SUBSTR(cast(identifier as char),1,4)+SUBSTR(cast(identifier as char),6) as 'p+q'
FROM drawing_table where length(identifier)=9
order by identifier desc;
Here is a snippet of the output:
identifier,p,q,p+q
STO-00021,STO-,0021,21
STO-00020,STO-,0020,20
STO-00019,STO-,0019,19
STO-00018,STO-,0018,18
STO-00017,STO-,0017,17
STO-00016,STO-,0016,16
STO-00015,STO-,0015,15
STO-00014,STO-,0014,14
STO-00013,STO-,0013,13
STO-00012,STO-,0012,12
STO-00011,STO-,0011,11
STO-00010,STO-,0010,10
STO-00009,STO-,0009,9
STO-00008,STO-,0008,8
STO-00007,STO-,0007,7
STO-00006,STO-,0006,6
STO-00005,STO-,0005,5
STO-00004,STO-,0004,4
STO-00003,STO-,0003,3
STO-00002,STO-,0002,2
STO-00001,STO-,0001,1
STA-00166,STA-,0166,166
STA-00165,STA-,0165,165
STA-00164,STA-,0164,164
STA-00163,STA-,0163,163
STA-00162,STA-,0162,162
STA-00161,STA-,0161,161
STA-00160,STA-,0160,160
STA-00159,STA-,0159,159
STA-00158,STA-,0158,158
STA-00157,STA-,0157,157
STA-00156,STA-,0156,156
STA-00155,STA-,0155,155
STA-00154,STA-,0154,154
STA-00153,STA-,0153,153
STA-00152,STA-,0152,152
STA-00151,STA-,0151,151
STA-00150,STA-,0150,150
STA-00149,STA-,0149,149
STA-00148,STA-,0148,148
STA-00147,STA-,0147,147
STA-00146,STA-,0146,146
STA-00145,STA-,0145,145
STA-00144,STA-,0144,144
STA-00143,STA-,0143,143
STA-00142,STA-,0142,142
STA-00141,STA-,0141,141
STA-00140,STA-,0140,140
STA-00139,STA-,0139,139
STA-00138,STA-,0138,138
Why is MySQL trying to add my string values?
If you want to concatenate two strings you should use concat function
For example:
SELECT CONCAT('WAS-','0020')
Result
WAS-0020
Use CONCAT(SUBSTR(cast(identifier as char),1,4), SUBSTR(cast(identifier as char),6))
You have to use the CONCAT function instead of the + operator.
see: Mysql Manual
In your example this would be like:
CONCAT( SUBSTR(cast(identifier as char),1,4), SUBSTR(cast(identifier as char),6) ) as 'p+q'

Categories