1

I want to add differents values into a string in after a certain value.

For example :

$values = [ "<first><first>","<second><second>","<third><third>"];

$string = "<A1>
               <B1>...<B1>
                <C1>...<C1>
                 <D1>
                  <example><example>
                 <D1>
               <B2>...<B2>
                <C2>...<C2>
                 <D2>
                  <example><example>
                 <D2>
            </A1>
            <A2>
               <B1>...<B1>
                <C1>...<C1>
                 <D1>
                  <example><example>
                 <D1>
            </A2>"

And i want to add the values after the <example><example> . Final result will be like this :

$string = "<A1>
           <B1>...<B1>
            <C1>...<C1>
             <D1>
              <example><example>
              <first><first>
             <D1>
           <B2>...<B2>
            <C2>...<C2>
             <D2>
              <example><example>
              <second><second>
             <D2>
        </A1>
        <A2>
           <B1>...<B1>
            <C1>...<C1>
             <D1>
              <example><example>
              <third><third>
             <D1>
        </A2>"
3
  • loop through your values, find first occurance of <example><example>, append your value / replace with <example><example> + value, repeat Commented Nov 14, 2019 at 12:15
  • how to find the first occurance of <example><example> with php? Commented Nov 14, 2019 at 12:16
  • you can use strpos for that Commented Nov 14, 2019 at 12:17

2 Answers 2

1

Static solution:

$values = ["one","two","three"];
$index = 0;
$string = "<A1>
                   <B1><B1>
                    <C1><C1>
                     <D1>
                      <example></example>
                      {$values[0]}
                     <D1>
                   <B2><B2>
                    <C2><C2>
                     <D2>
                      <example></example>
                     {$values[1]}
                     <D2>
                </A1>
                <A2>
                   <B1><B1>
                    <C1><C1>
                     <D1>
                      <example></example>
                     {$values[2]}
                     <D1>
                </A2>";
echo $string;

Dynamic solution:

$values = ["one","two","three"];
$index = 0;
$string = "<A1>
                   <B1><B1>
                    <C1><C1>
                     <D1>
                      <example></example>
                     <D1>
                   <B2><B2>
                    <C2><C2>
                     <D2>
                      <example></example>
                     <D2>
                </A1>
                <A2>
                   <B1><B1>
                    <C1><C1>
                     <D1>
                      <example></example>
                     <D1>
                </A2>";
$result = preg_replace_callback(
    "/(?<=<\\/example>)/",
    function($match) use (&$index, $values) { return $values[$index++];},
    $string
);
echo $result;
Sign up to request clarification or add additional context in comments.

1 Comment

not a good way, $values are most likely dynamic values (count might change), which requires a dynamic solution
0
enter code her

<?php


$find_placeholder_string = "<example><example>";
$values = array("<first><first>","<second><second>","<third><third>");

$str = "<A1>
           <B1>...<B1>
            <C1>...<C1>
             <D1>
              <example><example>
             <D1>
           <B2>...<B2>
            <C2>...<C2>
             <D2>
              <example><example>
             <D2>
        </A1>
        <A2>
           <B1>...<B1>
            <C1>...<C1>
             <D1>
              <example><example>
             <D1>
        </A2>";


		
$stripped = trim(preg_replace('/\s+/', '', $str));

$offset = 0;

for($i=0; $i<sizeof($values); $i++)
{
	$value_string_count = strlen($values[$i]);
	$find_placeholder_count = strlen($find_placeholder_string);
	
	$position = strpos($stripped, $find_placeholder_string, $offset);
	$stripped = substr_replace($stripped, $values[$i], $position+$find_placeholder_count, 0);
	$offset = $position + $find_placeholder_count + $value_string_count;
}
echo $stripped;


?>

e

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.