0

I have following mysql php array result.

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo '<tr>';
echo "<td> {$row['cust']} </td>".
     "<td> {$row['manu']} </td>".
     "<td> {$row['model']} </td>".
     "<td> {$row['serial']} </td>".
     "<td> {$row['capacity']} </td>".
     "<td> {$row['firmware']} </td>".
     "<td> {$row['deviceid']} </td>".
     "<td> {$row['ataver']} </td>".
     "<td> {$row['ltime']} </td>".
     "<td> {$row['date']} </td>".
     "<td> {$row['ourref']} </td>".
     "<td> {$row['result']} </td>";


/**                     foreach($row as $key=>$value) {
                                echo '<td>',$value,'</td>';
                        }*/
                        echo '</tr>';

"<td> {$row['capacity']} </td>". array holds information like

250000000000 bytes [250 GB]
400000000000 bytes [400 GB]
500000000000 bytes [500 GB]

I want to delete everything including bytes.

So the desired output would look like

250 GB
400 GB
500 GB

How can i achieve this using above code?

Thanks well in advance.

chris85
  • 23,846
  • 7
  • 34
  • 51
user2107349
  • 191
  • 2
  • 3
  • 16
  • Start using these: http://php.net/manual/en/ref.strings.php – Marc B Aug 31 '15 at 20:02
  • You simply wanna remove 000000000 and add GB... Use str_replace – Hearner Aug 31 '15 at 20:04
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Aug 31 '15 at 20:04
  • can you give working example please? – user2107349 Aug 31 '15 at 20:05

4 Answers4

1

It's quite simple, look here :

$String = "250000000000 bytes [250 GB]";

$String2 = substr($String, 0, 3); // take the first 3 caracteres

$result = $String2." GB"; // Just add GB after
echo $result;

Just replace "<td> {$row['capacity']} </td>". with the fallowing

   "<td>".substr($String, 0, 3)." GB</td>".
Hearner
  • 2,711
  • 3
  • 17
  • 34
0

You can use regular expressions to achieve this:

preg_match('/\[(.*?)\]/', $row['capacity'], $matches);
if (isset($matches[1])) {
    $row['capacity'] = $matches[1];
}
echo "<td> {$row['capacity']} </td>";
Elias
  • 95
  • 4
0

There are many ways to do this, I'd use a regex to capture everything inbetween the brackets.

$string = '250000000000 bytes [250 GB]
400000000000 bytes [400 GB]
500000000000 bytes [500 GB]';
preg_match_all('~\[(.*?)\]~', $string, $sizes);
print_r($sizes[1]);

Output:

Array
(
    [0] => 250 GB
    [1] => 400 GB
    [2] => 500 GB
)

Regex101 Demo: https://regex101.com/r/zR6pU1/1

The . is any character, the * is zero or more occurrences of any character and the ? stops at the first occurrence of the next character. The + might be a better quantifier so that you are sure that there is something in there. The + is one or more occurrences.

With the + quantifier,

$string = '250000000000 bytes [250 GB]
400000000000 bytes [400 GB]
500000000000 bytes [500 GB]';
preg_match_all('~\[(.+?)\]~', $string, $sizes);
print_r($sizes[1]);

If you wanted to be stricter you could even do:

$string = '250000000000 bytes [250 GB]
400000000000 bytes [400 GB]
500000000000 bytes [500 GB]';
preg_match_all('~\[(\d+ [MKG]B)\]~', $string, $sizes);
print_r($sizes[1]);

Which would match any numbers a space and then KB, MB, or GB. The [] is a character class allowing any of the literal characters inside to be present.

chris85
  • 23,846
  • 7
  • 34
  • 51
  • 1
    @MozzieMD it doesn't have to be a simple thing. It can be used to be more accurate about the data you are taking in. – chris85 Aug 31 '15 at 20:17
  • 1
    In this case you can just explode `[` and replace `]` regex will use much more resources for such a tiny task! – MozzieMD Aug 31 '15 at 20:21
  • @MozzieMD You did see all three possible answers I gave, correct? How efficient do you expect exploding and then replacing to be? – chris85 Aug 31 '15 at 20:36
  • we will see when you will have a lot of regex on your website and full memory usage... – MozzieMD Aug 31 '15 at 21:37
  • @MozzieMD Haha well phrased. Care to post your answer and we can benchmark and actually compare the values? Show me something more efficient and I will delete/update this answer. – chris85 Sep 01 '15 at 03:47
  • Do it yourself, explode by `[` and replace `]` and benchmark regex vs it.. when you have 1000 requests in the same moment! – MozzieMD Sep 01 '15 at 05:08
  • @MozzieMD did it, regex was faster, minimally, less than .2 second difference. I did it on a 10k interval. – chris85 Sep 01 '15 at 05:36
  • @MozzieMD do you want the results of an actual test on your "answer" and mine? Let me know. When you see you results maybe retract downvote? – chris85 Sep 03 '15 at 20:31
0

You can use:

$str = preg_replace('[[:digit:]]+ Bytes ', "", $str);

https://regex101.com/r/fE7nO5/1

Then str_replace to replace the [] characters.

$str = str_replace('[', ' ', $str);
$str = str_replace(']', ' ', $str);
$str = trim($str);

Edit: You can also do it with one regex:

$str = preg_replace('[[:digit:]]+ Bytes |\[|\]', "", $str);

https://regex101.com/r/fE7nO5/2

gmponos
  • 2,157
  • 4
  • 22
  • 33