Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP如何找到以$xA3或£开始的所有价格;符号,并将其替换为引导标签_Php_Twitter Bootstrap_Replace_Preg Replace_Str Replace - Fatal编程技术网

PHP如何找到以$xA3或£开始的所有价格;符号,并将其替换为引导标签

PHP如何找到以$xA3或£开始的所有价格;符号,并将其替换为引导标签,php,twitter-bootstrap,replace,preg-replace,str-replace,Php,Twitter Bootstrap,Replace,Preg Replace,Str Replace,我有以下案文: 你只需花14.99美元就可以买到红色的雨伞,花10美元就可以买到蓝色的 £12.49. 时间有限 我想在字符串中找到$14.99的价格,并将价格包装在引导标签标签中,同样的$12.49 有什么想法吗?使用PHP,您可以获得每个$的位置(以及我键盘上没有的其他符号) 然后,一旦你有了位置,只要确保你正在看一个美元金额,在子字符串上再次执行strpos,以找到最接近的。(点)在$etc之后 <?php $string = 'some stuff $14.99'; $pos =

我有以下案文:

你只需花14.99美元就可以买到红色的雨伞,花10美元就可以买到蓝色的 £12.49. 时间有限

我想在字符串中找到$14.99的价格,并将价格包装在引导标签标签中,同样的$12.49

有什么想法吗?

使用PHP,您可以获得每个$的位置(以及我键盘上没有的其他符号)

然后,一旦你有了位置,只要确保你正在看一个美元金额,在子字符串上再次执行strpos,以找到最接近的。(点)在$etc之后

<?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
?>

我设法做到了这一点,但我必须为每种货币做到这一点。我只有两种货币,所以我可以接受

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

<?php
$string = 'You can purchase the red umbrella for just $1455.99 and the blue one for $12.49. Limited time only.';
$pattern = '#\$(\d*)\.(\d*)#';
$replacement = '<span class="label label-success">$0</span>';
$string_with_price_replaced = preg_replace($pattern, $replacement, $string);

echo $string_with_price_replaced;
?>


感谢0访问我们的网站。非常有用。

$yourfinalstring=substr($string,$pos,$dotpos+2);我怎么知道在(点)后面只有两个数字?可能是一个或四个。如果你只有14美元,结果是1美元而不是14美元?真正地我以为你在做美元交易,我有一个hudge数据库,需要在40k+记录上做这个。在这些记录中,价格可能是$14.9999。在这种情况下,您可能希望在输入后对每个字符进行限定。看看这是不是一个数字。我不是说这不好,但我想找到一个更简单的解决办法。也许预赛会更容易。我不知道这就是我问的原因。如果你正在寻找一个正则表达式解决方案,你可能可以在这里玩它,并找出它<代码>预匹配所有(“/[\$\\\\\\\\\\\\\\\\\\\\\.\d+/”,$text,$matches)-剩下的部分由您来完成…您始终可以创建一个符号数组$sym=['$'、'%'、'#'、'@']。。。然后foreach($sym as$v){$pattern='\'.$v.(\d)\.(\d*)你应该把你自己的答案标记正确,然后向我投一票
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">

<?php
$string = 'You can purchase the red umbrella for just $1455.99 and the blue one for $12.49. Limited time only.';
$pattern = '#\$(\d*)\.(\d*)#';
$replacement = '<span class="label label-success">$0</span>';
$string_with_price_replaced = preg_replace($pattern, $replacement, $string);

echo $string_with_price_replaced;
?>