Skip Navigation

[Resolved] Apply RegEx to a field value in a view

This support ticket is created 3 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 4 replies, has 2 voices.

Last updated by philippeS-4 3 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#2127617

I have a view which pulls a list of PDFs from the media library - the filename (post title) contains information that I don't want to display in my view. Basically I am only interested in the last 2 words.

Here is a sample of the post title:
HN 35 1 January 2021
HN V32 No11 November 2018
HN V33 No2 February 2019

The following RegEx would return the last 2 words: (?:\S*\s*){2}$

Is it possible to apply this RegEx to the value of a field value or any other way of getting the same result?
Thanks,
Phil

#2127939

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

HI Phil,

Thank you for getting in touch.

If the titles are formatted as in your examples then you won't necessarily need REGEX to strip the last 2 words, you can actually trim them.

However to confirm you want "HN 35 1 January 2021" to be "January 2021" Correct?

Please let me know.
Thanks,
Shane

#2127941

Yes that is correct! Just the last 2 words!
Thanks,
Phil

#2127987

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Phil,

You should be able to do it with this shortcode below.

function wp_return_last_words( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'string' => '',
		),
		$atts
	);

	$text_arr = explode(' ',$atts['string']);
    $length = count($text_arr);
	return $text_arr[$length-2].' '.$text_arr[$length-1];

}
add_shortcode( 'wp_return_last_words', 'wp_return_last_words' );

Just add this to the Toolset Custom code section in Toolset-> Settings -> Custom code and activate it.

The code can be used like this [wp_return_last_words string='[wpv-post-title]']

Please let me know if this helps.
Thanks,
Shane

#2128061

My issue is resolved now. Thank you!