我们有一个像这样的字符串:
if (!function_exists('shop_attributes_in_loop')) {
function shop_attributes_in_loop(){
global $product;
$attributes = $product->get_attributes();
if(!empty($attributes)){
$attribute_single = array_keys($attributes);
$myArray = array();
echo '<div class="product_attributes">';
foreach ($attribute_single as $attribute => $value) {
$myArray[] = ucfirst($value);
}
echo implode(', ', $myArray).'</div>';
}
}
}
add_action('woocommerce_after_shop_loop_item', 'shop_attributes_in_loop');
目标是替换“merk,product”之间的逗号,并将逗号保持为“,”和“,&amp;”,这样我们就可以正确地拆分文件。
有什么建议吗?
亲切的问候
答案 0 :(得分:8)
首先,您应该查看Understanding CSV files and their handling in ABAP文章。
对于一次性工作,您可以使用此正则表达式(但请注意,如果字符串较长,可能效果不佳,请将其作为最后手段使用):
,(?!(?:[^"]*"[^"]*")*[^"]*$)
请参阅regex demo
模式详情:
,
- 逗号...... (?!
- 没有跟......
(?:
-
[^"]*
- 除"
"
- 双引号[^"]*"
- 见上文)*
- 上述分组模式的零个或多个序列[^"]*
- 除"
$
- 字符串结尾)
- 结束否定前瞻答案 1 :(得分:2)
我找到了比通过Accepted Answer使用Regex更好的答案。所以问题是我试图重新发明轮子。似乎SAP提供了一种用CSV读取结构的方法。
见下面的代码:
TYPES: BEGIN OF ttab,
rec(1000) TYPE c,
END OF ttab.
TYPES: BEGIN OF tdat,
userid(100) TYPE c,
activeuser(100) TYPE c,
firstname(100) TYPE c,
lastname(100) TYPE c,
middlename(100) TYPE c,
supervisor(100) TYPE c,
supervisor_firstname(100) TYPE c,
supervisor_lastname(100) TYPE c,
supervisor_middle(100) TYPE c,
scheduled_offering_id(100) TYPE c,
description(100) TYPE c,
domain(100) TYPE c,
registration(100) TYPE c,
current_registration(100) TYPE c,
max_registration(100) TYPE c,
item_type(100) TYPE c,
item_id(100) TYPE c,
item_revision_date(100) TYPE c,
revision_number(100) TYPE c,
title(100) TYPE c,
status(100) TYPE c,
start_date(100) TYPE c,
end_date(100) TYPE c,
location(100) TYPE c,
instructor_fistname(100) TYPE c,
instructor_lastname(100) TYPE c,
instructor_middlename(100) TYPE c,
column_number(100) TYPE c,
label(100) TYPE c,
value(100) TYPE c,
description2(100) TYPE c,
start_date_short(100) TYPE c,
begda TYPE begda,
start_time(100) TYPE c,
start_time_24_hour(100) TYPE c,
start_12_hour_type(100) TYPE c,
start_timezone(100) TYPE c,
end_date_short(100) TYPE c,
endda TYPE endda,
end_time(100) TYPE c,
end_time_24_hour(100) TYPE c,
end_12_hour_type(100) TYPE c,
end_timezone(100) TYPE c,
pernr TYPE pernr_d,
END OF tdat.
CONSTANTS: co_delete TYPE pspar-actio VALUE 'DEL',
co_attendance TYPE string VALUE '2002',
co_att_prelp TYPE prelp-infty VALUE '2002',
co_att_subty TYPE string VALUE '3000'.
DATA:
itab TYPE TABLE OF ttab WITH HEADER LINE,
idat TYPE TABLE OF tdat WITH HEADER LINE,
lw_idat LIKE LINE OF idat,
lw_found_training LIKE LINE OF idat,
file_str TYPE string,
lv_uname TYPE syuname,
lo_person TYPE REF TO zhr_cl_pa_person,
lv_input_time TYPE tims,
lv_output_time TYPE tims,
lv_day(2) TYPE c,
lv_month(2) TYPE c,
lv_year(4) TYPE c,
lv_time(6) TYPE c,
lv_abap_date TYPE string,
lv_lock_return LIKE bapireturn1,
ls_attendance LIKE bapihrabsatt_in,
lt_attendance_output TYPE TABLE OF bapiret2,
ls_return LIKE bapireturn,
ls_return1 LIKE bapireturn1,
lt_absatt_data TYPE TABLE OF pprop,
lw_absatt_data LIKE LINE OF lt_absatt_data,
lt_pa2002 TYPE TABLE OF pa2002,
lw_pa2002 LIKE LINE OF lt_pa2002,
lw_msg TYPE bapireturn1,
lt_p2002 TYPE TABLE OF p2002,
lw_p2002 LIKE LINE OF lt_p2002,
lc_pgmid TYPE old_prog VALUE 'ZKA_TEXT_UPDATE',
lr_upd_cluster TYPE REF TO cl_hrpa_text_cluster,
ls_text TYPE hrpad_text,
ls_pskey TYPE pskey,
lt_text_194 TYPE hrpad_text_tab,
lv_text TYPE string,
lo_ref TYPE REF TO cx_hrpa_invalid_parameter,
lw_struct TYPE tdat,
lo_csv TYPE REF TO cl_rsda_csv_converter.
CALL METHOD cl_rsda_csv_converter=>create
RECEIVING
r_r_conv = lo_csv.
CREATE OBJECT lr_upd_cluster.
*--------------------------------------------------*
* selection screen design
*-------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK selection1 WITH FRAME.
PARAMETERS: p_file TYPE localfile.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 4(51) text-002.
PARAMETERS p_futatt AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 4(51) text-001.
PARAMETERS p_active AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK selection1.
*--------------------------------------------------*
* at selection screen for field
*-------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
static = 'X'
CHANGING
file_name = p_file.
*--------------------------------------------------*
* start of selection
*-------------------------------------------------*
START-OF-SELECTION.
file_str = p_file.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = file_str
TABLES
data_tab = itab
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
OTHERS = 17.
*--------------------------------------------------------------------*
* Delete file headers
*--------------------------------------------------------------------*
DELETE itab INDEX 1.
*--------------------------------------------------*
* process and display output
*-------------------------------------------------*
LOOP AT itab .
CLEAR idat.
CALL METHOD lo_csv->csv_to_structure
EXPORTING
i_data = itab-rec
IMPORTING
e_s_data = lw_struct.
MOVE-CORRESPONDING lw_struct TO idat.
APPEND idat.
ENDLOOP.
答案 2 :(得分:1)
此任务不需要正则表达式。