A list of denied words, with a condition only if glued with symbols
-
[moved to Developing WordPress. Please use this for any coding questions. The old Plugins and Hacks forum is no longer in use.]
A list of denied words, with a condition only if glued with symbols
//my arrays
$Rejected_symbols = array('*','#','-','!','?','+','=','¨','^','~','@'); $Rejected_if_with_symbols = array('t1','t2','t3','t4');
// my search 1 – symbols always rejecteds
$search = $Rejected_symbols; $replace = array('Forbidden symbol','Forbidden symbol','Forbidden symbol','x','x','x','x','x','x','x','x'); (my-function)
// my search 2 – Words rejecteds, if theys are glueds with symbols
$search = $Rejected_if_with_symbols; $replace = array('Prohibited combination','Prohibited combination','Prohibited combination','Prohibited combination'); (my-function)
// my search 3, words always rejecteds and your array.
$search = array('string1','string2','string3','string4'); $replace = array('forbiten word','forbiten word','forbiten word','forbiten word'); (my-function)
In the first search, success, the forever prohibited symbols,
In the third search, success, words that do not desire are always forbidden.
But in SECOND search, words are always denied, and these are important must pass, only can be denied if they are glued with simbols in the first array.this words: (‘t1′,’t2′,’t3′,’t4’); in seatch 2 Are always being denied, and I do not want this, I want them denied only if they are glued with symbols:
%t1 ou ?t1 ou +t1 etc..
-
I only get what I want in an unintelligent way based on my limited knowledge:
$Search = array ('#t1', '&t1', ';t1', '%t1', '*t1', .... many values ??.... '-t2', '=t2', '@T2', '* t2', '!T2', '@t2', '#t2', '%t2' ... many values ??... '.t3', '@t3', ';t3 ','% T3 ',' *t3 ','~t3 ', ... a looooottttt .....);
well… is working, I put all kinds of possible formations, and so my code is very giant!
Hello @herculesnetwork my friend! There’s been a few changes in the forums since we last talked. The Plugins and Hacks sub-forum we used to use is no longer active. Code related questions should go in Developing WordPress. You’ll see I moved your topic to the correct place this time. The old forum is not monitored, so don’t use it any more!
Your situation is a perfect example where the power of regexp shines. You can specify a specific pattern of characters while applying any number of logical conditions. It’s also possible to capture sub groups for further processing, though you need not be concerned with this aspect right now. There are many tutorials available. Do a search and pick one that appeals to you. Regexp use is unrelated to WP, so don’t look for WP support in selecting a tutorial. It’s not important.
It’s difficult to compose correct regexp expressions “on the fly” (correct on first attempt without intermediate testing). It’s useful to have an environment where you can fiddle around with different expressions and see right away how they affect test data. There’s many sites that allow this, for example regexr.com.
Once you have the proper regexp, use it in something like preg_match() to determine if there is a match or not.
As a basic example to get you started, consider this regexp:
"/[&%^](and|or|to)/"
A match is made when any one of the symbols in square brackets occurs in front of any of the words in parenthesis. ‘&’ by itself does not match, ‘to’ by itself does not match, but ‘&to’ will match, as does ‘%to’ and ‘%or’, etc.
You can of course compose one large, all encompassing regexp, but that could get confusing and be hard to read. When it comes to specifying data in PHP, declaring arrays is often useful. Unfortunately, there is no way for regexp to directly use PHP array data. What you can do though, is use implode() to convert arrays into proper regexp fragments, then concatenate them together into a final regexp.
Thanks my friend. Thanks for pointing regex ??
Ok, thanks, now I now aboud new location to ask questions.
Regex seemed confused for the case:
"/[,.;](red|green|blue)/"
Therefore , correspond to red and . Match for green …..
Is not the case, I need all the signs between [] to match all words between () and not the second symbol for the second word, and third symbol for the third word, needs all to match for all.I tried imploding on an issue I made here and even marked it as unsupported, I thought it was silly. Let’s forget her
I’ve been watching, I think my case would be more preg_match
Because I want that, any value of $ variavel1 is pasted to any value of $ variable2, those values ??are considered and denied.$Variable1 = '!@#$%*(_+'; $Variable2 = 'abcdefgh';
@c would be denied
#c denied
%C denied, any $variable1 symbol that appears before any $variable2 words would be denied.Thanks ??
// a simpler thing that would get me what I need is:
How do I concatenate all the values of a variable1 with all values of variable2$Var1 = 'my1, my2, my3'; // here I have dozens of entries, they are symbols $Var2 = 'word1, word2, word3'; // here also dozens of entries, are words.
How do I have all the keys of a variable, placed together of the keys of a second variable?
$Values_that_I_needed = 'my1word1, my1word2, my1word3, my2word1, my2word2, my2word3, my3word1, my3word2, my3word3 ';
How would I build this variable up with all those values without having to type everything!i?
Imagine an example with 60 my1, my2 … and 130 word1, word2 …. it’s my case!
Put each of the 60my before each of the 130 words !!
// I need to concatenate / join / join each values / keys of a variable, with all the values/keys of another variable, to avoid making all these combinations by hand. and put in another variable.The solution I founded using explode and trim functions ?? SOLVED Thanks again ??
Any time ??
FYI, you had some trouble with regexp special characters because some need to be escaped because they have special meaning in regexp. For example, ‘?’ means one or no occurrence of the preceding token, so to actually use ‘?’ as a question mark you need to specify ‘\?’. Another useful feature is you can specify a range of ascii characters.
[!-/]
will match any character between ! and / in an ascii table, such as # and &. Specifying 3 ranges will match all punctuation characters I believe.There is also a regexp symbol to match any word: \w With 130 words to match, it would be more expedient to match special characters preceding any word. Since regexp can capture matches, you can capture what ever word matched and do a secondary check of the matched word using in_array() to see if it’s in the array list.
What you’ve done is fine, I’m not suggesting you now change to use regexp. I’m only pointing out just how powerful regexp can be. Maybe next time ??
I think it’s not going to be next time, it should be for this time, same rsrs
??? I was so excited when I saw the values of several being compiled in a print that I soon celebrated and I came here to say that I was soldered, but when I went to do with arrays for my solution, it did not work rsrrsr
—
I need to combine each of the values between each key between two ARRAYS
<?php $Var1 = array('my1', 'my2', 'my3'); $Var2 = array('word1', 'word2', 'word3'); $result = ""; foreach (explode(',', $Var1) as $w1) { foreach (explode(',', $Var2) as $w2) { $result .= trim($w1) . trim($w2). ', '; } } $result = trim($result, ', '); print_r($result); $search = $result; $replace = array('combination disallow','combination disallow','combination disallow'); $mytextfield = str_replace($search,$replace,$mytextfield);
If someone enters any of these combinations:
my1word1, my1word2, my1word3, my2word1, my2word2, my2word3, my3word1, my3word2, my3word3';
This will appear in place:
combination disallow
This example is with 3 values in each array, but in my potion, they are with 110 values in each of these two arrays, 1102 !
This function already works fine, but instead of $result, I’m having to do $Valuesmanually, and it’s almost impossible to do this manually with 110 keys in every $Var1 and $Var2 combining all of them, 1102,
I need to combine this As I learned how to combine strings into a variable in my previous question. But trying to make combinations with each key of the two arrays did not work.$regex = “/[!@#$%*()_’?’+{`}[]oa;.,/&]-(word1|word3|word3)/”;
So will this match each of these symbols with any words above?
?word1
]word1
*wprd3
etc..This my function above in print gets in a webpage doing in a new index.php, perfect.
But it does not work for what I need, because I want to use this data for comparison, and with non-gun strings, just like key in arrays, but this function does not adapt to arrays ??————-
$Var1 = ‘my1, my2, my3’; // here I have dozens of entries, they are symbols
$Var2 = ‘word1, word2, word3’;
$result = “”;
$var2_list = explode(‘,’, $Var2);foreach (explode(‘,’, $Var1) as $w1) {
foreach ($var2_list as $w2) {
$result .= trim($w1) . trim($w2). ‘, ‘;
}
}
$result = trim($result, ‘, ‘);
print_r($result);
The output:my1word1, my1word2, my1word3, my2word1, my2word2, my2word3, my3word1, my3word2, my3w
Works fine, but I need to do with arrays like I did in the post
$regex = "/[!@#$%*()_'?'+{
}[]oa;.,/&]-(word1|word3|word3)/”;
So will this match each of these symbols with any words above? ?word1 ]word1 *wprd3 etc.. This my function above in print gets in a webpage doing in a new index.php, perfect. But it does not work for what I need, because I want to use this data for comparison, and with non-gun strings, just like key in arrays, but this function does not adapt to arrays :( -------------
$Var1 = ‘my1, my2, my3’; // here I have dozens of entries, they are symbols
$Var2 = ‘word1, word2, word3’;
$result = “”;
$var2_list = explode(‘,’, $Var2);foreach (explode(‘,’, $Var1) as $w1) {
foreach ($var2_list as $w2) {
$result .= trim($w1) . trim($w2). ‘, ‘;
}
}
$result = trim($result, ‘, ‘);
print_r($result);The output:
my1word1, my1word2, my1word3, my2word1, my2word2, my2word3, my3word1, my3word2, my3w
`
Works fine, but I need to do with arrays like I did in the postOK, maybe we should revisit regexp again. First lets look at the symbols. As I mentioned, you’ll be better off specifying ranges. For example,
[!-.:-@[-_{-?]
should match nearly any symbol except forward slash / and backtick. Do not follow this with a hyphen – or else the regexp will only match symbols followed by a hyphen! Follow this with parenthesis so anything matching within is captured in $matches. Inside the parenthesis add the word selectors(\w+)
. Thus the full regexp is'/[!-.:-@[-_{-?](\w+)/'
.This will match any word, forbidden or not, preceded by nearly any symbol. All matched words will be in the preg_match() $matches array. Check all $matches except $matches[0] for occurrence in an array of forbidden words using in_array(). If in_array() returns true, then there is a forbidden symbol/word construct.
This way you only need to maintain one array of forbidden words and they do not weigh down the regexp with a huge list of OR conditions, nor need to be used to generate endless permutations. If you would rather maintain the words as a comma delimited list, use explode() to turn it into an array.
You seem to be confused about using explode() in your first reply after my last. You are trying to explode an array there. explode() is for strings like $var = ‘word1,word2,word3,word4’;, which are then converted to arrays. To convert arrays into strings, use implode(). In your more recent posts you seem to have figured this out, so maybe this is no longer an issue.
You are using trim() in a loop, which is fine. FYI, a useful shortcut is to use array_map(). To trim all array elements at once, you could do
$var = array_map('trim', $var1 )
. You still need to run nested loops to construct permutations, but you can remove all the trim() calls from the loops because everything has already been trimmed with array_map(). Thus your loop code is cleaner and what you are trying to do is more evident.Would that be close to making the combination of all the keys?
$Var1 = array('my1', 'my2', 'my3'); $Var2 = array('word1', 'word2', 'word3'); $result = ""; foreach (explode(',', $Var1) as $key => $w1) { foreach (explode(',', $Var2) as $key => $w2) { $result .= trim($w1) . trim($w2). ', '; } } $result = trim($result, ', '); print_r($result);
OPs!
I sent this post above without seeing your answer, before my last page reload here, there was no answer yet! I’ll analyze the $var = array_map (‘trim’, $var1) I’ve already seen a stackOv .. responded that used array_mapthis working very well to show string on screen:
$Var1 = ‘my1, my2, my3’; // here I have dozens of entries, they are symbols $Var2 = ‘word1, word2, word3’; $result = “”; $var2_list = explode(‘,’, $Var2); foreach (explode(‘,’, $Var1) as $w1) { foreach ($var2_list as $w2) { $result .= trim($w1) . trim($w2). ‘, ‘; } } $result = trim($result, ‘, ‘); print_r($result);
Does exactly the right thing, but with no array, I’d like to adapt it to array. Such as this one of array adaptation in this response prior to this.
Regex is too much complex for me, I’ve read that thing several times, it’s confusing for me, I’ve seen it work in front of me this combination in the schematic that I need with strings echoing the screen with the output, I just need to adapt this to arrays for Use the values for query. It has to be array. I’m using this in my project with $disallowcombination = array(‘words1′,’word2′,’MANUALLY!’); and my filter picks up few combinations, that’s what I can do in my hand.
$Var1 = array('my1', 'my2', 'my3'); $Var2 = array('word1', 'word2', 'word3'); $result = ""; foreach (explode(',', $Var1_list) as $key => $w1) { foreach (explode(',', $Var2_list) as $key => $w2) { $result .= trim($w1) . trim($w2). ', '; } } $result = trim($result, ', '); $search = $result; // HERE $disallowcombination = array('words1','word2','MANUALLY!'); $replace = array('disallow combination','disallow combination','disallow combination'); $mytextfield = str_replace($search,$replace,$mytextfield);
Sorry for a lot of mistakes before, post dirty, but before you could edit, we had an hour to correct, now with this change is not allowed ??
I removed your post before last, the “dirty” one. Yes, regexp are confusing, I don’t blame you.
When you used explode() in your first example, you were creating arrays, so in effect your first example IS using arrays!
Let’s see what’s wrong when you start with arrays in your second example. $Var1_list and $Var2_list are undefined. You use foreach with arrays anyway, which is what $Var1 and $Var2 are. Remove the entire explode() functions and replace with the array references. We do not need the $key => $w1 construct because we have no need for $key values. That form is useful for some associative arrays that have named keys. You have simple indexed array, so you can just do foreach ( $Var1 as $w1 ) and foreach ( $Var2 as $w2 ).
If you are careful in creating arrays to ensure there are no leading or trailing spaces, you do not need trim() at all. Still, it doesn’t hurt to be safe. Do look at using array_map() so you can remove the trim() calls from the loop. Your goal is to have an array of forbidden terms to use in str_replace(), so you should build an array instead of concatenating the various $w1 . $w2 together. Before starting the foreach loops, declare an empty array:
$result = array();
Inside the loop, add each combined term to the array:
$result[] = $w1 . $w2;
Then after the loops finish up with
$mytextfield = str_replace( $result, '[redacted]', $mytextfield );
Any string in the $result array that matches something in $mytextfield will be replaced with “[redacted]”. Naturally, [redacted] can be what ever else you want.
Hi BCWorkZ ??
That was what I got between my attempts and it did not break the plugin, but I still did not work:$Var1 = array('my1', 'my2', 'my3'); $Var2 = array('word1', 'word2', 'word3'); $result = array(); $var2_list = explode(',', $Var2); foreach (explode(',', $Var1) as $w1) { foreach ($var2_list as $w2) { $result[] = $w1 . $w2; $result = trim($w1) . trim($w2); } } //$result = trim($result); $result = array_map( $w1, $w2); print_r($result); $mytextfield = str_replace( $result, '[Hahaha not that]', $mytextfield );
I’ve never used a string in place of a variable here …ace ($result, ‘[redacted]’, $myt… I’m going to use it a lot, I’ve learned now ssrsrr
You are still trying to explode an array! Not only does it not work, it’s not necessary. Reference your arrays directly in foreach statements without exploding, that’s what foreach is meant for.
If you want to use array_map(), you need to call it correctly with the function to be applied to each array element (‘trim’) as the first argument. See https://php.net/manual/en/function.array-map.php
If you apply array_map() to the original input arrays, there is no need to use trim() anywhere else. It’s only desirable if there’s a chance of improper array elements being defined, for example:
array('my1 ',' my2',' my3 ',)
(extra spaces inside of quotes) If you are sure they will all be correct, there is no need for trim() at all. Another advantage of using arrays instead of comma delimited strings.
- The topic ‘A list of denied words, with a condition only if glued with symbols’ is closed to new replies.