0

我有一个字符串:

{ListingAgent名字}

我已附上您的房源 {Property Address} 的报价。

和数组:

$listingAgent['fname'] = 'Bob';
$property['address'] = '123 street';

不知何故,我需要用值替换 {ListingAgent First Name},用值替换 { $listingAgent['fname'] Prperty Address}$property['address']

我需要一种方法来遍历字符串并找到所有 {ListingAgent text} 并根据“文本”替换它。它几乎就像一个键值对。

我尝试了以下方法,但它只与第一个相呼应,我想我可能是在叫错树

function getbetween($content,$start,$end) {
    $r = explode($start, $content);
    foreach ($r as $key => $val){
        echo $key.'='.$val.'<br/>';
    }
}

$text = $coverletter['cover_text'];
$start = '{';
$end = '}';
echo getbetween($text,$start,$end);

这给了我:

0= 1=ListingAgent 名称}

我已附上 2=Property Address} 的报价。

可以通过 4=Agent Phone} 或电话 5=Agent Email} 联系到我的代理 3=Agent Name}。

我期待与您完成这笔交易。

6=买家名字} 7=买家姓氏}

4

2 回答 2

1

有很多方法可以做到这一点...首先,我假设您从数据库中获取信息,因此您可能会更好地考虑使用JOINs 并创建一个数组,而不是使用两个单独的数组数组。

方法

手动设置

如果您有不遵循相同模式的复杂键(由下面的自动方法假设),您可以创建头寸持有者和替换的索引数组,然后遍历批次查找并替换所有相关数据。

$listingAgent['fname'] = 'Bob';        //Example array
$property['address']   = '123 Strret'; //Example array

$replacements = array(
    'ListingAgent First Name' => $listingAgent['fname'],
    'Property Address'        => $property['address']
    );

foreach($replacements as $positionHolder=>$replacementText){
    $string = str_replace("{{$positionHolder}}", $replacementText, $string);
}
echo $string;

自动化

在不知道所有格式/值的情况下,不可能创建一个完全自动化的函数,keys但可以从显示的那些中推断:

$listingAgent['fname'] => {ListingAgent First Name}
$property['address'] => {Property Address}

例如,假设Buyer Last Name看起来像:

$buyers['blastname'] => {Buyers Buyer Last Name}

您可以使用如下函数:

$listingAgent['fname'] = 'Bob';        //Example array
$buyers['blastname']   = 'Jones';      //Example array
$property['address']   = '123 Strret'; //Example array


function replaceStrings($matches){
    global $property;
    global $listingAgent;
    global $buyers; //Add in all relevant arrays as globals

    $matches[1][0] = strtolower($matches[1][0]);
    $matches[2]    = explode(' ', trim(strtolower($matches[2])));

    if(count($matches[2]) > 1){
        $matches[2][0] = $matches[2][0][0];
    }

    $matches[2] = implode($matches[2]);

    return ${$matches[1]}[$matches[2]];
}

$string = preg_replace_callback('/\{([^ ]+)([^}]*)\}/', 'replaceStrings', $string);

echo $string;

用于JOIN创建一个数组

如果您从数据库中获取数据并决定使用JOINs,您可以将代码更新为:

$string = "
    {fname}
    I have attached an offer for your listing, {address}.
    ";

$replacementArray = array(
    'fname'    => 'Bob',
    'address'  => '123 Street',
    );

foreach($replacementArray as $positionHolder=>$replacementText){
    $string = str_replace("{{$positionHolder}}", $replacementText, $string);
}
echo $string;

显然,在这种情况下,您必须更新预定义文本中的占位符,如$string.

您也可以在不使用连接的情况下执行此操作,只需将其他数组合并在一起,这很容易做到!

输出

给定输入:

$string = "{ListingAgent First Name} I have attached an offer for your listing {Buyers Buyers Last Name}, {Property Address}.";

上述三个方法的输出(它们调用的地方echo $string)都是一样的:

Bob I have attached an offer for your listing, 123 Street. 
于 2013-09-14T18:26:57.583 回答
0

您可以使用str_replace函数将标记替换为实际值。

例如:

$text = $coverletter['cover_text'];
$text = str_replace("{ListingAgent First Name}", $listingAgent['fname'], $text);
$text = str_replace("{Prperty Address}", $property['address'], $text);
于 2013-09-14T17:20:16.617 回答