简体   繁体   English

PHP删除字符前的一切

[英]php delete everything before the character

I have following mysql php array result. 我有以下mysql php数组结果。

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. 提前谢谢。

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>". 只需替换"<td> {$row['capacity']} </td>". with the fallowing 与休闲

   "<td>".substr($String, 0, 3)." GB</td>".

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>";

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 Regex101演示: https ://regex101.com/r/zR6pU1/1

The . . is any character, the * is zero or more occurrences of any character and the ? 是任何字符, *是零个或多个出现的any character ,而?? 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. 它将匹配任何数字,然后是空格,KB,MB或GB。 The [] is a character class allowing any of the literal characters inside to be present. []是一个字符类,允许显示其中的任何文字字符。

You can use: 您可以使用:

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

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

Then str_replace to replace the [] characters. 然后str_replace替换[]字符。

$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 https://regex101.com/r/fE7nO5/2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM