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 9309459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:52:13+00:00 2026-06-19T00:52:13+00:00

I have a string like: $text = ‘Hello this is my string and texts’;

  • 0

I have a string like:

$text = 'Hello this is my string and texts';

I’ve got some not allowed words in array:

$filtered_words = array(
            'string',
            'text'
        );

I want to replace all the filtered words in my $text with ***, so I wrote:

$text_array = explode(' ', $text);
        foreach($text_array as $key => $value){
            if(in_array($text_array[$key], $filtered_words)){
                $text = str_replace($text_array[$key], '***', $text);
            }
        }
echo $text;

The Output:

Hello this is my *** and texts

But I need function to also replace texts with *** since it’s also contain a filtered word(text).

How I could achieve this?

Thanks

  • 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-19T00:52:14+00:00Added an answer on June 19, 2026 at 12:52 am

    You can just do it right away, str_replace supports to replace from an array into a single string:

    $text = 'Hello this is my string and texts';
    
    $filtered_words = array(
        'string',
        'texts',
        'text',
    );
    
    $zap = '***';
    
    $filtered_text = str_replace($filtered_words, $zap, $text);
    
    echo $filtered_text;
    

    Output (Demo):

    Hello this is my *** and ***
    

    Take care you have the largest words first and keep in mind when str_replace is in that mode, it will do one replacement after the other – like in your loop. So shorter words – if earlier – could be part of larger words.

    If you need something more failsafe you must consider doing a textual analysis first. That could also tell you if you didn’t know about words you might want to replace but you didn’t thought of so far.

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

Sidebar

Related Questions

I have a string like: hello #this# is #some text string# text text I
i have a string like this one: $string = some text http://dvz.local/index/index/regionId/28 http://stuff.kiev.ua/roadmap_page.php http://192.168.3.192/roadmap_page.php
I have a string like this: |T1| This is some text for the first
I have a string like this: Heading Some interesting text here HeadingSome interesting text
I have a string like this: a word {{bla|123|456}} another {{bli|789|123}} some more text
I have a string like this: SUBJECT=Some text here\r\n VALUE=19355711\r\n RCV_VALUE=2851404175\r\n RESULT=1\r\n CNCODE=0\r\n KEY1=1\r\n
So basically, I have a string like this: Some Text Here | More Text
I have string like this: Some standard text CONST_INSIDE_QUOTES blah blah CONST There might
I have string like this /c SomeText\MoreText Some Text\More Text\Lol SomeText I want to
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files

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.