dongtao4319 2017-08-17 17:43
浏览 89
已采纳

Smarty / PHP Regex取代

I have the next string:

countries_united_states_ohio

sometimes it can be countries_spain_madrid

How can I remove the word Countries and have the next output:
United States, Ohio
or
Spain, Madrid

I'm using Smarty and I tried something like this:

{$string|replace:"_":", "|capitalize}

The problem is that it returns United,States(as expected) and I guess I could remove the word 'countries' using substr.

Anyone has any idea?

Thank you!

  • 写回答

3条回答 默认 最新

  • dpjo15650 2017-08-17 19:32
    关注

    I think I found a way.
    You will need a list of all countries in the world. Example: https://www.countries-ofthe-world.com/all-countries.html

    The code will look at each part of the string and see if it's a "country word" if true:
    It will make sure it has not already grabbed that word (see all "uinited" countries).
    If it is not already in the country array then add it there.

    Now I can just implode the country.

    State/city is what is left of I remove country and _.

    $Ucountries = ["Uganda",
       "Ukraine",
       "United Arab Emirates (UAE)",
       "United Kingdom (UK)",
       "United States of America (USA)",
       "Uruguay",
       "Uzbekistan"];
    
    $Mcountries = ["Macedonia (FYROM)",
       "Madagascar",
       "Malawi",
       "Malaysia",
       "Maldives",
       "Mali",
       "Malta",
       "Marshall Islands",
       "Mauritania",
       "Mauritius",
       "Mexico",
       "Micronesia",
       "Moldova",
       "Monaco",
       "Mongolia",
       "Montenegro",
       "Morocco",
       "Mozambique",
       "Myanmar (Burma)"];
    
    $str = "countries_mexico_mexico_city";
    //$str = "countries_united_states_ohio";
    $str = str_replace("countries_", "", $str);
    
    // Copy array needed to $CountryList
    $CountryList = ${strtoupper(substr($str,0,1)). "countries"}; 
    
    $countryarr = array();
    $arr = explode("_", $str);
    
    Foreach($arr as $val){
        Foreach($CountryList as $item){
            If(stripos($item, $val) !== false){
                If(!in_array($val, $countryarr)){
                    $countryarr[] = $val;
                }
            }
        }
    }
    
    $country = implode(" ", $countryarr);
    $state_city = preg_replace("/" . implode("_",$countryarr) . "/", "", $str,1);
    $state_city = trim(str_replace("_", " ", $state_city));
    
    Echo $country . "
    " . $state_city;
    

    https://3v4l.org/POMPT

    Edit; thought of a way when I was brushing my teeth.
    Since the countryarr will only have Mexico I can with preg_replace replace only once.
    Also I had to change the replace pattern to a implode of countryarr.
    Now what is left is a state/city with a underscore first and one or more words separated with underscore.
    So replace underscore with space and trim the string.

    EDIT2; Now that I have had some sleep I thought that the array to match against countries will be very large.
    So I made a change.
    Each first letter of country is a new array, and the code will only loop in the countries with the same first letter.

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测