Php 在文本中查找和替换价格的算法

Php 在文本中查找和替换价格的算法,php,algorithm,magento,replace,Php,Algorithm,Magento,Replace,我有一段文字,是通过CMS中的magento提供的 检索到的文本可能包含价格。比如说 交付文本 超过200欧元的订单将免费交付。订单费10欧元 200欧元以下。重型货物可能要额外收费 我将如何替换价格的每一次发生,因此在上述情况下,我将进行更改 €200 €10 €200 我想根据当前使用的货币替换这些价格 $fromCur = 'EUR'; // currency code to convert from $toCur = 'USD'; // currency code to convert

我有一段文字,是通过CMS中的magento提供的

检索到的文本可能包含价格。比如说

交付文本

超过200欧元的订单将免费交付。订单费10欧元 200欧元以下。重型货物可能要额外收费

我将如何替换价格的每一次发生,因此在上述情况下,我将进行更改

€200
€10
€200
我想根据当前使用的货币替换这些价格

$fromCur = 'EUR'; // currency code to convert from
$toCur = 'USD'; // currency code to convert to
$toCurrencyPrice = Mage::helper('directory')->currencyConvert($fromCurrencyPrice, $fromCur, $toCur);
这是我将如何转换价格,唯一的问题是,我不知道我将如何在文本中找到价格

这是我到目前为止所做的尝试

//the text to search
$text = 'Orders over €200 are delivered Free Of Charge. €10 charge for orders under €200. There may be additional charges for heavy goods.';

$matches = array();
//find a price with a euro symbol
preg_match_all("/€[0-9]+/", $text, $matches);


$ints = array();
$counter = 0;
//remove the euro symbol
foreach ($matches[0] as $match) {
   //echo  substr( $match,6) . '<br/>';
   $ints[$counter] = substr( $match,6);
   $counter++;

}
//now i know i have to convert it to my price, but my issue is, how do i now replace new values, with the old values inside the $ text varaible
//要搜索的文本
$text='欧元以上订单;200件免费送货&欧元10欧元以下订单的费用;200重型货物可能要额外收费。”;
$matches=array();
//查找带有欧元符号的价格
preg_match_all(“/&euro;[0-9]+/”,$text,$matches);
$ints=array();
$counter=0;
//删除欧元符号
foreach($matches[0]为$match){
//回显substr($match,6)。“
”; $ints[$counter]=substr($match,6); $counter++; } //现在我知道我必须将其转换为我的价格,但我的问题是,现在如何用$text变量中的旧值替换新值

假设我想更改找到的匹配项,元素以$int(不带欧元符号的价格)为单位。我该怎么做?

不是整个解决方案,但首先,您可以使用以下方法提取价格和单位:

$text = "I bought a shoes 33 $, a tee shirt 44 $ and a short 55 €.";

$match = array();
preg_match_all("/([0-9]+)[ ]*(€)|([0-9]+)[ ]*([$])/", $text, $match);
print_r($match);
它将为您提供:

Array
(
    [0] => Array
        (
            [0] => 33 $
            [1] => 44 $
            [2] => 55 €
        )

    [1] => Array
        (
            [0] => 
            [1] => 
            [2] => 55
        )

    [2] => Array
        (
            [0] => 
            [1] => 
            [2] => €
        )

    [3] => Array
        (
            [0] => 33
            [1] => 44
            [2] => 
        )

    [4] => Array
        (
            [0] => $
            [1] => $
            [2] => 
        )
)

您将能够决定要应用什么逻辑(知道值和货币符号)

PHP文档是一个很好的起点。您必须使用字符串搜索函数。试着自己弄清楚,你有完整的PHP文档要查看。只要在谷歌上搜索字符串搜索php,你就会找到大量的信息

以下是一些可能对您有所帮助的功能:

我想pregmatch会满足你的需要。理解该功能并在您的范围内使用它


祝你好运

假设您始终使用欧元作为输入货币,请尝试下一个代码:

$string = 'Orders over €200 are delivered Free Of Charge. €10 charge for orders under €200. There may be additional charges for heavy goods.';
$pattern = "#€[0-9]{1,}#";
$newStr = preg_replace_callback($pattern, create_function(
        '$matches',
        'return Mage::helper(\'directory\')->currencyConvert($matches[0], \'EUR\', \'USD\');'
    ), $string);
echo $newStr;
我还没有测试过它,但它应该可以工作

更新:


我只是想转换你有,你可能需要删除,然后添加货币符号第一;但是你应该有一个起点-只需使用返回函数

开始,字符串搜索怎么样?使用正则表达式查找价格很简单。但是你怎么知道fromCUR呢?如果你看到20美元这样的价格,你怎么知道它们是美元、加拿大元还是澳大利亚元?你说的“转换算法”是什么意思?这在英语中是没有意义的?这将永远是对帮助的欢呼,我添加了我自己的示例,但我不知道如何用旧值替换新值(转换值)