Skip to content

Instantly share code, notes, and snippets.

@mcsf
Created May 20, 2024 11:35
Show Gist options
  • Save mcsf/c489db0ae5783c975187754235277354 to your computer and use it in GitHub Desktop.
Save mcsf/c489db0ae5783c975187754235277354 to your computer and use it in GitHub Desktop.
<?php
// Two sample posts: one with lots of blocks, including groups; another with
// just a brief paragraph mentioning the word "paragraph".
$sample1 = "<!-- wp:paragraph -->\n<p>This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:group {\"layout\":{\"type\":\"flex\",\"flexWrap\":\"nowrap\"}} -->\n<div class=\"wp-block-group\"><!-- wp:quote -->\n<blockquote class=\"wp-block-quote\"><!-- wp:paragraph -->\n<p>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin' caught in the rain.)</p>\n<!-- /wp:paragraph --></blockquote>\n<!-- /wp:quote -->\n\n<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\"><!-- wp:paragraph -->\n<p>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</p>\n<!-- /wp:paragraph --></blockquote>\n<!-- /wp:quote --></div>\n<!-- /wp:group -->\n\n<!-- wp:paragraph -->\n<p>As a new WordPress user, you should go to <a href=\"http://localhost:8881/wp-admin/\">your dashboard</a> to delete this page and create new pages for your content. Have fun!</p>\n<!-- /wp:paragraph -->";
$sample2 = "<!-- wp:paragraph -->\n<p>This is a smaller sample in which we actually discuss the term \"paragraph\".</p>\n<!-- /wp:paragraph -->";
// Set up a mock SQLite3 database and insert the sample posts in it
$db = new PDO("sqlite::memory:");
$db->exec("CREATE TABLE posts ( id INTEGER PRIMARY KEY, post_content TEXT)");
$stmt = $db->prepare("INSERT INTO posts (post_content) VALUES (?)");
$stmt->execute([$sample1]);
$stmt->execute([$sample2]);
// First, do a naive search for "paragraph", revealing that both posts matched
$result = $db->query(
"SELECT id FROM posts WHERE post_content LIKE '%paragraph%'"
);
var_dump(count($result->fetchAll()));
// Then, register PHP's own 'strip_tags' function to use in SQL statements, and
// repeat the search but now filtering 'post_content' through the function in
// the LIKE operation. Observe that only the second post matched.
$db->sqliteCreateFunction('strip_tags', 'strip_tags', 1);
$result = $db->query(
"SELECT id FROM posts WHERE strip_tags(post_content) LIKE '%paragraph%'"
);
var_dump(count($result->fetchAll()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment