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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:11:18+00:00 2026-05-27T09:11:18+00:00

I am trying to get a variable value and add it in a string

  • 0

I am trying to get a variable value and add it in a string to output html elements. Here is my code:

// String
$str = 'Some validations [IF TEST]<a href="text.html">firstValue</a>[ELSEIF TEST1]secondValue[/IF] in [IF OK]thirdValue[/IF] end of string.';

// Regex
$do = preg_match_all("/\[IF (.*?)\](.*?)(\[\/IF\]|\[ELSEIF(.*?)\](.*?)\[\/IF\])/i", $str, $matches, PREG_SET_ORDER);
$str = str_replace("[IF ". $matches[0][1] ."]", "<?php if($". $matches[0][1] ."){ echo('".$matches[0][2]."');} ?>", $str);
echo $str;

However, for some reasons, when I use the $matches[0][2], it breaks my string giving this result in the browser:

Some validations firstValue');} ?>firstValue[ELSEIF TEST1]secondValue[/IF] in [IF OK]thirdValue[/IF] end of string.

and this in the source:

Some validations <?php if($TEST){ echo('<a href="text.html">firstValue</a>');} ?><a href="text.html">firstValue</a>[ELSEIF TEST1]secondValue[/IF] in [IF OK]thirdValue[/IF] end of string.

Thanks all !

  • 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-27T09:11:18+00:00Added an answer on May 27, 2026 at 9:11 am

    PHP is not going to execute the PHP that is inside your string. All you’re doing is replacing [IF TEST]... with a block of PHP code, which will not get processed. You need to process the PHP code first and then replace the [IF TEST]... section with the result of that code.

    Try using variable variables to check if the variable $TEST is set and then insert the appropriate text corresponding to that variable.

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

Sidebar

Related Questions

I am trying to get some xpath from xsl variable using xsl ver 1.0
I am trying to get strpos to search string variable $string for the phrase
I'm trying to get some info out of a decoded JSon string into an
I am trying to add anchors to my html output. The html is created
I'm trying to get a class memeber variable list at run time. I know
I am trying to get jquery to pick up variable that I am defining
I am trying to use a variable to get a function in a extended
I am trying to created nested dictionary variable like the below, But I get
I am trying to add a few different GET variables to the url. I
I'm trying to generate some code in a web service. But it's returning 2

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.