preg_match and reg expression using alphanumeric, commas, periods, exclamations, etc - php

I am having one hell of a time coming up with a decent way make this if statement search a file for these codes. I set up the text file to read from as such:
myfile.txt
r)
0Y7
1a6
q.
#g
#(
#a
!P
T[
V}
0,
Here is a brief of what I got going.
$subject = file_get_contents(fvManager_Path . 'myfile.txt');
if ( preg_match('/^[a-zA-Z0-9,]+$/',$result['fmbushels_itemCode'], $subject) ) {
Basically I am trying to search the text file line by line to see if the whole string exists. They are case sensitive as well.
$result['fmbushels_itemCode'] is from a sql query and always returns a code like the above in the text.
I'd appreciate any help on this. If someone knows a better way of doing this or a different command, I'd be willing to give that a shot as well :)
edit:
private function _fvShareBushels() {
$subject = file_get_contents(fvManager_Path . 'myfile.txt');
if (count($vShareArray) > 0) {
$vCntMoves = count($vShareArray);
for ($vI = 0;$vI < $vRunMainLoop;$vI++) {
sell $result['fmbushels_itemCode']);
}
}
}
This is a snippet of a big code. I had to rip most out because of post limitation. The area I could be working with is:
if (count($vShareArray) > 0) {
If I could make this something like:
if (count($vShareArray) > 0 && $result['fmbushels_itemCode'] **is not in** $subject) {

If you want to do line by line, use the file() function.
$f = file(fvManager_Path . 'myfile.txt');
foreach($f AS $line){
// $line is current line at file
}
I'm not to sure if you understand completely how preg_match works. The first parameter is the regular expression pattern, the second is what you want to match the pattern to, and the third is an array of matches. So for every valid pattern matched in the second parameter a new index on the array is created.
I'm not 100% on what you're trying to accomplish. Are you trying to see if the $result['fmbushels_itemCode'] exists in the file?
If the above is the correct case you simply just need to do something like:
$f = file('myfile.txt');
array_map('trim', $f);
if(in_array($result['fmbushels_itemCode'], $f)){
// success
}

Related

preg_match / word boundery only works to a limited extent

I would like to compare a text from its textarea with a come-separated keyword list and trigger a specific action if one or more hits are found.
Unfortunately, my script only works to a limited extent and triggers my function even if a character string, e.g. B. "ee" or "work" is identified.
Example for the keyword list:
"FREE Shipping,times,50% OFF,Backpack,Marketing,working,Owner,CEO"
Example of a text from the textarea:
"it seems that the function is not working correctly"
$messageTemp = explode(",",$GLOBALS['Message']);
foreach ($messageTemp as $valueK) {
if (preg_match("~\b(.+)$valueK\b~",$GLOBALS['Keyword']) !== 0 ) {
$GLOBALS['Key'] = 1;
};
};
I'm grateful for every hint
To put an arbitrary string inside reg exp you have to escape it first. Also, I added ignore case option. And removed the (.+) part.
foreach ($messageTemp as $valueK) {
$reg ="~\b".preg_quote($valueK, '/')."\b~i";
if (preg_match($reg, $keyword) !== 0) {
$GLOBALS['Key'] = 1;
}
}

PHPs strpos does not work as intended with double quoted string

I'm using the following code to return true or false if a string contains a substring in PHP 8.0.
<?php
$username = "mothertrucker"; // This username should NOT be allowed
$banlistFile = file_get_contents("banlist.txt"); //Contains the word "trucker" in it
$banlist = explode("\n", $banlistFile); // Splits $banlistFile into an array, split by line
if (contains($username, $banlist)) {
echo "Username is not allowed!";
} else {
echo "Username is allowed";
}
function contains($str, array $arr)
{
foreach($arr as $a) { // For each word in the banlist
if (stripos($str, $a) !== false) { // If I change $a to 'trucker', it works. "trucker" does not
return true;
}
}
return false;
}
?>
This is to detect if an inappropriate word is used when creating a username. So for example, if someone enters the username "mothertrucker", and "trucker" is included in the ban list, I want it to deny it.
Right now with this code, If I just type in the word "trucker" as a username, it is found and blocks it. Cool. However if there's more to the string than just "trucker", it doesn't detect it. So the username "mothertrucker" is allowed.
I discovered that if I explicitly type in 'trucker' instead of $a in the stripos function, it works perfectly. However, if I explicitly type in "trucker" (with double quotes), it stop working, and only blocks if that's the only thing the user entered.
So what I'm seeing, is it looks like the string $a that I'm passing it is being interpreted by PHP as a double quoted string, when in order for this to detect it properly, it needs to be a single quoted string. But as far as I can tell, I have no control over how php passes passing the variable.
Can I somehow convert it to a single quoted string? Perhaps the explode command I'm using in line 2 is causing it? Is there another way I can pull the data from a txt document and have it be interpreted as a single quote string? Hopefully I'm made sense with my explanation, but you can copy and paste the code and see it for yourself
Thanks for any help!
One potential problem would be any whitespace (which includes things like \r) could stop the word matching, so just trimming the word to compare with can tidy that up...
stripos($str, $a)
to
stripos($str, trim($a))
I do not know what your file actually contains so i dont know what the result of explode is.
Anyways my suggestion is (depending on the speed you want to perform this and also the length of the banlist file also your level of banning) to not explode the file and just look into it as a whole.
<?php
$username = "allow"; // This username should be allowed
$banlist = "trucker\nmotherfucker\n donot\ngoodword";
var_dump(contains($username, $banlist));
function contains($str, $arr)
{
if (stripos($arr, $str) !== false) return true;
else return false;
}
?>
Otherwise if you are going to allow say good which is an allowed word but since it is in the file with goodword it will not (using my example), you should not use stripos but instead use your example and use strcasecmp

How can I str_replace partially in PHP in a dynamic string with unknown key content

Working in WordPress (PHP). I want to set strings to the database like below. The string is translatable, so it could be in any language keeping the template codes. For the possible variations, I presented 4 strings here:
<?php
$string = '%%AUTHOR%% changed status to %%STATUS_new%%';
$string = '%%AUTHOR%% changed status to %%STATUS_oldie%%';
$string = '%%AUTHOR%% changed priority to %%PRIORITY_high%%';
$string = '%%AUTHOR%% changed priority to %%PRIORITY_low%%';
To make the string human-readable, for the %%AUTHOR%% part I can change the string like below:
<?php
$username = 'Illigil Liosous'; // could be any unicode string
$content = str_replace('%%AUTHOR%%', $username, $string);
But for status and priority, I have different substrings of different lengths.
Question is:
How can I make those dynamic substring be replaced on-the-fly so that they could be human-readable like:
Illigil Liosous changed status to Newendotobulous;
Illigil Liosous changed status to Oldisticabulous;
Illigil Liosous changed priority to Highlistacolisticosso;
Illigil Liosous changed priority to Lowisdulousiannosso;
Those unsoundable words are to let you understand the nature of a translatable string, that could be anything other than known words.
I think I can proceed with something like below:
<?php
if( strpos($_content, '%%STATUS_') !== false ) {
// proceed to push the translatable status string
}
if( strpos($_content, '%%PRIORITY_') !== false ) {
// proceed to push the translatable priority string
}
But how can I fill inside those conditionals efficiently?
Edit
I might not fully am clear with my question, hence updating the query. The issue is not related to array str_replace.
The issue is, the $string that I need to detect is not predefined. It would come like below:
if($status_changed) :
$string = "%%AUTHOR%% changed status to %%STATUS_{$status}%%";
else if($priority_changed) :
$string = "%%AUTHOR%% changed priority to %%PRIORITY_{$priority}%%";
endif;
Where they will be filled dynamically with values in the $status and $priority.
So when it comes to str_replace() I will actually use functions to get their appropriate labels:
<?php
function human_readable($codified_string, $user_id) {
if( strpos($_content, '%%STATUS_') !== false ) {
// need a way to get the $status extracted from the $codified_string
// $_got_status = ???? // I don't know how.
get_status_label($_got_status);
// the status label replacement would take place here, I don't know how.
}
if( strpos($_content, '%%PRIORITY_') !== false ) {
// need a way to get the $priority extracted from the $codified_string
// $_got_priority = ???? // I don't know how.
get_priority_label($_got_priority);
// the priority label replacement would take place here, I don't know how.
}
// Author name replacement takes place now
$username = get_the_username($user_id);
$human_readable_string = str_replace('%%AUTHOR%%', $username, $codified_string);
return $human_readable_string;
}
The function has some missing points where I currently am stuck. :(
Can you guide me a way out?
It sounds like you need to use RegEx for this solution.
You can use the following code snippet to get the effect you want to achieve:
preg_match('/%%PRIORITY_(.*?)%%/', $_content, $matches);
if (count($matches) > 0) {
$human_readable_string = str_replace("%%PRIORITY_{$matches[0]}%%", $replace, $codified_string);
}
Of course, the above code needs to be changed for STATUS and any other replacements that you require.
Explaining the RegEx code in short it:
/
The starting of any regular expression.
%%PRIORITY_
Is a literal match of those characters.
(
The opening of the match. This is going to be stored in the third parameter of the preg_match.
.
This matches any character that isn't a new line.
*?
This matches between 0 and infinite of the preceding character - in this case anything. The ? is a lazy match since the %% character will be matched by the ..
Check out the RegEx in action: https://regex101.com/r/qztLue/1

How to read article text file to string?

I want PHP read my article text file like this.
sample text file :
OMG! Where is my right hand.
I try to find my right hand but I can't see it.
please tell me how to find it.
Now I have this function code
function getContent($file_path,$path=''){
$file_path = $file_path;
if(file_exists('./'.$file_path)){
$f_read = fopen($path.$file_path,'r');
$rs = "";
while (!feof($f_read)) {
$rs .= fread($f_read, 8192);
}
fclose($f_read);
}
else{
echo $rs = "Not Connect File !";
}
return($rs);
}
after use that code :
OMG! Where is my right hand. I try to find my right hand but I can't see it. please tell me how to find it.
I want to use PHP function read first line to string1 and after first line is string2 like this
$string1 = "OMG! Where is my right hand."
$string2 = "I try to find my right hand but I can't see it.
please tell me how to find it."
Help me please :)
You can use below code for splitting your paragraph into string and also assign the character (like period, comma, etc.) from where do you want the paragraph to split.
preg_split('/[.?!]/',$mystring);
You may refer this link for more information: Explode a paragraph into sentences in PHP
or
http://php.net/manual/en/function.explode.php
You can use $string=#file_get_contents($path_name); $string=#explode("\n",$string); //for get each line.
In order to to read the first line into $string1 and the rest of the file into $string2 ...
Read the first line, as you do above, then call file_get_contents to get the rest.
Use the offset parameter to tell file_get_contents() to start reading after the end of the first line (pass the string length of the first line).
try using explode() function of PHP
function getContent($file_path,$path=''){
$file_path = $file_path;
if(file_exists('./'.$file_path)){
$f_read = fopen($path.$file_path,'r');
$rs = "";
while (!feof($f_read)) {
$rs .= fread($f_read, 8192);
$demo=explode('.', $rs);
}
fclose($f_read);
}
else{
echo $rs = "Not Connect File !";
}
return($demo);//result wiil be stored in $demo array like $demo[0], $demo[1]
}

Php preg_replace To change image width

I have some images in my database
http://***.com/2013/12/reign-death-midseason-finale-featured.jpg?w=500
http://***.com/reign-death-midseason-finale-featured.png?w=120
http://***.com/2013/12/finale-featured.jpg?w=50
http://***.com/2013/finale-featured.jpg?w=50&h=50
http://***.com/2013/12/reign-death-midseason-finale-featured.jpg
I want to change the images with w= to same width and ?w=50&h=50 also. all with W should come as w=600 and w=600&h=600. This is what I was trying str_replace but there is a problem with this that w= always change and in some cases there is height also and it also need to be changed, i have search net and found that it can be done with preg_replace don't know how.
EDIT
Answer needed is if(hight is null) result is case 1 w=600 and if h is not null case 2 w=600&h=600
please help
Sorry it took me a while, I was kinda busy
try the following
$pattern = '~(?<=\W(w|h)\=)(\d+?)(?=\D|$)~';
$replace = '600';
$subject = 'http://localhost/2013/finale-featured.jpg?w=50&h=50';
echo preg_replace($pattern,$replace,$subject);
try this with many variations of your URIs
PS: this kind of advanced string finding/replacing is the domain of regular expressions. If you find you need to do a lot of this, consider starting to learn about them, it's a whole language in itself. I used some assertions (lookahead (?<=) and lookbehind (?=)) for this particular solution
If you want to avoid using a regular expression you can parse the URL and query string into arrays, which would make evaluating the query string values much easier.
$url = 'http://***.com/2013/finale-featured.jpg?w=50&h=50';
$urlArray = parse_url($url);
parse_str($urlArray['query'], $queryStringArray);
if ( !isset($queryStringArray['h']) || $queryStringArray['h'] === null ) {
} else if ( ... ) {
} else if ( ... ) {
}

Categories