dongshu9458 2018-04-18 02:35
浏览 49
已采纳

在PHP中处理文本以查找匹配的字符

How can I process text with some codes.

So suppose I have text as below

Hello {::first_name::} {::last_name::},
How are you?
Your organisation is {::organisation::}

For any text between {:: and ::} should be evaluated to get its value.

I tried exploding text to array using space as delimiter and then parsing array items to look for "{::" and if found get string between "{::" and "::}" and calling database to get this field value. So basically these will be db fields.

Below is the code I have tried

$msg = "Hello {::first_name::} {::last_name::},
 How are you? 
 Your organisation is {::organisation::}";

$msg_array = explode(" ", $msg);
foreach ($msg_array as $str) {
    if (strpos($str, "{::") !== false) {
        $field_str = get_string_between($str, "{::", "::}");
        $field_value = $bean->$field_str; //Logic that gets the value of the field
        $msgStr .= $field_value . " ";
    } else {
        $msgStr .= $str . " ";
    }

}

function get_string_between($string, $start, $end)
{
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}
  • 写回答

2条回答 默认 最新

  • doumingchen3628 2018-04-18 03:38
    关注

    Your script seems fine. Your script in fiddle

    If you are looking for alternative way, you can try using preg_match_all() with str_replace(array, array, source)

    <?php
    $bean = new stdClass();
    $bean->first_name = 'John';
    $bean->last_name = 'Doe';
    $bean->organisation = 'PHP Company';
    
    $string = "Hello {::first_name::} {::last_name::}, How are you? Your organisation is {::organisation::}";
    
    // find all placeholders
    preg_match_all('/{::(.+?)::}/i', $string, $matches);
    
    $placeholders = $matches[0];
    
    //strings inside placeholders
    $parts = $matches[1];
    
    // return values from $bean by matching object property with strings inside placeholders
    $replacements = array_map(function($value) use ($bean) {
        // use trim() to remove unexpected space
        return $bean->{trim($value)};
    }, $parts);
    
    echo $newstring = str_replace($placeholders, $replacements, $string);
    

    Short format:

    $string = "Hello {::first_name::} {::last_name::}, How are you? Your organisation is {::organisation::}";
    
    preg_match_all('/{::(.+?)::}/i', $string, $matches);
    
    $replacements = array_map(function($value) use ($bean) {
        return $bean->{trim($value)};
    }, $matches[1]);
    
    echo str_replace($matches[0], $replacements, $string);
    

    And if you prefer to use a function:

    function holder_replace($string, $source = null) {
        if (is_object($source)) {
    
            preg_match_all('/{::(.+?)::}/i', $string, $matches);
    
            $replacements = array_map(function($value) use ($source) {
                return (property_exists(trim($value), 'source')) ? $source->{trim($value)} : $value;
            }, $matches[1]);
    
            return str_replace($matches[0], $replacements, $string);        
        }
        return $string;
    };
    
    echo holder_replace($string, $bean);
    

    OUTPUT:

    Hello John Doe, How are you? Your organisation is PHP Company
    

    fiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题