Oscar Reyes
Oscar Reyes

Reputation: 4352

check if a word in string match with a key in array, then replace it

i want to find a keyword inside a string, im trying to make the script to find it and then replace it, but only that keyword (of course if others are found, replace them as well). but im lack of experience for this job and im trying to do it just for practice. this is my beginning code:

 $var = array(
    '{test1}' => 'something1',
    '{test2}' => 'something2',
    '{test3}' => 'something3'
);

$output = 'Please work it var {test1}!';

foreach($var as $element)
{
    if(strstr($output, $element) !== false)
    {
        echo 'not found<br>';
    }
    else
    {
        echo 'found<br>';
    }
}

for now im just checking if its found, but what i dont understand, why its repeating? it should just say "found" once and then say "not found", like this (found, not found, not found).

output:

found
found
found

Upvotes: 0

Views: 137

Answers (2)

Bryan
Bryan

Reputation: 3494

This is a little confusing for me, as strstr is usually used to grab a portion of the string. I think a more appropriate approach would be using strpos. also you're not testing the key. don't even know why it's saying found. try

foreach($var as $key=>$val)
{

    if(strpos($output, $key) !== false)
    {
        //the key is contained in the output.
    }
    else
    {
        //not found
    }
}

if you let us know what you're trying to replace, I can see about helping with that. Also don't listen to everyone telling you to use == to compare against false. === or !== is the appropriate comparison as there are other things that == false(ie.. 0 ==false).

Realistically though, if you're trying to just replace the key of your array with the value, there is no need for a check.

foreach($var as $key=>$val)
     $output = str_replace($key, $value, $output);

if the key is not found, nothing happens, if it is, it's replaced with its corresponding value. No check necessary.

Upvotes: 1

AJReading
AJReading

Reputation: 1223

First of all you have your if statement wrong. It is not finding it 3 times, that's why it is repeating (3 times for each item in your array):

if(strstr($output, $element) == false)

Remove the !. What you are saying is, if a match is found then echo "not found".

That is one problem, the second is that your code will not work. You are looking for the values of your array, rather than the keys. Try this:

$var = array(
    '{test1}' => 'something1',
    '{test2}' => 'something2',
    '{test3}' => 'something3'
);

$output = 'Please work it var {test1}!';

foreach($var as $search => $replace)
{
    if(strstr($output, $search) == false)
    {
        echo 'not found<br>';
    }
    else
    {
        echo 'found<br>';
    }
}

The difference here is that you can access the array keys and values in the foreach buy using foreach (array_expression as $key => $value). I named the array keys $search since that is what you are looking for. Then you can access the array value using $replace to replace the string once found like so:

$var = array(
    '{test1}' => 'something1',
    '{test2}' => 'something2',
    '{test3}' => 'something3'
);

$output = 'Please work it var {test1}!';

foreach($var as $search => $replace)
{
    if(strstr($output, $search) == false)
    {
        echo 'not found<br>';
    }
    else
    {
        echo 'found - (' . str_replace($search, $replace, $output) . ')<br>';
    }
}

Upvotes: 0

Related Questions