Php 从与数组匹配的句子中获取单词

Php 从与数组匹配的句子中获取单词,php,laravel-5,Php,Laravel 5,目前我在php @if(count($label_types) > 0) @foreach ($label_types as $label_type) @if($label_type) {{ $label_type->fldLabelTypeName }} @endif @endforeach @endif 其中包含以下行 防水(这是防水的) 光泽 Normal 既然防水记录上有(这是防水的) 现在我只想返回与这些关键

目前我在
php

   @if(count($label_types) > 0)
   @foreach ($label_types as $label_type)
      @if($label_type)
        {{ $label_type->fldLabelTypeName }}
      @endif
   @endforeach
   @endif
其中包含以下行

防水(这是防水的)

光泽

Normal

既然
防水记录上有(这是防水的)

现在我只想返回与这些关键字匹配的单词

防水
光泽
普通

它们是否为
大写
小写

例如,如果情况是:
waterproof ss
和double
s


返回将是防水的

您可以使用regex解决您的问题。首先,对于这些情况,您需要将案例映射到字符串上,如key
waterprofs | waterproffs | waterproffs
和value
防水
。映射的键将作为正则表达式中的模式工作。将检查字符串中的模式。如果模式匹配,那么它将返回您在地图中定义的值

function getLabel(string $string)
{
    // You own custom map using regex, key-value pair,
    $matchersMap = [
        'waterproofs|waterproof' => 'Waterproof',
        'glossies|glossy' => 'Glossy',
        'normal' => 'Normal'
    ];

    $result = -1;

    foreach($matchersMap as $matchesKey => $replaceValue) {
        if (preg_match("/$matchesKey/", strtolower($string))) {
            $result = $replaceValue;
            break;
        }
    }

    return $result;
}

var_dump(getLabel("waterproof has the (This is waterproof)")); //Waterproof 

希望您能对如何使用regex显示所需值有一个最低限度的了解。

您可以使用regex解决问题。首先,对于这些情况,您需要将案例映射到字符串上,如key
waterprofs | waterproffs | waterproffs
和value
防水
。映射的键将作为正则表达式中的模式工作。将检查字符串中的模式。如果模式匹配,那么它将返回您在地图中定义的值

function getLabel(string $string)
{
    // You own custom map using regex, key-value pair,
    $matchersMap = [
        'waterproofs|waterproof' => 'Waterproof',
        'glossies|glossy' => 'Glossy',
        'normal' => 'Normal'
    ];

    $result = -1;

    foreach($matchersMap as $matchesKey => $replaceValue) {
        if (preg_match("/$matchesKey/", strtolower($string))) {
            $result = $replaceValue;
            break;
        }
    }

    return $result;
}

var_dump(getLabel("waterproof has the (This is waterproof)")); //Waterproof 
希望您能对如何使用regex显示所需的值有一个基本的了解