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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:20:50+00:00 2026-05-24T08:20:50+00:00

I’m trying use preg_match in an IF statement and return false if a string

  • 0

I’m trying use preg_match in an IF statement and return false if a string contains some templated functions not allowed.

Here are some example templated functions allowed:

{function="nl2br($value.field_30)"}
{function="substr($value.field_30,0,250)"}
{function="addslashes($listing.photo.image_title)"}
{function="urlencode($listing.link)"}
{function="AdZone(1)"}

These are mixed in with html etc.

Now I’d like this preg_match statement to return true if regex matches the code format but didn’t contain one of the allowed function keywords:

if (preg_match('(({function=)(.+?)(nl2br|substr|addslashes|urlencode|AdZone)(.+?)\})',$string)) {
    // found a function not allowed
} else {
    // string contains only allowed functions or doesn't contain functions at all
}

Does anyone know how to do this?

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

    Not quite sure what you’re trying here, but if I were to make a regexp that matched a list of words (or function names as the case may be), I’d do somthing like

    // add/remove allowed stuff here
    $allowed = array( 'nl2br', 'substr', 'addslashes' );
    
    // make the array into a branching pattern
    $allowed_pattern = implode('|', $allowed);
    
    // the entire regexp (a little stricter than yours)    
    $pattern = "/\{function=\"($allowed_pattern)\((.*?)\)\"\}/";
    
    if( preg_match($pattern, $string, $matches) ) {
        # string DOES contain an allowed function
        # The $matches things is optional, but nice. $matches[1] will be the function name, and
        # $matches[2] will be the arguments string. Of course, you could just do a
        # preg_replace_callback() on everything instead using the same pattern...
    } else {
        # No allowed functions found
    }
    

    The $allowed array makes it easier to add/remove allowed function names, and the regexp is stricter about the curly brackets, quotes and general syntax, which is probably a good idea.

    But first of all, flip the if..else branches, or use a !. preg_match is meant for, well, matching stuff in the string, not for matching stuff that isn’t in there. So you can’t really get it to return true for something that isn’t there

    Still, as Álvaro mentioned, regexps probably aren’t the best way to go about this, and it is pretty risky to have functions exposed like that, no matter the rest of the code. If you just needed to match words it should work fine, but since it’s function calls with arbitrary arguments… well. I can’t really recommend it 🙂

    Edit: First time around, I used preg_quote on the imploded string, but that of course just escapes the pipe characters, and then the pattern won’t work. So skip preg_quote, but then just be sure that function names don’t contain anything that might mess up the final pattern (e.g. run each function name through preg_quote before imploding the array)

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

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.