Dynamic Menu with a string replace

I’m wondering if theres a smarter way to do this. I have it working, but it seems like a lot of code for what I want.

I just want to list out pages that have a true for the featureinnav() field, and if the page title contains the word ‘for’, wrap it in a ‘sup’ tag.

Here’s what ive got. How can i clean it up?

<ul>
  <?php foreach ($pages->visible() as $sitenav): ?>
    <?php if ($sitenav->featureinnav()->bool()): ?>

    <?php
    $string = $sitenav->title()->html();
    $keys = array('for');
    $patterns = array();
    ?>

  <?php foreach ($keys as $key): ?>
    <?php $patterns[] = '/\b('.$key.')\b/i'; ?>
      <li><a <?php e($sitenav->isOpen(), ' class="active"') ?> href="<?= $sitenav->url() ?>"><?= preg_replace($patterns, '<sup>$0</sup>', $string) ?></a></li>
  <?php endforeach ?>

  <?php endif ?>
  <?php endforeach ?>
</ul>

If you have more than one key, you will have duplicate links…

Oh man. your right. Im not even sure i need a keys array. i based it on some code from stackoverflow that did multiple words in a single string. I will only ever have one key, which is the word ‘for’.

more head scratching…

This seems to do the trick:

<ul>
  <?php foreach ($pages->visible() as $sitenav): ?>
  <?php if ($sitenav->featureinnav()->bool()):
    $string = $sitenav->title()->html();
    $pattern = '/\b('.'for'.')\b/i';
  ?>

  <li><a <?php e($sitenav->isOpen(), ' class="active"') ?> href="<?= $sitenav->url() ?>"><?= preg_replace($pattern, '<sup>$0</sup>', $string) ?></a></li>

  <?php endif ?>
  <?php endforeach ?>
</ul>