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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:28:07+00:00 2026-06-13T14:28:07+00:00

I’m trying to give my client the ability to call a function that has

  • 0

I’m trying to give my client the ability to call a function that has various code snippets by inserted a short code in their WYSIWYG editor.

For example, they will write something like…

[getSnippet(1)]

This will call my getSnippet($id) php function and output the appropriate ‘chunk’.

It works when I hard code the $id like this…

echo str_replace('[getSnippet(1)]',getSnippet(1),$rowPage['sidebar_details']);

However, I really want to make the ‘1’ dynamic. I’m sort of on the right track with something like…

function getSnippet($id) {
 if ($id == 1) {
  echo "car";
 }
}

$string = "This [getSnippet(1)] is a sentence.This is the next one.";
$regex = '#([getSnippet(\w)])#';
$string = preg_replace($regex, '. \1', $string);

//If you want to capture more than just periods, you can do:
echo preg_replace('#(\.|,|\?|!)(\w)#', '\1 \2', $string);

Not quite working 🙁

  • 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-13T14:28:09+00:00Added an answer on June 13, 2026 at 2:28 pm

    Firstly in your regex you need to add literal parentheses (the ones you have just capture \w but that will not match the parentheses themselves):

    $regex = '#(\[getSnippet\((\w)\)\])#';
    

    I also escaped the square brackets, otherwise they will open a character class. Also be aware that this captures only one character for the parameter!

    But I recommend you use preg_replace_callback, with a regex like this:

    function getSnippet($id) {
        if ($id == 1) {
            return "car";
        }
    }
    
    function replaceCallback($matches) {
        return getSnippet($matches[1]);
    }
    
    $string = preg_replace_callback(
        '#\[getSnippet\((\w+)\)\]#',
        'replaceCallback',
        $string
    );
    

    Note that I changed the echo in your getSnippet to a return.

    Within the callback $matches[1] will contain the first captured group, which in this case is your parameter (which now allows for multiple characters). Of course, you could also adjust you getSnippet function to read the id from the $matches array instead of redirecting through the replaceCallback.

    But this approach here is slightly more flexible, as it allows you to redirect to multiple functions. Just as an example, if you changed the regex to #\[(getSnippet|otherFunction)\((\w+)\)\]# then you could find two different functions, and replaceCallback could find out the name of the function in $matches[1] and call the function with the parameter $matches[2]. Like this:

    function getSnippet($id) {
       ...
    }
    
    function otherFunction($parameter) {
       ...
    }
    
    function replaceCallback($matches) {
        return $matches[1]($matches[2]);
    }
    
    $string = preg_replace_callback(
        '#\[(getSnippet|otherFunction)\((\w+)\)\]#',
        'replaceCallback',
        $string
    );
    

    It really depends on where you want to go with this. The important thing is, there is no way of processing an arbitrary parameter in a replacement without using preg_replace_callback.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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.