Forum

Thread tagged as: Question, Discussion

Remove spaces from field

Is there a way to remove spaces from a text field.

I have tried using replace=" |" but it doesn't seem to do anything.

For example, I have a simple text field holding a phone number. the phone number is entered like 01452 000 000 but I would like to output like 01452000000.

Marc Sanders

Marc Sanders 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You'd need to use a template filter to achieve that.

https://docs.grabaperch.com/api/template-filters/

Thats great, didn't realise this was available - Got a simple one setup and working.

<?php
    class PerchTemplateFilter_space extends PerchTemplateFilter {
        public function filterAfterProcessing($value, $valueIsMarkup = false) {
            if ($this->Tag->type == 'text') {
            return str_replace(' ', '', $value);
          }
        }
    }

    PerchSystem::register_template_filter('space', 'PerchTemplateFilter_space');

Thank you Drew