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

  • Home
  • SEARCH
  • 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 7064609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:51:19+00:00 2026-05-28T04:51:19+00:00

NOTE: If you don’t want to read this, check TLDR version at bottom of

  • 0

NOTE: If you don’t want to read this, check “TLDR” version at bottom of this comment.

I currently have a basic BBCode class.

When an user makes a comment, he can use [item] [/item] tags. Anything written between [item] [/item] tags are automatically converted to their 32x32px images, and a onHover effect to load item status with ajax.

For example, you can make comments like this:

[item]Cloth Armor[/item]
[item]Wooden Sword[/item]
[item]Iron Shield[/item]

PHP will automatically show image of “Cloth Armor”. When you hover this image, you can see additional information about this item. (e.g how much defense bonus it gives)

It’s all basic, however, I don’t want to force my users to use [item] [/item] tags. I want to convert every string that is a “item name” to be converted automatically.

Let’s say, user made a comment like this, without using the BBCodes.

Hello, my characters needs some defense so I bought a Cloth Armor. Then, I will buy Wooden Sword and Iron Shield.

Okay, we can do this in many ways, but I want to get it done with LEAST steps as much as possible.

For example, something like this works but I don’t want to use such function:

$itemsArray = array('Iron Shield', 'Wooden Sword', 'Cloth Armor');
$comment = $_POST['comment'];
foreach($itemsArray as $k => $v)
{
      str_replace($v, "onHoverEffect:$v", $comment);
}
echo $comment;

This script would loop the string countless of times. (actually, there are 300+ items in the database, so it would loop 300 times, searching and replacing each item one by one, making such a huge & unnecessary load.)

I want a guidance or an example about how can this be done in a “single loop”. It should start from the first letter of string. For any correct item name he finds (item names are in the array), it should call the replace function, but generally, never loop the entire string more than once.

Too Long – Didn’t Read Version

  1. An user made a comment like “New York is more crowded than Las Vegas”.
  2. You have an array of city names in database. (New York, Las Vegas, Moscow etc.)
  3. You should be “uppercasing” city names found in the comment only with a “single loop”. (you won’t be using foreach $cities)

I want a guidance or an example about how can this be done in a “single loop”.

  • 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-28T04:51:20+00:00Added an answer on May 28, 2026 at 4:51 am

    strtr() is very fast.

    You could use it to make your replacements.

    $items = array('Iron Shield', 'Wooden Sword', 'Cloth Armor');
    $itemsReplacements = array();
    
    foreach($items as $item) {
        $itemsReplacements[$item] = 'onHoverEffect:' . $item;
    }
    
    $str = strtr($str, $itemsReplacements);
    

    CodePad.

    Note that strtr() doesn’t make any distinction between words on their own or a grouping of characters. If you wanted words on their own, you’d have to use a regular expression with word boundaries (\b) or roll something similar.

    $items = array('Iron Shield', 'Wooden Sword', 'Cloth Armor');
    
    $str = preg_replace('/\b(' . implode('|', array_map('preg_quote', $items, array_fill(0, count($items), '/')))  . ')\b/', 'onHoverEffect:$0', $str);
    

    CodePad.

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

Sidebar

Related Questions

I am reading the Java Hashmap documentation but I don't understand this sentence. Note
Note: See the bottom of this post for an explanation for why this wasn't
Note : The code in this question is part of deSleeper if you want
Note:I don't mean some theoretical question which don't have any implementation just languages that
Note: I don't want to attach the Image to the Email I want to
I want to add outline on an image, do you have any idea? Note:
I have some message elements like this... <span class=error message>Whoops! Don't forget your name.</span>
I have this json data: [ {ID:1,name:google,IP:69.5.33.22,active:true}, {ID:2,name:bing,IP:70.5.232.33,active:false} ] I want to get the
What exactly does Environment.ExternalStorageDirectory return? I read from the developer docs: Note: don't be
NOTE: I don't think this is a duplicate of the billion other questions about

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.