Home > Mobile >  Wrapping Uppercase Lines With Tag
Wrapping Uppercase Lines With Tag

Time:12-16

Regex 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);
  • Related