PHP - 如果字符串部分匹配字符串

时间:2017-04-21 09:35:32

标签: php

出于某种原因,<?php $street = 'Lightbowne Road'; $addresline1 = '284 Lightbowne Road '; $addresline2 = 'Lightbowne Road '; $addresline3 = '284 Lightbowne Road'; $addresline4 = 'jim '; $addresline5 = 'Lightbowne Road'; if ( stripos($street, $addresline1) !== false ) { $addresline1 = 'addresline1 '; } if ( stripos($street, $addresline2) !== false ) { $addresline2 = 'addresline2 '; } if ( stripos($street, $addresline3) !== false ) { $addresline3 = 'addresline3 '; } if ( stripos($street, $addresline4) !== false ) { $addresline4 = 'addresline4 '; } if ( stripos($street, $addresline5) !== false ) { $addresline5 = 'addresline5 '; } $addresslines = $addresline1.$addresline2.$addresline3.$addresline4.$addresline5; $streetcomp = trim($street.' '.$addresslines); echo '<p><strong>street:</strong> '.$street.'</p>'; echo '<p><strong>addresline1:</strong> '.$addresline1.'</p>'; echo '<p><strong>addresline2:</strong> '.$addresline2.'</p>'; echo '<p><strong>addresline3:</strong> '.$addresline3.'</p>'; echo '<p><strong>addresline4:</strong> '.$addresline4.'</p>'; echo '<p><strong>addresline5:</strong> '.$addresline5.'</p>'; echo '<p><strong>streetcomp:</strong> '.$streetcomp.'</p>'; ?> 没有达到我认为应该具有的预期效果。

我在这里尝试做的是,如果一个变量内容部分匹配另一个,则覆盖该变量。

street: Lightbowne Road

addresline1: addresline1

addresline2: addresline12

addresline3: addresline3

addresline4: jim

addresline5: addresline5

因此,对于上面的代码,所需的输出应为:

    street: Lightbowne Road

    addresline1: 284 Lightbowne Road

    addresline2: Lightbowne Road

    addresline3: 284 Lightbowne Road

    addresline4: jim

    addresline5: addresline5

但它不会出现:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")

所以它基本上只做一个完全匹配。我们不想要的。

2 个答案:

答案 0 :(得分:3)

  

记住PHP中的哪些订单参数就像在针中搜索干草堆一样

manual for stripos()将其参数标记为string $haystack , string $needle;也就是说,它会在第一个参数中搜索第二个参数。

所以你的论据是错误的:if ( stripos($addresline1, $street) !== false ) {

当字符串相同时它起作用,因为如果$a$b相同,则$a包含$b {{1} }包含$b,所以它会找到你放置参数的方式。

答案 1 :(得分:1)

您所要做的就是从条带中的两个参数中更改位置。此代码将为您提供预期的输出。

参考:http://php.net/manual/en/function.stripos.php

Stripos搜索第一个内部的第二个参数,而不是第二个参数。

<?php
    $street = 'Lightbowne Road';
    $addresline1 = '284 Lightbowne Road ';
    $addresline2 = 'Lightbowne Road ';
    $addresline3 = '284 Lightbowne Road';
    $addresline4 = 'jim ';
    $addresline5 = 'Lightbowne Road';
    if (  stripos($addresline1, $street ) !== false ) {
        $addresline1 = 'addresline1 ';
    }
    if (  stripos($addresline2, $street) !== false ) {
        $addresline2 = 'addresline2 ';
    }
    if (  stripos($addresline3, $street) !== false ) {
        $addresline3 = 'addresline3 ';
    }
    if (  stripos($addresline4, $street) !== false ) {
        $addresline4 = 'addresline4 ';
    }
    if (  stripos($addresline5, $street) !== false ) {
        $addresline5 = 'addresline5 ';
    }
    $addresslines = $addresline1.$addresline2.$addresline3.$addresline4.$addresline5;
    $streetcomp = trim($street.' '.$addresslines);

echo '<p><strong>street:</strong> '.$street.'</p>';
echo '<p><strong>addresline1:</strong> '.$addresline1.'</p>';
echo '<p><strong>addresline2:</strong> '.$addresline2.'</p>';
echo '<p><strong>addresline3:</strong> '.$addresline3.'</p>';
echo '<p><strong>addresline4:</strong> '.$addresline4.'</p>';
echo '<p><strong>addresline5:</strong> '.$addresline5.'</p>';
echo '<p><strong>streetcomp:</strong> '.$streetcomp.'</p>';

?>