array results to show all after string replace

Hello,

I am novice and have some code reused from previous post.  It is partially working, returning only the matched strings with my replace key.  However, I'd like for it to return all strings from array (both string replaced and without any changes).  

Also, I need to return match regardless of character.  I was thinking of setting initial array to string lower case, but looking for some advise there as well.

Thanks in advance.

currently returns:

 [1]=> "a splendid <mark>Volvo</mark> runs fast"
 [2]=> "x5-pento and <mark>Volvo</mark>"
 [3]=> "x5-<mark>pento</mark> and <mark>Volvo</mark>"

and need for it to return:

[1]=> "a splendid <mark>Volvo</mark> runs fast"
[2]=>"XC90- <mark>volvo</mark>"
[3]=>"some other cars run"
[4]=>"x5- <mark>pento</mark> and <mark>Volvo</mark>"
[5]=>"BMWi"

<?php

error_reporting(E_ALL);
echo '<pre>';
          

// TEST DATA FROM THE POST AT EE
$m[] = array();
$ques = array("a splendid Volvo runs fast", "XC90-volvo", "some other cars run",  "x5-pento and Volvo", "BMWi");
$key = array("Volvo", "pento", "Mustang");


// LOOK FOR SUBSTRINGS
foreach ($ques as $ques_string)
{
    foreach ($key as $key_string)
    {
        if (strpos($ques_string, $key_string) === FALSE) continue;
        $ques_string = str_replace("$key_string", "<mark>$key_string</mark>", $ques_string);
        $m[] = $ques_string;
    }
}

// SHOW THE WORK PRODUCT
var_dump($m);



        
?>
johnsonallstarsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
I'm not sure to understand your logic, but could this make sense?

$m[] = array();
$ques = array("a splendid Volvo runs fast", "XC90-volvo", "some other cars run",  "x5-pento and Volvo", "BMWi");
$key = array("Volvo", "pento", "Mustang");


// LOOK FOR SUBSTRINGS
foreach ($ques as $ques_string)
{
    $m[] = $ques_string;
    foreach ($key as $key_string)
    {
        if (strpos($ques_string, $key_string) === FALSE) continue;
        $ques_string = str_replace("$key_string", "<mark>$key_string</mark>", $ques_string);
        $m[] = $ques_string;
    }
}

// SHOW THE WORK PRODUCT
var_dump($m);

Open in new window

0
johnsonallstarsAuthor Commented:
Hi Marco,

I ran your script, but it's returning this….

 [1]=> "a splendid Volvo runs fast"
 [2]=> "a splendid <mark>Volvo</mark> runs fast"
 [3]=> "XC90-volvo"
 [4]=> "some other cars run"
 [5]=> "x5-pento and Volvo"
 [6]=> "x5-pento and <mark>Volvo</mark>"
 [7]=> "x5-<mark>pento</mark> and <mark>Volvo</mark>"
 [8]=> "BMWi"


I am looking avoid the repeated results in the array.  So, it only displays the final updated (string replace and non updates)..   Essentially, the wanted result would be like this below:

 [1]=> "a splendid <mark>Volvo</mark> runs fast"
 [2]=> "XC90-volvo"
 [3]=> "some other cars run"
 [4]=> "x5-<mark>pento</mark> and <mark>Volvo</mark>"
 [5]=> "BMWi"

So, basically, it would have the same number of strings/results as the Original $ques array + all the keys marked with <mark>.  html highlight option

Does that help?
0
Ray PaseurCommented:
It's usually easier to show us the before-and-after data sets than to try to describe the rules.  It's not clear to me exactly what the inputs and outputs should be, but I'll take a shot at it, however guessing is, in my experience, the least reliable way of achieving good results.  Maybe this design can offer some guidance...  Please use "view source" to see the data.
http://iconoun.com/demo/temp_johnsonallstars.php
<?php // demo/temp_johnsonallstars.php

/**
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28674956.html
 * http://php.net/manual/en/function.str-ireplace.php
 */
error_reporting(E_ALL);

// AGGREGATE THE RESULTS IN THIS ARRAY
$m = [];

// SOME TEST DATA
$ques = array
( "a splendid Volvo runs fast"
, "XC90-volvo"
, "some other cars run"
,  "x5-pento and Volvo"
, "BMWi"
)
;
// SOME SIGNAL STRINGS
$key = array
( "Volvo"
, "pento"
, "Mustang"
)
;
// LOOK FOR SUBSTRINGS
foreach ($ques as $ques_string)
{
    // TEST FOR EACH OF THE KEYS
    foreach ($key as $key_string)
    {
        // IF THIS KEY IS NOT IN THE STRING, SKIP ON TO THE NEXT KEY (TEST IS CASE-INSENSITIVE)
        if (stripos($ques_string, $key_string) === FALSE) continue;

        // A KEY WAS FOUND - ADD THE ORIGINAL STRING TO THE AGGREGATION ARRAY
        $m[] = 'BEFORE: ' . $ques_string;

        // MODIFY THE ORIGINAL STRING TO ADD SOME MARKUP (REPLACEMENT IS CASE-INSENSITIVE)
        $ques_string = str_ireplace("$key_string", "<mark>$key_string</mark>", $ques_string);

        // ADD THE MODIFIED ORIGINAL STRING TO THE AGGREGATION ARRAY
        $m[] = 'AFTER: ' . $ques_string;
    }
}

