working with textstrings
-
I would like to update an spceific textstring within the image caption.
Caption is blablablaREPLACEblablabla . The substring REPLACE should be repaced for instance with 2#105:Headline .I am thinking of something like
replace (in_caption; sub_string; with_headline)Hope you understand 🙂
Cheers, Ernst Wilhelm
-
Thanks for an interesting question. MLA does have a simple
,str_replace(s,r)option/format value but it does not support expanding a variable value such as[+iptc:2#105+]in the replacement portion. For that, you can use the regular expression functions added to resolve this earlier topic:For your application, you can use the
,extract(p,v)format/option value to break the Caption into three parts; prefix, match (REPLACE) and postfix. Then, you can use these parts in a Content Template to assemble your replacement value. You can find more information and examples in the “Regular Expression Features” section of the Settings/Media Library Assistant Documentation tab.I did some testing to develop a solution for your application and found a couple of small defects in the current MLA version. Thank you for giving me the opportunity to find and fix these problems. I have uploaded a new MLA Development Version dated 20190306 that contains the fixes you will need for your application. You can find step-by-step instructions for using the Development Version in this earlier topic:
PHP Warning on media upload with Polylang
Once you have installed the Development Version you can use a Content Template to update your Caption values. Here is the template I developed for your application:
template:([+post_excerpt,extract( '/(?<pre>^.*)(?<match>EXIF)(?<post>.*$)/', pre )+])([+iptc:2#105+]|[+matches:match+])([+matches:post+])The template has three parts. First:
([+post_excerpt,extract( '/(?<pre>^.*)(?<match>REPLACE)(?<post>.*$)/', pre )+])This part takes the Caption value (from the
post_excerptdatabase field) and applies a regular expression to divide it into three named subpatterns:(?<pre>^.*)matches everything from the start of the string up to the start of the replacement target.(?<match>REPLACE)matches the replacement target.(?<post>.*$)matches everything from the end of the replacement target to the end of the string.
The second argument of the
extractformat/option value,, pre ), returns the prefix portion of the Caption. The second template part is:([+iptc:2#105+]|[+matches:match+])This part returns the IPTC headline value, if present, or the original replacement target if no headline is available. The third part is:
([+matches:post+])This adds the postfix portion of the original Caption to the final result.
You can apply the template on a one-time basis using MLA’s Bulk Edit action in the Media/Assistant submenu:
- Navigate to the Media/Assistant admin screen.
- Check the box to the left of each item you want to edit.
- Pull down the “Bulk Actions” control and select “Edit”.
- Click “Apply” to open the Bulk Edit Area.
- Make the changes you want to apply to the selected items. For example, enter the complete template given above in the Caption text box in the right-hand column.
- Click “Update” in the lower-right corner of the Bulk Edit area to apply your changes.
- When the operation is complete, click “Refresh” to see the results.
The above solution has one dangerous property. If you apply the template to an item that does NOT contain the “REPLACE” literal the entire Caption will be replaced by the IPTC headline and the original content will be lost. Be careful to apply the template only to the items that need updating.
It would be great if you can install the Development Version and try the solution outlined above. I will leave this topic unresolved until I hear back from you. Thanks again for alerting me to this MLA defect and giving me a good example of regular expression processing.
It has been a month since my last post in this topic. I hope the MLA Development Version allowed you to accomplish your task.
I am marking this topic resolved, but please update it if you have any problems or further questions regarding the above suggestions. Thanks for your interest in the plugin.
The topic ‘working with textstrings’ is closed to new replies.