Home > Net >  Wrap whole lines of uppercase text in bbcode bold tags
Wrap whole lines of uppercase text in bbcode bold tags

Time:12-16

I'm trying to write a regular expression for detecting and wrapping uppercase lines with [b]..[/b].

I have a description. This description varies, as it describes games and not all descriptions have a line consisting of only uppercase words.

How can I return a a string that has lines containing only uppercase words and wrap with those tags?

Example

BUILD THE ULTIMATE BRAWLER

Design your ultimate LEGO Minifigure hero with unique style, strategy and personality. A cactus with an attitude? A scrappy janitor ninja who happens to have fighting chickens? A sledgehammer-swinging clown with a few scores to settle? Sure! Build the ultimate brawler and level them up with unlockable content!

BATTLE THROUGH EPIC LEGO THEMES

From the swashbuckling shores of Barracuda Bay, to the waterlogged caverns of Ninjago® Seabound, to a dusty wild west saloon and the mythical Monkie Kid jungle, all of your favorite LEGO themes are brawlable! Collect unlockable minifigs, power-ups, and emotes along the way.

CROSS-PLAY WITH FRIENDS ACROSS PLATFORMS

Team up and brawl 4v4, party with friends, or play a battle royale-style game mode where it’s “every-player-for-themself”. With multiple game modes, levels have unique challenges and win conditions.

I want to return ALL of that, but with the detected uppercase lines each wrapped with [b] [/b].

My attempted pattern:

/([A-Z] (?: [A-Z] ) )\n/mg

https://regex101.com/r/GBLVLU/1

CodePudding user response:

We can try using preg_replace() here:

$input = "BUILD THE ULTIMATE BRAWLER\n\nDesign your ultimate LEGO Minifigure hero with unique style, strategy and personality. A cactus with an attitude? A scrappy janitor ninja who happens to have fighting chickens? A sledgehammer-swinging clown with a few scores to settle? Sure! Build the ultimate brawler and level them up with unlockable content!";
$output = preg_replace("/([A-Z] (?: [A-Z] ) )\n/", "[b]$1[/b]\n", $input);
echo $output;

This prints:

[b]BUILD THE ULTIMATE BRAWLER[/b]

Design your ultimate LEGO Minifigure hero with unique style, strategy and personality. A cactus with an attitude? A scrappy janitor ninja who happens to have fighting chickens? A sledgehammer-swinging clown with a few scores to settle? Sure! Build the ultimate brawler and level them up with unlockable content!

CodePudding user response:

You were on the right path with the m flag, but that flag has no meaning to a pattern with no anchors (^, $).

Using start of line and end of line anchors around a character class that contains only uppercase letters and spaces and hyphens.

Code: (Demo)

echo preg_replace('/^[A-Z -] $/m', '[b]$0[/b]', $text);

If your text may contain multibyte characters (such as accented characters), then add u after the m pattern modifier.


Alternatively, if you want to apply bold bbcode tags to full lines that do not contain any lowercase letters, then the following pattern uses a negated character class to match one or more characters on a single line that do not contain a lowercase letter (\p{Ll}) and no "vertical whitespaces". This is unicode/multibyte safe.

Code: (Demo)

echo preg_replace('/^[^\p{Ll}\v] $/mu', '[b]$0[/b]', $text);

CodePudding user response:

The following matches all uppercase lines, wraps lines with bbcode and returns text

echo preg_replace_callback('/([A-Z-] (?: [A-Z] ) )(\r\n|\r|\n)/', function($matches) { 
   return "[b]{$matches[0]}[/b]"; 
}, $string);
  • Related