开发者

Php - Only show last part of value

I have a value from my database: 250.00$ 100x100cm or 1960.00$ 500x500cm

How do I make it so that it cuts away the price and only shows the measurements?

So it should only pr开发者_StackOverflowint out: 100x100cm or 500x500cm

I hope that's easy enough to understand.


Use explode( ' ', $value); and pull the value from [1] in the resulting array.

echo array_pop( explode( ' ', '250.00$ 100x100cm' ) ); // 100x100cm

My example is doing something similar - using array_pop() I'm retrieving the value of the last index in the resulting array from explode() and returning it to be echo'd.


Assuming that's a single space separating the pieces and there are no other spaces:

list($price, $measurement) = explode(" ", $value, 2);

Note that the price is optional if you don't want it:

list(, $measurement) = explode(" ", $value, 2);

And the "2" is just for a bit of safety in case the measurement has a space.


if thats always the format I would try the explode php function. Something like;

$array = explode("$ ", "250.00$ 100x100cm");

You can then just echo out which part of the array you need...


Try this:

$value = "250.00$ 100x100cm";
$value_parts = explode(" ", $value);
echo $value_parts[1];


Is it always going to be a three digit number? echo substr($string, -7); Otherwise you might want some regex preg_match("!\d+x\d+cm!", $value);


If you always have a space between the price and dimensions, you can use

$newVar = explode(" ", $yourVar); to split the value by a space and then just use echo $newVar[1] to show your dimensons.


foreach ($values as $value) {
  list($price, $size) = explode(' ', $value);
  echo $size;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