How to remove the first letter in the text block?

In a text block, I highlight the initial visually.
The first letter in the text block is read out with short(1,'').
The first letter is to be subtracted from the remaining text.
I do this with <?= Str::substr($textblock->kt(),1) ?>
Unfortunately, html tags interfere with this, as they are also counted.
How can I ensure that html tags are ignored?
Is there a suitable field method?

Blueprint:

text: 
  type: writer
  label: Text

Template:

<?php if($textblock = $page->text()): ?>
<span class="first-letter first-letter-block bg-white text-black">
<?= $textblock->kt()->inline()->short(1,'') ?>
</span>
<?= Str::substr($textblock->kt(),1) ?>
<?php endif ?>

You should remove the letter before calling kt(), however, you then have to switch to the Kirbytext helper kirbytext($string).

On a sidenote, this if statement is useless, as $page->text() will always return a field object, check for isNotEmpty() instead.

Thank you for your prompt reply.

I have never worked with the Kirbytext helper kirbytext($string) and am therefore not familiar with how it works. Therefore, I apologize for my lack of knowledge.

Unfortunately, HTML tags are still displayed in the initial text, but no longer in the subsequent text, where I used Str::afterStart instead of Str::substr.

<?php if ($page->text()->isNotEmpty()): ?>
<span class="first-letter first-letter-block bg-white text-black">
<?php $textblock = $page->text();?>
<?= kirbytext($textblock->short(1,'')) ?>
</span>
<?= Str::afterStart($textblock->kt(),1) ?>
<?php endif ?>

Text: <p><strong>Leben und Schönheit, Bios und Ästhetik</strong> – die Verbindung von natürlichen Inhaltsstoffen, modernster Forschung und Leidenschaft für die Schönheit bestimmen seit dem ersten…

What type of field is this? If it is a writer field, then it will not work (but then I don’t know why you use Kirbytext on that field)

Yes, that’s right. I used a writer field. I have now used a textarea field:

text: 
  type: textarea
  buttons:
    - bold
    - italic
  label: Text

… but the result is unfortunately unchanged, with the following output.
Shouldn’t all HTML tags be removed with the function “->kt()->inline()”?

<?php if ($page->text()->isNotEmpty()): ?>
<span class="first-letter first-letter-block bg-white text-black">
<?php $textblock = $page->text();?>
<?= Str::short($textblock->kt()->inline(),1,'') ?>
</span>
<?= Str::afterStart($textblock->kt(),1) ?>
<?php endif ?>

You are still passing the rendered Html to Str::short(), while you need to pass just the value of the field before applying kirbytext in each case, see above.

So:

<?= kirbytext(Str::short($textblock->value(), 1, '')) ?>
<?= kirbytext(Str::after($textblock->value(), 1, '')) ?>

Note that if your users put html into the field or use a kirbytag at the beginning of a line, this will still fail and I don’t know if there is a good way to account for all edge cases without breaking stuff

Many thanks for the explanations of the modified code.
It looks logical and tidy.
However, the output is empty :unamused:

For a better understanding, I will list all areas again here:

Blueprint with textarea-field:

text: 
  type: textarea
  buttons:
    - bold
    - italic
  label: Text

.txt-file:

----

Text:

**Leben und Schönheit, Bios und Ästhetik** – die Verbindung von natürlichen Inhaltsstoffen, modernster Forschung und Leidenschaft für die Schönheit bestimmen seit dem ersten Tag die Philosophie. 

Höchste Qualität ist nicht einfach eine Möglichkeit, sie ist die Voraussetzung. Sie ist die Wurzel – für die Culture Of Total Beauty.

----

Output in the template:

<?php if ($page->text()->isNotEmpty()): ?>
<span class="first-letter first-letter-block bg-white text-black">
<?php $textblock = $page->text();?>
<?= kirbytext(Str::short($textblock->value(), 1, '')) ?>
</span>
<?= kirbytext(Str::after($textblock->value(), 1, '')) ?>
<?php endif ?>

Output in the browser:

It even outputs a list that does not exist. The list item is therefore also visible in the initial element:

Source code:

<span class="first-letter first-letter-block bg-white text-black">
<ul>
<li>
</li>
</ul>
</span>

Note that if your users put html into the field or use a kirbytag at the beginning of a line, this will still fail and I don’t know if there is a good way to account for all edge cases without breaking stuff

That will not be the case. Fortunately.

Well, it’s another one of those edge cases I mentioned. Your text starts with markdown **

1 Like

If Markdown and HTML tags make things too complex, I’d rather make a compromise and do without text formatting. However, my output only works with this code:

<?php if ($page->text()->isNotEmpty()): ?>
<span class="first-letter first-letter-block bg-white text-black">
<?php $textblock = $page->text();?>
<?= Str::short($textblock->kt()->inline(), 1, '') ?>
</span>
<?= Str::substr($textblock->kt()->inline(), 1) ?>
<?php endif ?>

Thank you for your effort and the useful explanations.
With your help I was able to learn more about Kirby again. :man_technologist: