问题描述:

I have a menu that I am loading onto a template. The only issue is the the way they want it to look is to have the first number normal font size (lets say 12px) and the last numbers to be smaller (lets say 9px)

Is there a way to automatically find the last 2 characters and change the font size? Because the menu varies. It could be 7.99 or it could be 12.99. I could easily make the call saying the 3rd and fourth character will change but it's so variable. Thanks for any help in the right direction.

$spaces = str_replace(" ", "", $this->orderData->menu1);

$checkPrice =preg_replace('/^(\d{3})(\d{3})$/', '$1.$2', $spaces);

网友答案:

Yes, I'm, assuming that 12.99 is a price and you want .99 to be smaller. I will inject an inline style for this example but it could be a class name also...

// Lets assume the string is this
$string = "Hello, the price of pie is 12.99 today but now the pie is only 1.99 and not .99p";

// Let's replace the pence with any number between 1.00 and 9999.99
$string = preg_replace("/([0-9]{1,4})\.([0-9]{2})/is","\$1.<span style='font-size:9px'>\$2</span>",$string);

// If you wanted the price of ".99" too (meaning there are no pounds)
// then this would work
$string = preg_replace("/([0-9]{0,4})\.([0-9]{2})/is","\$1.<span style='font-size:9px'>\$2</span>",$string);
相关阅读:
Top