Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7954907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:34:30+00:00 2026-06-04T03:34:30+00:00

Hi all im trying to build a regular expression which will sustain the following

  • 0

Hi all im trying to build a regular expression which will sustain the following criteria.

Word to be censored in this example “view”.
Character to be used after censor: “%”, since “*” mess up my post formatting.

Examples of word use:
view

views
preview
I went to see the great view
The view was great wasn’t it.

Example after word censor:
%%%%
views
preview
I went to see the great %%%%
The %%%% was great wasn’t it.

Here is some code I have:

$string = preg_replace_callback('/\s*'. preg_quote($word, '\\') .'\s*/is', 'bbcode_callback_censored', $string);

Trouble is this matches everything right now since i use “*” in the regex ater “\s”. Any ideas what I could do to fulfill my criteria?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:34:32+00:00Added an answer on June 4, 2026 at 3:34 am

    Don’t match for whitespace, use a word boundary

    Try

    $string = preg_replace_callback('/\b'. preg_quote($word, '\\') .'\b/is', 'bbcode_callback_censored', $string);
    

    See it here on Regexr

    You just need to make sure that the content of $word does not start or end with a non word character, then the word boundary will not work.

    \b is a word boundary. It matches on a change from a word character (as defined in \w) to a non word character as defined in \W, or the other way round.

    Alternative: whitespace boundary

    If you don’t like the word boundary because it is possible that your word to replace starts or end with non word characters like “#view”, define your own “whitespace boundary”, e.g. like this:

    (?<=^|\s)#view(?=$|\s)
    

    See it here on Regexr

    Would look in your code like this

    $string = preg_replace_callback('/(?<=^|\s)'. preg_quote($word, '\\') .'(?=$|\s)/is', 'bbcode_callback_censored', $string);
    

    (?<=^|\s) will match if there is the Start of the String or whitespace before

    (?=$|\s) will match if there is the end of the String or whitespace ahead

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a regular expression that will catch one of three patterns
I'm trying to build a regular expression that will detect any character that Windows
Im trying to build a query that will fetch all changed rows from a
I am trying to build a program that will get all the system keyEvents
I'm trying to build a regular expression for validating a SWTOR character name. It
I have been trying to build a regular expression but haven't been able to
I need to build a regular expression for all nonempty sequences of letters other
I am trying to build a regular expression to match any git read+write URL
I'm trying to build a query where it will return all rows from today's
I'm trying to build a script to retrieve all feed entries from a given

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.