我有以下文字:
You can purchase the red umbrella for just $14.99 and the blue one for £12.49. Limited time only
我想在字符串中查找 $14.99 价格,并将价格包装在 Bootstrap 标签中,对于 £12.49 也是如此。
有什么想法吗?
最佳答案
使用 PHP,您可以使用 strpos()获取每个 $ 的位置(以及键盘上没有的其他符号)...
然后,一旦你有了位置,只要确保你正在查看的是美元金额,方法是在子字符串上再次执行 strpos 以找到 $ 等之后最近的 .(点)...
<?php
$string = 'some stuff $14.99';
$pos = strpos($string, '$');
//pos = 11
$newstring = substr($string, $pos);
$dotpos = strpos($string, '.');
$yourfinalstring = substr($string, $pos, $dotpos + 2);
//your final string = $14.99
?>
在 javascript 中只需使用 indexOf()
var string = 'some stuff $14.99';
var pos = string.indexOf('$');
// pos = 11
关于PHP 如何查找字符串中以 $ 或 £ 符号开头的所有价格并将其替换为 Bootstrap 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30076091/