1

For some reason stripos isn't having the desired affect I thought it should have.

What im trying to do here is if one variables contents partially match another then override that variable.

<?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>';

?> 

So for the above code the desired output should be:

street: Lightbowne Road

addresline1: addresline1

addresline2: addresline12

addresline3: addresline3

addresline4: jim

addresline5: addresline5

but it does not it comes out as:

    street: Lightbowne Road

    addresline1: 284 Lightbowne Road

    addresline2: Lightbowne Road

    addresline3: 284 Lightbowne Road

    addresline4: jim

    addresline5: addresline5

So its basically just doing an exact match. Which we don't want.

0

2 Answers 2

2

Remembering which order arguments go in PHP is like searching for a haystack in a needle

The manual for stripos() labels its arguments as string $haystack , string $needle; that is, it searches for the second argument in the first argument.

So you have your arguments the wrong way around: if ( stripos($addresline1, $street) !== false ) {

It works when the strings are identical because if $a is identical to $b, then $a contains $b and $b contains $a, so it will find it whichever way you put the parameters.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the second pair of eyes. will accept in 10 mins
0

All you have to do is to change the place from your two parameters in your stripos. This code will provide you the expected output.

Reference: http://php.net/manual/en/function.stripos.php

Stripos search the second parameter inside the first, not the first inside the second.

<?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>';

?> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.