Roger Creasy
Roger Creasy

Reputation: 1529

How do I select the 1st n characters or through the fist "<"?

I am auto-generating an incipit (first bit of a string as an intro) using regex. The incipit is used in a Twig template. The original string can include html. If my incipit ends within html tags, it breaks the Twig template. I want to get the first 260 characters OR all characters from the beginning of the string to the first <, whichever is fewer characters.

This works for first 260 characters:

if (preg_match('/^.{1,260}\b/s', $body, $match)) {
    $incipit=$match[0]; 
    if (strlen($body >= 260)) {
        $incipit .= "&hellip;"; //adding ellipsis only if body is truncated 
    } 
}

I tried adding an or within my regex.

preg_match('/^.{1,260}\b|[^>]/s', $body, $match))

But, that does not work the | seems to be ignored.

Upvotes: 1

Views: 72

Answers (1)

rock321987
rock321987

Reputation: 11042

First thing first. You don't need word boundary i.e. \b.

OR condition won't be useful here and can create a conflict if both conditions are satisfied (i.e. < is also present and the characters is less than 260).

You can use this regex instead

^[^<]{1,260}

regex demo

Regex Breakdown

^ #Start of string
[^<] #Negated character class. Match anything other than <
{1,260} # Repeat it between 1 to 260 chaarcters

This regex will work according to whichever condition satisfies first.

Upvotes: 2

Related Questions