Extract string between characters (flexible) • Online NC
  • PHP

Extract string between characters (flexible)

Easily isolate specific parts of a string with start and end characters.
  • Modifié il y a 5 mois
  • Share
Extract String Between Characters Flexible

Improve the user experience by trimming unnecessary data and showing only the most relevant part of a string. By defining flexible start and end characters, you can clean up dynamic content for clearer and more professional displays.

Useful for:

✅ Extracting values from structured or delimited strings

✅ Cleaning up URLs, slugs, or identifiers

✅ Displaying clean titles or references in UI elements

✅ Supporting moderation or filtering logic with better control over input

1️⃣ Paste the code into your child theme’s functions.php file.

2️⃣ Edit your Elementor Template and add the text, button, action list, or other widget where you want the content to appear.

3️⃣ Insert any Voxel dynamic tag containing a string > Add a modifier > Select Cut before and/or after characters (flexible) > Save.

4️⃣ Enter the characters for:

Start character (optional): content before this will be removed
End character (optional): content after this will be removed

You should now see:
@post(content).cut_before_and_after_char(‘|’,’/’)

This modifier is flexible:

If only the start character is provided, everything after it is returned.
If only the end character is provided, everything before it is returned.
If both are provided, only the part in between is returned.

				
					// File : Extract string between characters (flexible).php
add_filter( 'voxel/dynamic-data/modifiers', function( $modifiers ) {

class Cut_Before_And_After_Char extends \Voxel\Dynamic_Data\Modifiers\Base_Modifier {
    public function get_label(): string {
        return 'Cut before and/or after characters (flexible)';
    }

    public function get_key(): string {
        return 'cut_before_and_after_char';
    }

    protected function define_args(): void {
        $this->define_arg( [
            'type' => 'text',
            'label' => 'Start character (cut before this)',
        ] );
        $this->define_arg( [
            'type' => 'text',
            'label' => 'End character (cut after this)',
        ] );
    }

    public function apply( string $value ) {
        $start_char = $this->get_arg(0);
        $end_char = $this->get_arg(1);

        if ( empty( $value ) ) {
            return $value;
        }

        $start_char = html_entity_decode($start_char);
        $end_char = html_entity_decode($end_char);

        $start_char = $this->clean_escaped_characters( $start_char );
        $end_char = $this->clean_escaped_characters( $end_char );

        if ( !empty( $start_char ) ) {
            $start_pos = strpos( $value, $start_char );
            if ( $start_pos !== false ) {
                $start_pos += strlen($start_char);
                $value = substr( $value, $start_pos );
            }
        }

        if ( !empty( $end_char ) ) {
            $end_pos = strpos( $value, $end_char );
            if ( $end_pos !== false ) {
                $value = substr( $value, 0, $end_pos );
            }
        }

        return trim( $value );
    }

    private function clean_escaped_characters( $char ) {
        if ( $char === '\\' ) {
            return '\\';
        }
        return str_replace('\\', '', $char);
    }
}

$modifiers['cut_before_and_after_char'] = \Cut_Before_And_After_Char::class;
return $modifiers;
});


				
			
  • Kevin Ekelmans Presario