// SHOW THE WORK PRODUCT
echo '<pre>';
print_r($m);

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Ray PaseurCommented:
Or maybe like this?  There are some things here that are powerful code smells, such as having arrays numbered from one instead of zero (most programming languages number arrays from zero).  If you're new to programming in general and PHP in particular, this article might be a good source of learning resources.

<?php // demo/temp_johnsonallstars.php

/**
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28674956.html
 * http://php.net/manual/en/function.stripos.php
 * http://php.net/manual/en/function.str-ireplace.php
 */
error_reporting(E_ALL);

// AGGREGATE THE RESULTS IN THIS ARRAY
$m = [];

// SOME TEST DATA
$ques = array
( "a splendid Volvo runs fast"
, "XC90-volvo"
, "some other cars run"
, "x5-pento and Volvo"
, "BMWi"
)
;
// SOME SIGNAL STRINGS
$keys = array
( "Volvo"
, "pento"
, "Mustang"
)
;
// LOOK FOR SUBSTRINGS
foreach ($ques as $ques_string)
{
    // COPY THE STRING
    $new_string = $ques_string;

    // TEST FOR EACH OF THE KEYS
    foreach ($keys as $key_string)
    {
        // SAVE THIS STRING, USING THE ORIGINAL STRING AS A KEY
        $m[$ques_string] = $new_string;

        // IF THIS KEY IS IN THE STRING, WORK WITH IT (TEST IS CASE-INSENSITIVE)
        if (stripos($new_string, $key_string) !== FALSE)
        {
            // MODIFY THE STRING TO ADD SOME MARKUP (REPLACEMENT IS CASE-INSENSITIVE)
            $new_string = str_ireplace("$key_string", "<mark>$key_string</mark>", $new_string);

            // ADD THE MODIFIED ORIGINAL STRING TO THE AGGREGATION ARRAY
            $m[$ques_string] = $new_string;
        }
    }
}

// REMOVE UNWANTED DUPLICATES AND RENUMBER THE ARRAY (STRANGE REQUIREMENT)
$m = array_unique($m);
$k = 1;
foreach ($m as $new_string)
{
    $out[$k] = $new_string;
    $k++;
}

// SHOW THE WORK PRODUCT
echo '<pre>';
print_r($out);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
johnsonallstarsAuthor Commented:
Hi Ray,

I'll try to show before/after here as best as possible.   Sorry, for the guessing, I hope this works.

Before data (string data, 5 strings in array):

"a splendid Volvo runs fast",
"XC90-volvo",
"some other cars run",  
"x5-pento and Volvo",
"BMWi"



This is the result when I copy/paste your php code from view source

result:

<pre>Array
(
    [0] => Array
        (
        )

    [1] => BEFORE: a splendid Volvo runs fast
    [2] => AFTER: a splendid <mark>Volvo</mark> runs fast
    [3] => BEFORE: XC90-volvo
    [4] => AFTER: XC90-<mark>Volvo</mark>
    [5] => BEFORE: x5-pento and Volvo
    [6] => AFTER: x5-pento and <mark>Volvo</mark>
    [7] => BEFORE: x5-pento and <mark>Volvo</mark>
    [8] => AFTER: x5-<mark>pento</mark> and <mark>Volvo</mark>
)


…This below is the after …. a result that I would want, but not able to code at the moment….

 [1]=> "a splendid <mark>Volvo</mark> runs fast"
 [2]=> "XC90-<mark>volvo</mark>"
 [3]=> "some other cars run"
 [4]=> "x5-<mark>pento</mark> and <mark>Volvo</mark>"
 [5]=> "BMWi"
0
johnsonallstarsAuthor Commented:
Ohh, think I see now.

results from your 2nd post:
 
<pre>Array
(
    [1] => Array
        (
        )

    [2] => a splendid <mark>Volvo</mark> runs fast
    [3] => XC90-<mark>Volvo</mark>
    [4] => some other cars run
    [5] => x5-<mark>pento</mark> and <mark>Volvo</mark>
    [6] => BMWi
)
0
johnsonallstarsAuthor Commented:
Hi Ray,

Thank you.  I wonder though.  I'm not sure why it would be too strange..  I'm doing resultant data, same as starting data; only marked-up.  It sounds like I might be taking the wrong approach to this or I need to be more clear on subject line (hind-sight).

Also, I liked the article you mentioned.

Many thanks!
0
Ray PaseurCommented:
Yeah, getting clarity on the objectives is one of the real challenges in an online forum like this -- when I try to give beginner advice to someone who does not think he is a beginner, he gets offended.  And when I try to give expert advice to someone who is not an expert, she thinks I am creepy.  :-)

That said, if we are making progress it is a good thing!

One of the risks in the design of this application would arise if there were overlapping or duplicated information in the           input data set.  The edge cases are where madness lies, and smart programmers try to avoid the edges.

This is what I get from the second code snippet.  I think I posted all the right code...
<pre>Array
(
    [1] => a splendid <mark>Volvo</mark> runs fast
    [2] => XC90-<mark>Volvo</mark>
    [3] => some other cars run
    [4] => x5-<mark>pento</mark> and <mark>Volvo</mark>
    [5] => BMWi
)

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.