For the navigation on my website, I created an index that is built from headline blocks. When you click on a headline in the index you scroll to that part in the page. One problem is that some headlines have non-alphanumeric content (like (, / or ?) that i don’t want to end up in the URL, and that I’d like to replace with a ‘_’
I understood that a regular expressions like
'/[^a-z0-9 ]/i' should work normally. But when I add this to
Str::replace() it does not replace any alphanumeric character (it does not react nor give an error)
Does Kirby need specific/different symbols to work? Or is there maybe another helper in Kirby that replaces non-alphanumeric characters?
<ul class="index"> <?php foreach ($page->process()->toBlocks() as $block): ?> <?php if ($block->type() == "heading"): ?> <li> <a href="#<?= Str::replace($block->excerpt(), '/[^a-z0-9 ]/i', '_' ) ?>"><?= $block->excerpt() ?></a> </li> <?php endif ?> <?php endforeach ?> </ul>