RegEx ,replace() function with arrays: Fails with Uncaught TypeError
-
1) Question to please answer here and in the help/docs:
Regular Expression Features
/wp-admin/options-general.php?page=mla-settings-menu-documentation&mla_tab=documentation#mla_regular_expressions
The ,replace(p,r,v) option/format value
To specify an array argument enclose the argument in braces ( “{” and “}” ) and separate elements with spaces. For a simple example,
,replace( {'/,/' '/-/'}, {' ' '_'} )
will change commas to spaces and dashes to underscores, while,replace( {'/,/' '/-/'}, ' ' )
will change both commas and dashes to spaces.Question:
If you replace an array with an equally long array like
{'/from1/' '/from2/' '/from3/'}, {'to1' 'to2' 'to3'}
do all transformations happen in one pass, or are the transformation pairs processed in a pipe?- That is the whole string gets processed through the first from→to pair,
- then the second from→to pair,
- and then eventually the last from→to pair?
In my example:
{'/\s+-\s+/' '/\s+/' '/?/'}, {'?' '-' '--'}
- Transform the special sequence space-hyphen-space to a bullet as my temporary unique placeholder.
- Then transform any successive whitespace characters uniformly to a single hyphen.
- Then transform my temporary unique placeholder, the bullet, into a double hyphen.
Example input:
Main topic - Sub topic - Remark at the end.jpg
Should become:
Main-topic--Sub-topic--Remark-at-the-end.jpg
If this is not processed in a pipe, but in a single pass, then I would simply need to run 3 consecutive Bulk Edit UI operations, each containing only a single from→to pair. Which is also ok if needed only rarely.
2) Usability feedback: I think the syntax of two separate arrays whose pairs are determined by their positional counterpart is possibly closest to the technical model you use, but not at all the mental model of the user intent. Easier to read and mentally process would have been a single array with consecutive from/to pairs, like replace{ ‘/from1/’ , ‘to1’ , ‘/from2/’ , ‘to2’ , ‘/from3/’ , ‘to3’ }. Just a feedback.
3) Bug report: RegEx replace() function with arrays fails with Uncaught TypeError for trim()
- WordPress → Media → Assistant → Selecting 3 images > Bulk actions → Apply → Bulk Edit UI opens
- Filling “ALT Text” with the help example:
template:[+post_title,replace( {'/,/' '/-/'}, {' ' '_'} )+]
- Results in this error message in the Admin UI:
An ajax.fail error has occurred. Please reload the page and try again. (error), jqXHR( 500, error, <p>There has been a critical error on this website.</p><p><a href="https://www.ads-software.com/documentation/article/faq-troubleshooting/">Learn more about troubleshooting WordPress.</a></p>)
- And this in php_error.log :
[23-May-2023 08:06:46 UTC] PHP Fatal error: Uncaught TypeError: trim(): Argument #1 ($string) must be of type string, array given in /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-data.php:1260 Stack trace: #0 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-data.php(1260): trim() #1 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-data-source.php(500): MLAData::mla_apply_field_level_format() #2 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-data-source.php(179): MLAData_Source::_evaluate_data_source() #3 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-options.php(860): MLAData_Source::mla_get_data_source() #4 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-main.php(984): MLAOptions::mla_get_data_source() #5 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-main.php(1046): MLA::_process_bulk_value() #6 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-main.php(1386): MLA::mla_prepare_bulk_edits() #7 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-main.php(1877): MLA::mla_process_bulk_action() #8 /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-main.php(1898): MLA::_bulk_edit_ajax_handler() #9 /data/web/customer-id/html/apps/domain-directory/wp-includes/class-wp-hook.php(308): MLA::mla_inline_edit_ajax_action() #10 /data/web/customer-id/html/apps/domain-directory/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters() #11 /data/web/customer-id/html/apps/domain-directory/wp-includes/plugin.php(517): WP_Hook->do_action() #12 /data/web/customer-id/html/apps/domain-directory/wp-admin/admin-ajax.php(188): do_action() #13 {main} thrown in /data/web/customer-id/html/apps/domain-directory/wp-content/plugins/media-library-assistant/includes/class-mla-data.php on line 1260
- The topic ‘RegEx ,replace() function with arrays: Fails with Uncaught TypeError’ is closed to new replies.