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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:42:50+00:00 2026-05-22T14:42:50+00:00

I am looking to convert a string with a special HTML tag and parse

  • 0

I am looking to convert a string with a special HTML tag and parse it accordingly. Below I will show what the original string is followed by what I want the parsed string to be. If someone can direct me towards a proper coding method to make this possible that would be fantastic.

Original String:

$string = '<string 1="Jacob" 2="ice cream">{1} likes to have a lot of {2}.</string>';

Parsed String:

$parsed_string = 'Jacob likes to have a lot of ice cream.';]

EDIT:

I forgot to add that the $string variable may having multiple strings with multiple options, for example the $string variable could be the following:

$string = '<string 1="hot dog">I like to have {1}</string> on <string 1="beach" 2="sun">the {1} with the blazing hot {2} staring down at me.';

I need a solution that can parse the code example above.

EDIT 2:

Here is a sample code I developed that is incomplete and has a few bugs. If there is more than one option e.x. 1=’blah’ 2=’blahblah’ it will not parse the second option.

$string = '<phrase 1="Jacob" 2="cool">{1} is {2}</phrase> when <phrase 1="John" 2="Chris">{1} and {2} are around.</phrase>';

preg_match_all('/<phrase ([0-9])="(.*?)">(.*?)<\/phrase>/', $string, $matches);

    print $matches[1][0] . '<br />';
    print $matches[2][0] . '<br />';
    print $matches[3][0] . '<br />';

    print '<hr />';

    $string = $matches[3][0];

    print str_replace('{' . $matches[1][0] . '}', $matches[2][0], $output);

    print '<hr />';

    print '<pre>';
    print_r($matches);
    print '</pre>';
  • 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-22T14:42:51+00:00Added an answer on May 22, 2026 at 2:42 pm

    As $string is no valid XML (e.g. containing numbers as attribute names), you may try:

    $string = '<string 1="Jacob" 2="ice cream">{1} likes to have a lot of {2}.</string>';
    $parsed_string = strip_tags($string);
    for ($i = 1; $i <= 2; $i++) {
        if (preg_match('/' . $i . '="([^"]+)"/', $string, $match))
            $parsed_string = str_replace('{' . $i .'}', $match[1], $parsed_string);
    }
    echo $parsed_string;
    

    UPDATE

    Your EDIT switched from having one <string> tag to having multiple <string> tags in the variable now. This one should work for multiples:

    $string2 = '<string 1="hot dog">I like to have {1}</string> on <string 1="beach" 2="sun">the {1} with the blazing hot {2} staring down at me.</string>';
    $parsed_string2 = '';
    $a = explode('</string>', $string2);
    foreach ($a as $s) {
        $parsed_elm = strip_tags($s);
        for ($i = 1; $i <= 2; $i++) {
            if (preg_match('/' . $i . '="([^"]+)"/', $s, $match))
                $parsed_elm = str_replace('{' . $i .'}', $match[1], $parsed_elm);
        }
        $parsed_string2 .= $parsed_elm;
    }
    echo $parsed_string2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to convert a string of html entities specifying ASCII codes (ie: &#97;)
I'm looking for a way to convert a preprocessor token to a string. Specifically,
I am looking for a redistributable component to convert HTML to PDF. I would
I am looking for a library to convert HTML to PDF, including styles. I
how convert string into Unicode string in Perl. I am looking some attribute in
I'm looking for a way to convert a string that contains a character escape
I'm looking for a function to convert a string to the xml string with
I'm looking for a function to convert a string of text that is in
I am looking to do something really simple. Merely convert a string, such as
How can I convert special characters in a string to its equivalent htmlentities in

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.