Currently I have this variable in php
@if(count($label_types) > 0)
@foreach ($label_types as $label_type)
@if($label_type)
{{ $label_type->fldLabelTypeName }}
@endif
@endforeach
@endif
Which contains the following rows
Waterproof (This is waterproof)
Glossy
Normal
Now since waterproof has the (This is waterproof)
on the records
Now I want to only return the words that matches with these keywords
waterproof
, glossy
, normal
whether they are uppercases
, lowercases
For example if the case is: waterproofss
with double s
the return would be waterproof
You can solve your problem by using regex. First, you need to map your cases on string-like key waterprofs|waterproffs|waterproffss
and value Waterproof
for these cases. Your mapped key will work as a pattern in a regex. preg_match will check your pattern in your string. if the pattern matched, then it will return its value which one you defined in your map.
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
Hopefully, you will get a minimum idea of how you will show your desired value by using regex.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.