New Website

I've made a new website, as lynx.io is dead. You can find it here: http://macr.ae/—it's similar in format to lynx.io, but has better articles, and they're all written by me.

JavaScript disabled

While it will still mostly work, a lot of this site's functionality relies on JavaScript - please enable it for the best experience.

Using preg_replace as a word censor or for smilies

In this article I will be demonstrating a basic word censor / smilie parser that you can use on your website. First I will give you the code, then I will explain how to use it, and finally I will tell you how it works.

The code:

<?php

function parse($text, $smile = true, $swears = true) {
    // Checks to see whether word censoring is enabled
    // If it is, censor the words in the array $words
    if ($swears) {
        $words = array(
            'badword' => 'bad****',
            'baad' => '****',
        );
        $text = preg_replace(array_keys($words), $words, $text);
    }

    // Checks to see whether smilies are enabled
    // If it is, use the smilies specifies in the array $smilies
    if ($smile) {
        $smilies = array(
            ':)' => 'smile.png',
            ':D' => 'happy.png',
            ':(' => 'sad.png',
        );
        $image_location = '/images/smilies/';

        foreach($smilies as $value) {
            $smilies_img[] = $image_location . $value;
        }

        $text = preg_replace(array_keys($smilies), $smilies_img, $text);
    }

    return $text;

   }

Using this function is simple:

parse($text_to_parse) or parse($text_to_parse, 0) if you want to disable smilies, or parse($text_to_parse, 1, 0) to disable the word censor.

How it works

The code is in two sections - the word censor is in the first part of the code, while the smilies are in the second part of code. The word censor is much simpler than the smilie converter, because the smilie converter has to cycle through all the smilies specifying a location where the images are stored (specified in the $image_location variable). It’s pretty self explanatory.

About Callum Macrae:

Callum Macrae is the founder of lynx.io and a JavaScript developer from the United Kingdom. He is currently writing his first book, to be published by O'Reilly Media.

You can view more articles by this author here.

Tags: php scripts preg_replace regex

Comments

No comments; leave one below.

says:

Add comment

 

You can use markdown in comments (press "m" for a cheatsheet).

Enable JavaScript to post a comment

Markdown Cheat Sheet

# This is an <h1> tag
## This an <h2> tag
###### This is an <h6> tag

Inline markup: _this text is italic_, **this is bold**, and `code()`.

[Link text](link URL "Optional title")
[Google](http://google.com/ "Google!")

![Alt text](image URL)

![This is a fish](images/fish.jpg)

1. Ordered list item 1
2. Ordered list item 2

* Unordered list item 1
* Unordered list item 2
* Item 2a
* Item 2b

And some code:

// Code is indented by one tab
echo 'Hello world!';

Horizontal rules are done using four or more hyphens:

----

> This is a blockquote

This is an <h1> tag

This an <h2> tag

This is an <h6> tag

Inline markup: this text is italic, this is bold, and code().

Link text Google

This is a fish

  1. Ordered list item 1
  2. Ordered list item 2
  • Unordered list item 1
  • Unordered list item 2
    • Item 2a
    • Item 2b

And some code:

// Code is indented by one tab
echo 'Hello world!';

Horizontal rules are done using four or more hyphens:


This is a blockquote

Toggle MarkDown / HTML (t), full reference or close this