The parable of the pearls before swine was given by Jesus a short time before the Passover season of 28 A.D. It was offered as part of his extensive Sermon on the Mount message given to both his disciples and to the public interested in his teachings.
Interestingly, although somewhat rare and usually sought after, the KJV bible mentions pearls in only eight places. Four of these locations are in the book of Revelation. The verse you are referring to is in Matthew 7. This verse records Jesus stating a parable that we are not to offer holy things to dogs and we are not to cast our pearls before swine. Why is this the case? It is because such animals might walk over them and then afterwards come and attack us!
Jesus stated, "Do not give that which is holy to the dogs, nor cast your pearls before the swine, lest they trample them under their feet, and turn around and tear you in pieces" (Matthew 7:6, HBFV).
The word "pearls" in Matthew 7 is symbolic of godly wisdom and the salvation brought by his kingdom. God's wisdom is likened to something of a very great price in another reference in Matthew (see Matthew 13:45 - 46). All this is discussing God's kingdom and the wisdom one must acquire in order to enter it (Job 28:12 - 20).
Wisdom is found using the Holy Spirit, which is only given to those who obey him (Acts 5:32). How can, however, understanding and knowledge lead us to our own pearls of great price (Matthew 13:45 - 46)? How can they bring us to the pure truth of God? It is only through recognizing and obeying his laws and his ways (Psalm 119:104).
God's precepts are written in the Scriptures. If one has his Spirit, one can study his Word (His Precepts) for Holy knowledge, and upon this foundation the Eternal will then grant Holy understanding.
Wisdom like Pearls
If we take our wisdom (like precious pearls) and throw it all around without knowing whether it might fall before dogs (an impure or self-righteous heart) or swine (someone who is considered unclean), Jesus warns us that they will likely tear us to pieces. This means they will twist and distort what is said and then come after us!
God tells us in the book of Proverbs that there are times when we should be silent (not casting our pearls before others) and times when we should respond directly (especially about what is the truth). Jesus sometimes answered the questions and accusations thrown his way by the religious leaders of his day (e.g. Matthew 12:1 - 8) and sometimes he refused to respond to their inquiries (e.g. Matthew 21:23 - 27).
We must discern the reason for any questions according to the person who is doing the asking and answer within that context.
Do not answer a fool according to his folly, lest you also be like him. Answer a fool according to his folly, so that he may not be wise in his own conceit (Proverbs 26:4 - 5, HBFV).
We should not cast our pearls of Godly wisdom before "swine," meaning someone who is a militant, ready to fight, unbeliever lest they turn on us and insult us or do us harm.