0

How can I remove instances of words from a string which do not appear in an array?

$myVar = "my sisteralannis is not that blonde, here is a goodplace";
$myWords=array(
    array("is","é"),
    array("on","no"),
    array("that","aquela"),
    array("sister","irmã"), 
    array("my","minha"),
    array("myth","mito"),
    array("not","não"),
    array("he","ele"),
    array("good","bom"),
    array("place","lugar"),
    array("here","aqui"),
    array("ace","perito")
); 

echo $myVar;

The strings 'sisteralannis' and 'goodplace' do not exists in the $myWords array and should be removed from the string.

Expected Output: my is not that, here is

2
  • @mickmickusa , is much simpler now, just removing of the variable the strings that do not exist in the array, solve my problem. Commented Dec 15, 2017 at 1:53
  • why u dont combine all array to one 2d array Commented Dec 15, 2017 at 3:26

2 Answers 2

2

Try to explode that variable, and check in a loop if they match.
Try this example:

$myVar = "my sisteralannis is not that blonde, here is a goodplace";

$myWords=array(
        array("is","é"),
        array("on","no"),
        array("that","aquela"),
        array("sister","irmã"), 
        array("my","minha"),
        array("myth","mito"),
        array("not","não"),
        array("he","ele"),
        array("good","bom"),
        array("place","lugar"),
        array("here","aqui"),
        array("ace","perito")
    );

$words = explode(" ",$myVar);

foreach($myWords as $w){
    $words = array_diff($words,$w);
}

$words = array_diff(explode(" ",$myVar),$words);

echo implode(" ",$words);

Output:

my is not that here is
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot @EL3PHANTEN, but it lacks a small adjustment, its code mixes the order of the strings in the output: is that my not here
@ArianeMartinsGomesDoRego oh yes ofc. Fixed it :) and a bit cleaner
Thank you very much, it really was very good, it works in first attempt on my project too. Thank you very much :-)
@ArianeMartinsGomesDoRego You are wellcome. :)
1

Please see the code below. Ive added a function inspect_data() to trim the data, and ive added a function called exclude_characters() to consider the characters that you dont want to be included on your data comparison.

<?php 

function inspect_data($text, $array){
    $data = '';
    foreach($array as $myWord){
        if(in_array($text,$myWord)){            
            $data=$text;
        }

    }
    return $data;
}

function exclude_characters($text){

    $excluded_characters = array(',','!');

    $data['string'] = '';
    $data['special'] = '';
    foreach($excluded_characters as $char){
        if (strpos($text, $char) !== false) {
            $data['string'] = str_replace($char,"",$text);
            $data['special'] = $char;
        }
        else{
             $data['string'] = $text;
          
        }
    }
    return $data;
}

$myVar = "my sisteralannis is not that blonde, here is a goodplace";
    $myWords=array(
        array("is","é"),
        array("on","no"),
        array("that","aquela"),
        array("sister","irmã"), 
        array("my","minha"),
        array("myth","mito"),
        array("not","não"),
        array("he","ele"),
        array("good","bom"),
        array("place","lugar"),
        array("here","aqui"),
        array("ace","perito")
    ); 

$newVar = '';
$var = explode(" ",$myVar);
foreach($var as $v){
    
    $data = exclude_characters($v);
    $newVar .= inspect_data($data['string'],$myWords).$data['special']." ";
    
}
echo $newVar;






 ?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.