PHP中的部分字符串匹配

时间:2014-11-25 11:45:33

标签: php match

$var1 = 'accommodation_rate_fri_to_sun_garden_view';
$var2 = 'Garden View';

if (  stripos($var1, $var2) !== false ) {
    echo 'found';
}
else {
    echo 'not found';
}

//结果为not found。是否还有其他部分匹配功能?

2 个答案:

答案 0 :(得分:1)

<?php

$haystack = 'accommodation_rate_fri_to_sun_garden_view';
$needle   = 'Garden View';
$needles  = explode(' ', $needle);
$partial  = false;

foreach($needles as $needle)
{
    if(stripos($haystack, $needle) !== false)
    {
        $partial = true;
        break;
    }
}

if($partial)
{
    echo 'found';
}
else
{
    echo 'not found';
}

答案 1 :(得分:1)

试试这个 -

$var1 = 'accommodation_rate_fri_to_sun_garden_view';
$var2 = 'Garden View';
$var2 = str_replace(" ", "_", strtolower($var2));
if (  stripos($var1, $var2) !== false ) {
    echo 'found';
}
else {
    echo 'not found';
}