Replace field method with regular expression support …

Dear community,

for creating id/class names from field’s contents, I would like to replace all non-word-characters from a field with a hyphen.

I thought about something like:

<?php echo $testimonial->company()->lower()->str_replace('\W', '-') ?>

\W meaning non-word-character in regular expression. I think such field methods don’t exist yet, do you think it’s a good idea to add a replace-method?

PS: I just discovered the escape method, maybe that’s a solution to this task, but I think replacing parts of field’s contents is a great feature. : )

You can use the str::slug() method to convert strings to lower case and ASCII at the same time. That’s what Kirby uses to convert titles to URLs:

<?php echo str::slug($testimonial->company()) ?>

If you need it in many places, you can create a custom filed method for that.

2 Likes

@pedroborges That works like a charm, thank you very much. : )