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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:28:48+00:00 2026-06-15T17:28:48+00:00

I have an array of sample user string inputs which may or may not

  • 0

I have an array of sample user string inputs which may or may not have smarty variables in them which id like to escape with {literal}{/literal} tags.

Array
(
    [0] => {$PLEASE}
    [1] => {PLEASE}
    [2] => {{PLEASE}}
    [3] => {{{PLEASE}}}
    [4] => {a{PLEASE}}
    [5] => {a{$PLEASE}}
    [6] => {{$PLEASE}a}
    [7] => {{PLEASE}a}
    [8] => {{{$PLEASE}}}
    [9] => {{{{PLEASE}}}}
)

Here is what I hope to achieve.

Array
(
    [0] => {$PLEASE}
    [1] => {literal}{PLEASE}{/literal}
    [2] => {literal}{{PLEASE}}{/literal}
    [3] => {literal}{{{PLEASE}}}{/literal}
    [4] => {literal}{a{PLEASE}{/literal}
    [5] => {literal}{a{/literal}{$PLEASE}{literal}}{/literal}
    [6] => {literal}{{/literal}{$PLEASE}{literal}a}{/literal}
    [7] => {literal}{PLEASE}a}{/literal}
    [8] => {literal}{{{/literal}{$PLEASE}{literal}}}{/literal}
    [9] => {literal}{{{{PLEASE}}}}{/literal}
)

Right now I have this

$data = preg_replace('/{+([^\$])([a-z0-9]*)}+/si', '{literal}{\1\2}{/literal}', $data);

Which gives me

Array
(
    [0] => {$PLEASE}
    [1] => {literal}{PLEASE}{/literal}
    [2] => {literal}{PLEASE}{/literal}
    [3] => {literal}{PLEASE}{/literal}
    [4] => {a{literal}{PLEASE}{/literal}
    [5] => {a{$PLEASE}}
    [6] => {{$PLEASE}a}
    [7] => {literal}{PLEASE}{/literal}a}
    [8] => {{{$PLEASE}}}
    [9] => {literal}{PLEASE}{/literal}
)

Been stuck for quite sometime now, was wondering if anyone could help me figure it out or if its even possible to do so.

  • 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-15T17:28:49+00:00Added an answer on June 15, 2026 at 5:28 pm

    ok, I’m sure there’s a more elegant way, perhaps one-liner, but whatever, it works with the following:

    //Step 1: Replace 'real' smarty variables with an intermediate string
    $data1 = preg_replace('/{(\$\w+)}/', "!!!$1!!!", $arr);
    //replace start and end curly braces with {literal}:
    $data2 = preg_replace('/{(.*)}/', '{literal}{$1}{/literal}', $data);
    //Replace all inner smarty variables with their original string:
    $data3 = preg_replace('/.!!!(.*)!!!/', '{/literal}$1{literal}', $data2);
    //Replace standalone variables with their original string:
    $data4 = preg_replace('/^!!!(.*)!!!$/', '{$1}', $data3);
    

    You can merge steps 3&4 in one command

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

Sidebar

Related Questions

I have an array that looks like this (sample): Array ( [1600] => Array
I have string array which is fetched from an external xml response. This string
I have a simple array with x number of items. I am displaying them
Very simple question... I have an array of pixels, how do I display them
So i have a class name repository which is just a simple array.Here is
I have a simple form with a dropdown list binded to a string array
I have an array which could be of any size coming from users. The
I have an simple array array = [apple, orange, lemon] array2 = [[apple, good
I have a simple array with bank holidays: var bank_holidays = ['06/04/2012','09/04/2012','07/05/2012','04/06/2012','05/06/2012','27/08/2012','25/12/2012','26/12/2012','01/01/2013','29/03/2013','01/04/2013','06/05/2013','27/05/2013']; I want
I have a simple array of stuff: $array = array(apples,oranges,strawberries); I am trying to

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.