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?
  }
}
15
  • "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. Commented Jul 18, 2016 at 5: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. Commented Jul 18, 2016 at 5:33
  • To turn '常州的' into an array: ['常州的'], if you have more words, then just separate by commas. Commented Jul 18, 2016 at 6:43
  • Im just confused with iterating through the array and applying my new text for it Commented Jul 18, 2016 at 6: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? Commented Jul 18, 2016 at 6:45

2 Answers 2

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

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

Comments

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.

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.