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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:29:12+00:00 2026-05-13T08:29:12+00:00

I have this web page where users can add smilies to their comments. And

  • 0

I have this web page where users can add smilies to their comments. And I want to limit the number of smilies per comment. The “system” works but I have some problems with the regex part.
I have my smilies defined in a config file like so:

$config['Smilies'] = Array (
    // irrelevant stuff
    'smilies' => Array (
        ':)' => 'smile.gif',
        ':(' => 'sad.gif',
        // some more smilies
        's:10' => 'worship.gif',
        's:11' => 'zip.gif',
        's:12' => 'heart.gif',
        // some more smilies
        's:1' => 'dry.gif',
        's:2' => 'lol.gif',
        's:3' => 'lollol.gif',
        // some more smilies
    )
);

And then when I validate the comment (to see how many smilies are there), I loop trough this array and match the smile to the content of the comment. The regex is used like this:

foreach ( $this->config['smilies'] as $smilie => $smilieImage )
{
    $matches = Array ();
    Preg_Match_All ( '/' . Preg_Quote ( $smilie ) . '/i', $Content, $matches );

    $numOfFoundSmilies += Count ( $matches[0] );
}

The problem is that the if I enter “s:10” into the comment, the above code will find two matches: “s:10” and “s:1”. My knowledge of regular expressions is very poor, and I can’t figure this one out.

  • 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-13T08:29:13+00:00Added an answer on May 13, 2026 at 8:29 am

    Regular expressions are greedy by default (at least PCREs). Usually you could circumvent this:

    /a+/ # selects the whiole string from "aaaaaaa"
    
    /a+?/ # selects only "a"
    

    In your case, this doesn’t help much, since you can’t just throw in a question mark somewhere. The only possibility is to re-order your search array and instantly replace the found places. Search first for s:10 and second for s:1, and use preg_replace() instead of the matching. This way, the second doesn’t find the first anymore.

    Another possibility: Split your search array in two. If you know, that the one always has the structure ‘s:’ plus digits, you could have your regexp in this second loop like

    Preg_Match_All ( '/' . Preg_Quote ( $smilie ) . '(?![0-9])/i', $Content, $matches );
    

    with (?![0-9]) a look ahead expression looking for any non-digit.

    And a third one: If you allow (== convert) smileys only at certain places, you could use this:

    Preg_Match_All ( '/\b' . Preg_Quote ( $smilie ) . '\b/i', $Content, $matches );
    

    \b is a “word boundary”, usually any not-(letter, digit, underscore). Drawback is obviously, that not all smileys (like “abc;-)xyz”) will be found.

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

Sidebar

Related Questions

I have a web page where you can add new users onto the system.
I have a web page. When this web page is loaded, I want to
Here is my situation. I have a web page for users to create their
I have a HTML file, which generates a web page. Users can enter some
my structure is like this: master page aspx(web form) ascx(user control) I have a
I have this web application using Spring Web Flow framework. In my main page
I have a very general web page where I display information. I have this
I have used this code for extracting urls from web page.But in the line
I have a web page with swf file defined this way <object width=600 height=400
I have a web page coded in PHP. This page is for posting a

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.