0

I have an array $track['context'] This outputs the following

常州, 的, 妈咪ZA, 已揽件快件已从, 常州, 发出快件到达

Each one of these are tracking details.

I am running the array through the following code to try and run a preg_match for each item that is inside of $track['context'] then replace if a string from $badWords is present

$badWords = ['常州', '的']; // I would want these to end up being ['one', 'two']
$arrayToCheck = $track['context'];
foreach ($badWords as $badWord) {
  if (preg_match("/($badWord)/", $arrayToCheck)) {
        // Do I run my preg_match function here?
  }
}
NooBskie
  • 3,761
  • 31
  • 51
  • *"I am running the array through the following code"*: the code produces errors. (1) *$badWords* is not an array, so `foreach` on it cannot work. (2) *arrayToCheck* is not a string, so *preg_match* cannot work. – trincot Jul 18 '16 at 05:31
  • Yea maybe i should explain better. How would I make `$badWords = '常州的';` into an array and run it through a `foreach` with the appropriate strings i want to replace it with. – NooBskie Jul 18 '16 at 05:33
  • To turn '常州的' into an array: `['常州的']`, if you have more words, then just separate by commas. – trincot Jul 18 '16 at 06:43
  • Im just confused with iterating through the array and applying my new text for it – NooBskie Jul 18 '16 at 06:44
  • Please provide more representative sample data, both for the words to search and the translations. Please add these in your question as variables, so the structure you have is clear. Also, what is *$tracking_info*? Does it have anything to do with the question? – trincot Jul 18 '16 at 06:45
  • I've edited the question now And removed `$tracking_info` as it is only for validation on my production site – NooBskie Jul 18 '16 at 07:01
  • How do you decide to put 'one', 'two' and not 'hello', 'there'? Do you have a translation from-to mapping somewhere? – trincot Jul 18 '16 at 07:09
  • `one` `two` are just manual strings I input myself. I basically just want a one to one mapping – NooBskie Jul 18 '16 at 07:12
  • But a program cannot just guess what to put, so what do you want it to put? – trincot Jul 18 '16 at 07:13
  • Would it be better to just run a if statement if it detects a match of `$badWords`? – NooBskie Jul 18 '16 at 07:17
  • Why do you ask me? I don't know what you actually want to have as final result. Do you want to translate? Then you need to have the translations somewhere. Do you want to just know the positions (0, 1, 2, ...) in the `$track['context'];` array that have at least one of these bad words? Or do you want occurrences to be replaced with something? If so, what should be the replacement? It is your call... just be specific in your question about the desired outcome. – trincot Jul 18 '16 at 09:27
  • I dont know how much more specific i can be... I am running the array through the following code to try and run a preg_match for each item that is inside of $track['context'] then replace if a string from $badWords is present `if preg_match $badWord { preg_replace 'my_own_string' } ` Im just wanting to iterate through the badwords and preg_replace the ones that are matched I can do it in javascript im just not sure how to do it in php – NooBskie Jul 18 '16 at 09:34
  • OK, so you want all occurrences of all bad words to be replaced with the literal 'my_own_string'? (so all the same?) – trincot Jul 18 '16 at 09:35
  • The my_own_string part will be me manually typing them into an array i just don't know how to match them in a one-to-one way **edit** `my_own_string` should be an array of the strings actually my mistake – NooBskie Jul 18 '16 at 09:38
  • But yes any thing matching 'bad_string' will be replaced by 'good_string' its just that there are a lot of strings that need to be changed – NooBskie Jul 18 '16 at 09:45

2 Answers2

1

I would suggest a data structure for the bad words, where the word is the key and the replacement the value in an associative array.

Then you could loop over your content array, and do the replacement with a callback function:

// Sample data:
$track['context'] = array(
    'qdf 常州', 
    'fdhlkjfq fdkq ',
    '的 fdsqfsf'
);
// Make a translation table for the bad words:
$badWords = [
    '常州' => 'one',
    '的' => 'two'
];
// Build a regular expression that matches any of the above words:
$regexp = "/\b(" . implode('|', array_map('preg_quote', array_keys($badWords))) . ")\b/u";

// Iterate over the content
foreach ($track['context'] as &$subject) {
    $subject = preg_replace_callback($regexp, function($matches) use ($badWords) {
        // Replace the matched bad word with what we have mapped for it:
        return $badWords[$matches[0]];
    }, $subject);
}
// Output results:
print_r ($track['context']);

See it run on eval.in

trincot
  • 317,000
  • 35
  • 244
  • 286
0

First of all, make $badWords an array, like:

$badWords = array('bad_word1', 'bad_word2');

Second, I would use the strpos function, which is used to find the occurrence of one string inside other.

Lastly, remember to set $noBadWordsFound to false in your code.

Community
  • 1
  • 1
Sitron_NO
  • 9
  • 1