1

I hava a string and its

$value ='xyz:101,xyz:102,xyz:103,xyz:104,xyz:105,ABC:201,xyz:202,xyz:203,xyz:204,xyz:205';

And I want to show this value in a table like

<table width='50%' border='1' cellpadding='10' cellspacing='10'>
  <tr>
    <th>Category</th>
    <th>Code</th>
  </tr>
  <tr>
    <td>XYZ</td>
    <td>101</td>
  </tr>
  <tr>
    <td>XYZ</td>
    <td>102</td>
  </tr>
  <tr>
    <td>XYZ</td>
    <td>103</td>
  </tr>
  <tr>
    <td>XYZ</td>
    <td>104</td>
  </tr>
  <tr>
    <td>XYZ</td>
    <td>105</td>
  </tr>
  <tr>
    <td>ABC</td>
    <td>201</td>
  </tr><tr>
    <td>ABC</td>
    <td>202</td>
  </tr>
  <tr>
    <td>ABC</td>
    <td>203</td>
  </tr>
  <tr>
    <td>ABC</td>
    <td>204</td>
  </tr>
  <tr>
    <td>ABC</td>
    <td>205</td>
  </tr>
</table>

can anybody help me for this

9
  • xyz:101 is key:value pair...? if is the value then it is array or simple string? Commented Jul 11, 2017 at 5:32
  • it is a value pair Commented Jul 11, 2017 at 5:34
  • array or string? Commented Jul 11, 2017 at 5:35
  • How far have you come? Any code to share? Commented Jul 11, 2017 at 5:35
  • So what have you tried? Please note that this is a place offering help if you have issues with your own code. We are not here to do your work for you, for that please hire a (payed) programmer. We expect you to start yourself, implement what you need and ask if you run into a problem you cannot solve yourself. Commented Jul 11, 2017 at 5:36

6 Answers 6

3

Explode the values on comma and colon and you get an array with the values. Then output them accordingly.

$value='xyz:101,xyz:102,xyz:103,xyz:104,xyz:105,ABC:201,xyz:202,xyz:203,xyz:204,xyz:205';

$arr = explode(",", $value);

Foreach($arr as $pair){
    $parts =explode(":", $pair);
    Echo "<tr>\n<td>";
    Echo $parts[0];
    Echo "</td>\n<td>";
    Echo $parts[1];
    Echo "</td>\n</tr>";
}

https://3v4l.org/TbtbX

Sign up to request clarification or add additional context in comments.

1 Comment

@simhumileco sure... Anything else I can help you with? ;-)
1

Please check below mentioned solution. This will help you.

$str = 'xyz:101,xyz:102,xyz:103,xyz:104,xyz:105,ABC:201,xyz:202,xyz:203,xyz:204,xyz:205';
$array = explode(',', $str);
$temp = array();
foreach ($array as $i => $j):
    $temp[$i] = explode(':', $j);
endforeach;
?>
<table width='50%' border='1' cellpadding='10' cellspacing='10'>
    <tr>
        <th>Category</th>
        <th>Code</th>
    </tr>
    <?php foreach ($temp as $value): ?>
        <tr>
            <td><?= $value[0] ?></td>
            <td><?= $value[1] ?></td>
        </tr>
    <?php endforeach; ?>
</table>

Let me know if it not works.

2 Comments

$val is key value pair as @Deshu Yadav mention in comment
Please check in question comments.
1

Even if I don't like spoonfeeding, there you go:

<?php  
$value='xyz:101,xyz:102,xyz:103,xyz:104,xyz:105,ABC:201,xyz:202,xyz:203,xyz:204,xyz:205';

$array = explode(",", $value);
?>

<table width='50%' border='1' cellpadding='10' cellspacing='10'>
    <tr>
        <th>Category</th>
        <th>Code</th>
    </tr>
    <?php foreach($array as $value) :
    $exploded = explode(":", $value);
    $key = $exploded[0];
    $value = $exploded[1];
    ?>
        <tr>
            <td><?php echo $key; ?></td>
            <td><?php echo $value; ?></td>
        </tr>
    <?php endforeach; ?>
</table>

1 Comment

$val is key value pair as @Deshu Yadav mention in comment
0

IF it is key value pair the use foreach :

foreach ($value as $key => $v) {
  echo "<tr>";      
  echo "<td>".$key."</td>";
  echo "<td>".$v."</td>"; 
  echo "</tr>";
}

Comments

0

If value='' is a part of your string; get quotations content by this code

$string = "value='xyz:101,xyz:102,xyz:103,xyz:104,xyz:105,ABC:201,xyz:202,xyz:203,xyz:204,xyz:205'";
$matches = array();
preg_match( '/value=\'([^\']*)\'/i', $string, $matches ) ;
$content = $matches[0];

and use this code to generate table :

echo "<table width='50%' border='1' cellpadding='10' cellspacing='10'>";
$array = explode(',', $content);
foreach ($array as $item) {
    $data = explode(':', $item);
    echo "<tr>";
    echo "  <th>$data[0]</th>";
    echo "  <th>$data[1]</th>";
    echo "</tr>";
}
echo "</table>";

4 Comments

Don't use regex to split a simple string.
@Andreas i'm explained 'If value='' is a part of string' use regex , it's not for split
Yes it is! Use substr() if you know the first seven chars are not supposed to be there and the last one use substr. That is what the function is there for. Regex however is not meant to split simple strings. You are using far more time and memory on a simple task that substr and explode can do. Very poor answer in my opinion.
Also have you tried the code? Your content is the same as the string.
0

Use explode() and foreach:

<?php

$value = 'xyz:101,xyz:102,xyz:103,xyz:104,xyz:105,ABC:201,xyz:202,xyz:203,xyz:204,xyz:205';

$rows = explode(',', $value);

?>
<table width='50%' border='1' cellpadding='10' cellspacing='10'>
    <tr>
        <th>Category</th>
        <th>Code</th>
    </tr>
<?php

foreach ($rows as $row) {
    $values = explode(':', $row);

?>
    <tr>
        <td><?php echo $values[0]; ?></td>
        <td><?php echo $values[1]; ?></td>
    </tr>
<?php

}

?>
</table>

For reference, see:

For an example, see:

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.