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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:38:41+00:00 2026-05-24T15:38:41+00:00

Using PHP, I’m looking to take a piece of text and search through it

  • 0

Using PHP, I’m looking to take a piece of text and search through it and replace certain words with another word from the list.

e.g.

Search through the text to find any word in this list:
pretty,beautiful,gorgeous,lovely,attractive,appealing

and then replace this word with another from the same list
(but not selecting the same word).

Hope this makes sense!

thanks in advance.

  • 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-05-24T15:38:42+00:00Added an answer on May 24, 2026 at 3:38 pm

    you could use preg_replace_callback:

    $random_string = '…';
    $needle = array('pretty', 'beautiful', 'gorgeous', 'lovely', 'attractive', 'appealing');
    $new_string = preg_replace_callback(
      array_map(
        function($v) { return '/'.preg_quote($v).'/'; }, // assuming $needle does not contain '/' 
        $needle),
      function($matches) use($needle) {
        do {
          $new = $needle[rand(0, count($needle)-1)];
        while($new != $matches[0]) {
        return $new;
      },
      $random_string);
    

    to make sure your $needle array does not contain characters which have a special meaning in a regular expression, we call preg_quote on each item of the array before searching.

    instead of doing a do{}while() loop you could also copy the array and remove the matched word (pretty much depends on the actual data: few items → copy&remove, many items → pick one random until it’s different from the match)

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

Sidebar

Related Questions

Using PHP I'm attempting to take an HTML string passed from a WYSIWYG editor
Using php, I'm trying to create a script which will search within a text
Using php, I am looking to find a set of unique combinations of a
Using php/html, I want to retrieve email addresses (plus other information) from MySQL and
Using PHP's proc_open() , I can start a process, read from STDOUT and STDERR
using php or javascript or ajax how to replace specific link to * http://www.google.com/
Using PHP, is there a nice way to get the (parsed) introduction only from
Using PHP Regular Expression I want to extract some value from code <?xml version=1.0
Using php I want to load XML files from several websites and want to
using PHP and MySQL i have grabbed an array of facebook user ids from

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.