A few months back I was working on a Drupal 7 to Drupal 10 migration. I also migrated a WordPress blog into the site. Some of the content dated back close to a decade and was littered with inline styling. Think style tags inside the content setting the font size and font-family. I wanted to get rid of that to make the content look better.
I found some old code on GitHub and wondered if it worked. It did.
Then I started having fun with it. I've had ChatGPT generate code and comments for me so I decided to see what happened when I asked for humorous things. I asked for code styled as an episode of Seinfeld, as if George Carlin wrote it, as well as with Star Wars and Back to the Future twists. The results are great.
Seinfeld
<?php
/**
* @file
* Primary module hooks for Remove Inline CSS module.
* "The one where George tries to fix the internet."
*/
/**
* Implements hook_update_N().
* "The episode begins in a drab office, where George discovers his true calling."
*/
function remove_inline_css_update_1005() {
/**
* For removing inline CSS from body fields in Drupal.
* Jerry's voice: "George decides to cleanse the internet of its inline CSS, embarking on a digital diet."
*
* -Install devel module.
* -Navigate to devel/php.
* -Paste the following code and execute.
* Kramer, with enthusiasm: "It's like giving the web a spa day!"
*/
// Elaine, puzzled: "So, you're telling me you're going through the internet's dirty laundry?"
$results = db_select('field_data_body', 'f')
->fields('f', ['entity_id', 'body_value']) // George, proudly: "First, you gotta find the dirt."
->execute() // "The moment of truth," whispers George.
->fetchAll(); // "Gotcha!" George exclaims, a little too loudly.
// Kramer, sliding in: "Now the real magic happens, right, Georgie?"
foreach ($results as $result) {
// Jerry, skeptically: "You're just going to pull the rug from under the CSS?"
$body = preg_replace(
'/(<[^>]*) style=("[^"]+"|\'[^\']+\')([^>]*>)/i', // The magic spell to strip the style away.
'$1$3', // "Abracadabra," George mutters, unsure of the incantation.
$result->body_value // The garment now bare, stripped of its inline styles.
);
// Elaine, curious: "And then what? You just put it back like nothing happened?"
db_update('field_data_body')
->fields([
'body_value' => $body, // "Voilà, as good as new," George attempts a French accent.
])
->condition('entity_id', $result->entity_id) // "Making sure it goes back to the right closet," Kramer adds helpfully.
->execute(); // The deed is done. George stands back, a mixture of pride and fear.
}
// Jerry, as the function ends: "And just like that, George becomes a web designer. Or so he thinks."
}
George Carlin
<?php
/**
* You ever notice how websites are like a teenager's bedroom? Stuff thrown everywhere, especially these styles right inline with the content. Yeah, we're here to clean that mess up, kinda like enforcing a "no food in the bedroom" rule but for CSS in HTML.
*/
/**
* Here we are, playing the role of the digital janitor.
*/
function remove_inline_css_update_1005() {
/**
* This bit here is like telling you how to clean up your room. Only your room is a website, and your clothes are CSS. Too much analogy? Too bad.
*
* Here's the game plan:
* - First, you pretend to be a developer and install some fancy tool called 'devel module'.
* - Next, you find this hidden alley in your site called 'devel/php'.
* - You copy this spell I'm about to show you, chant it into the void, and voila, your website is less of a mess.
*/
// Alright, let's roll up our sleeves and dive into the dumpster.
$results = db_select('field_data_body', 'f') // We're rummaging through the database like it's a discount bin.
->fields('f', ['entity_id', 'body_value']) // What we're stealing... I mean, borrowing: IDs and the mess itself.
->execute() // Light this candle!
->fetchAll(); // And here we are, a treasure trove of... junk.
// Now, we've got to clean each piece, one at a time. Patience, grasshopper.
foreach ($results as $result) {
// Here's where the magic happens. We're going to scrub the inline CSS off like it's graffiti on a subway car.
$body = preg_replace(
'/(<[^>]*) style=("[^"]+"|\'[^\']+\')([^>]*>)/i', // The magic eraser. Don't ask me how it works; it's regex voodoo.
'$1$3', // Strip it down, baby. We're making it modest.
$result->body_value // The victim of our cleaning spree.
);
// Now, we put the cleaned-up content back where we found it, like nothing ever happened.
db_update('field_data_body')
->fields([
'body_value' => $body, // Fresh and so clean.
])
->condition('entity_id', $result->entity_id) // This is how we make sure we're not throwing someone else's stuff out.
->execute(); // Bam! Done. Like it never happened.
}
// And there you have it, folks. Digital hygiene, Carlin style. Because sometimes, you gotta clean up the internet, one piece of inline CSS at a time.
}
Star Wars
<?php
/**
* In a galaxy far, far away, websites are under the tyranny of inline CSS styles.
* This script is the chosen one, prophesized to bring balance to the Code.
*/
/**
* Our Rebel function embarks on its mission, codenamed Update 1005.
*/
function remove_inline_css_update_1005() {
/**
* The mission briefing:
* Like a Jedi with a lightsaber, you must be precise and cautious.
* Your targets: the field_data_body tables, where the Dark Side has hidden its inline styles.
*
* The plan:
* - Use the force, or in this case, the devel module, to access your powers.
* - Navigate the treacherous paths to 'devel/php', the heart of your Drupal starship.
* - With the code below, unleash a wave of force that will cleanse the site of darkness.
*/
// Our first move: Scouting the enemy territory.
$results = db_select('field_data_body', 'f')
->fields('f', ['entity_id', 'body_value']) // Locking onto the targets.
->execute() // Engaging hyperdrive.
->fetchAll(); // The spoils of our reconnaissance.
// With the targets in sight, it's time to strike.
foreach ($results as $result) {
// The lightsaber duel against inline CSS begins.
$body = preg_replace(
'/(<[^>]*) style=("[^"]+"|\'[^\']+\')([^>]*>)/i', // The Force, distilled into a regex pattern, cutting through darkness.
'$1$3', // A swift, clean cut.
$result->body_value // The battleground.
);
// Victory is close; we must now ensure the galaxy remembers our deeds.
db_update('field_data_body')
->fields([
'body_value' => $body, // The planets, freed from the Empire's grasp.
])
->condition('entity_id', $result->entity_id) // Ensuring no innocent planets are caught in the crossfire.
->execute(); // The Death Star of inline CSS, destroyed.
}
// As the dust settles, the galaxy breathes a sigh of relief.
// Inline CSS has been vanquished, for now. But remember, young Padawan,
// the fight for clean, maintainable code is never-ending.
}
Back to the Future
<?php
/**
* "Great Scott!" Doc exclaimed. "We've discovered that inline CSS is disrupting the space-time continuum of web development!"
* Marty, with a puzzled look: "So, you're telling me, Doc, that by removing this inline CSS, we're... we're cleaning up the future of the web?"
* Doc nods vigorously: "Exactly, Marty! It's our responsibility to ensure a cleaner, more maintainable web for future generations!"
*/
/**
* The function is prepared for its critical mission. Doc punches in the destination time: Update 1005.
*/
function remove_inline_css_update_1005() {
/**
* The plan is laid out on the workbench:
* Marty: "So, first we need to install this 'devel module' thing, right?"
* Doc, pointing at the screen: "Precisely! That's our entry point. Then we head to 'devel/php'—our version of 88 miles per hour."
* Marty, cracking his knuckles: "Let's do this. Let's make the web a better place."
*/
// The duo starts their operation, scanning the database for anomalies—inline CSS.
$results = db_select('field_data_body', 'f')
->fields('f', ['entity_id', 'body_value']) // Targeting the affected areas.
->execute() // Activating the flux capacitor.
->fetchAll(); // A successful scan. They've got what they need.
// With the targets in sight, Marty and Doc get to work.
foreach ($results as $result) {
// Marty, armed with regex, begins the cleanup operation.
$body = preg_replace(
'/(<[^>]*) style=("[^"]+"|\'[^\']+\')([^>]*>)/i', // The regex, more powerful than a bolt of lightning.
'$1$3', // The inline CSS disappears, like fading photographs.
$result->body_value // The document, now part of a cleaner timeline.
);
// They carefully replace the cleaned content, ensuring history is preserved.
db_update('field_data_body')
->fields([
'body_value' => $body, // The future of the web, now free of clutter.
])
->condition('entity_id', $result->entity_id) // Making sure they're correcting the right timeline.
->execute(); // The operation is a success. The web is safe, for now.
}
// As they lean back, Doc turns to Marty with a grin: "We did it, Marty! The future of web development is secure!"
// Marty, looking at the cleaner code, can't help but smile: "This is heavy, Doc. Heavy-duty web development!"
}
The Office
<?php
/**
* "Dunder Mifflin Infinity 2.0" - Michael insists the website needs a revamp to be more like "the Facebook."
* The task falls to the unlikely duo of Dwight and Ryan to remove inline CSS, making the site as efficient as possible.
*/
/**
* Ryan, appointed as the temporary tech lead, introduces the project in the conference room.
*/
function remove_inline_css_update_1005() {
/**
* Ryan (talking head): "Michael wants the website to be hip. Removing inline CSS is like... making our website wear a suit. Every day."
* Dwight chimes in, "Exactly. And not just any suit. A Schrute Farms beet-colored suit."
*
* The plan:
* - Dwight suggests using the 'devel module' as if it were a beet harvester for code.
* - Ryan, wanting to appear sophisticated, agrees but plans to Google what that means later.
* - Michael walks by, overhears, and asks if "Devel" is a new social media platform.
*/
// Cut to the bullpen where Jim pretends to understand what's happening but is really launching rubber bands at Dwight's desk.
$results = db_select('field_data_body', 'f') // Ryan, looking focused, trying to appear like he knows what he's doing.
->fields('f', ['entity_id', 'body_value']) // Dwight claims he can identify inline CSS by scent.
->execute() // Pam, offering moral support, "You got this, guys."
->fetchAll(); // Kevin, misunderstanding the task, asks if this will help him win at poker.
// Angela criticizes the operation for not being Christian enough while Oscar tries to explain what inline CSS actually is.
foreach ($results as $result) {
// Dwight declares, "This is the moment I've been training for my entire life."
$body = preg_replace(
'/(<[^>]*) style=("[^"]+"|\'[^\']+\')([^>]*>)/i', // Meredith, uninterested: "Does this mean we get a longer lunch break?"
'$1$3', // Creed, to the camera: "I've been removing CSS since the '80s. They used to call me 'The Cleaner.'"
$result->body_value // Toby tries to contribute, but Michael shuts him down: "Nobody cares, Toby!"
);
// Jim uses this moment to prank Dwight by convincing him the website is now sentient.
db_update('field_data_body')
->fields([
'body_value' => $body, // Stanley, unfazed, continues his crossword puzzle.
])
->condition('entity_id', $result->entity_id) // Phyllis quietly knits a sweater, oblivious to the tech talk.
->execute(); // Ryan proudly announces completion, while Pam prints out a screenshot for the Scranton history museum.
}
// Michael, in a talking head, concludes: "And that, my friends, is how Dunder Mifflin enters the digital age... by removing styles or something."
}
Now I want to throw an entire codebase at ChatGPT and have it write humorous comments.