Add str to php array tool

This commit is contained in:
Illya Marchenko 2023-12-12 11:45:57 +02:00
parent 6a860633b2
commit 3b0517161f
Signed by: stuzer05
GPG Key ID: A6ABAAA9268F9F4F
4 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,58 @@
<?php
// Allow from any origin
if (isset($_SERVER['HTTP_ORIGIN'])) {
// Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
// you want to allow, and if so:
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
// may also be using PUT, PATCH, HEAD etc
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$input = json_decode(file_get_contents('php://input'), true);
$data = $input['data'];
$data_delimiter = $input['delimiter'];
function var_export_short($data, $return=true) {
$dump = var_export($data, true);
$dump = preg_replace('#(?:\A|\n)([ ]*)array \(#i', '[', $dump); // Starts
$dump = preg_replace('#\n([ ]*)\),#', "\n$1],", $dump); // Ends
$dump = preg_replace('#=> \[\n\s+\],\n#', "=> [],\n", $dump); // Empties
if (gettype($data) == 'object') { // Deal with object states
$dump = str_replace('__set_state(array(', '__set_state([', $dump);
$dump = preg_replace('#\)\)$#', "])", $dump);
} else {
$dump = preg_replace('#\)$#', "]", $dump);
}
if ($return===true) {
return $dump;
} else {
echo $dump;
}
}
switch ($input['mode']) {
case 'json':
echo var_export_short(@json_decode($data, true));
break;
default:
echo var_export_short(explode(empty($data_delimiter) ? "\n" : $data_delimiter, $data));
break;
}

View File

@ -34,6 +34,7 @@ export default {
'str_sort_lines': 'Str sort lines',
'str_to_lower_upper': 'Str to lower/upper',
'str_remove_duplicate_lines': 'Str remove duplicate lines',
'str_to_php_array': 'Str to PHP array',
'url_encode_decode': 'URL encode/decode',
},
'SQL': {

View File

@ -35,6 +35,11 @@ const router = createRouter({
name: 'str_remove_duplicate_lines',
component: () => import('../views/StrRemoveDuplicateLines.vue'),
},
{
path: '/str_to_php_array',
name: 'str_to_php_array',
component: () => import('../views/StrToPHPArray.vue'),
},
{
path: '/fix_ru_en_keyboard',
name: 'fix_ru_en_keyboard',

View File

@ -0,0 +1,61 @@
<template>
<h2 class="tool-title">Str to PHP array</h2>
<hr class="mt-5 mb-5">
<div class="input-group">
<label for="data">Data</label>
<textarea id="data" v-model="toolData.data" v-on:change="result" style="height: 150px"></textarea>
</div>
<div class="input-group">
<label for="data_delimiter">Delimiter</label>
<input id="data_delimiter" v-model="toolData.dataDelimiter" placeholder="," type="text" v-on:change="result" :disabled="toolData.dataIsJson">
<div>
<input id="data_is_json" name="data_is_json" v-model="toolData.dataIsJson" v-on:change="result" type="checkbox"> <label for="data_is_json">is json</label>
</div>
</div>
<hr class="mt-5 mb-5">
<div class="input-group">
<label for="result">Result</label>
<textarea id="result" v-model="toolResult" style="height: 150px"></textarea>
</div>
</template>
<script>
import axios from 'axios'
import { config } from '../../config';
export default {
data() {
return {
toolData: {
data: '',
dataDelimiter: '',
dataIsJson: '',
},
toolResult: '',
};
},
methods: {
result() {
axios.post(`${config.APP_URL}/api/php/data_to_php_array.php`, {
data: this.toolData.data,
delimiter: this.toolData.dataDelimiter,
mode: this.toolData.dataIsJson ? 'json' : null,
})
.then((response) => {
this.toolResult = response.data;
Promise.resolve(response);
});
}
}
}
</script>
<style lang="scss">
</style>