Checking if string contain values from an Array in PHP - php

I'm using PHP 7.1.12 and I trying to check if values from an array are present in a string to do that I'm doing this:
public function checkWords($word) {
$list = array('extinção','desativação','obrigatório');
foreach($list as $l) {
if (stripos($word, $l) !== false) {
return true;
}
}
return false;
}
and them I call the function
echo($this->checkWords('Físicaem desativação/extinção voluntária:23000.010237/2012-46''); //returns false
Now comes the weird part, if I go to the function and replace $l with let's say: 'hello'.
public function checkWords($word) {
$list = array('extinção','desativação','obrigatório');
foreach($list as $l) {
if (stripos($word, 'extinção') !== false) {
return true;
}
}
}
the function call will return true
echo($this->checkWords('Físicaem desativação/extinção voluntária:23000.010237/2012-46''); //returns true
Any ideas?

Since this question has a bounty, cannot flag it as duplicate, but the issue is with the function used in your case:
stripos($word, 'extinção')
As here is stated, stripos doesn't handle special characters very well and produces this non deterministic results. Use mb_strpos instead.

This seems to be an issue with the string encoding.
1) Check if using mb_stripos works. http://php.net/manual/en/function.mb-stripos.php
2) if (1) fails you may be serving the file with the wrong encoding. Check if your file charset matches your html charset meta header. https://developer.mozilla.org/en/docs/Web/HTML/Element/meta
<meta charset="utf-8">

When you have to deal with multi-byte characters at that time simple string function will not work. You have to use mb functions for it. There is list of mb functions which work with multi-byte characters, Check entire list here:
http://php.net/manual/en/ref.mbstring.php
Now for find position you can use mb_stripos function. Try below code:
function checkWords($word) {
$list = array('extinção','desativação','obrigatório');
foreach($list as $l) {
if (mb_stripos($word, $l) !== false) {
return true;
}
}
return false;
}
var_dump(checkWords('Físicaem desativação/extinção voluntária:23000.010237/2012-46')); // return true
echo PHP_EOL;
var_dump(checkWords('Físicaema dessativação/extsinção voluntária:23000.010237/2012-46')); //return false
DEMO

Related

Search string to see if it contains any string from an array PHP

I'm trying to check for if a string contains any blacklisted words from an array of string.
This is my current code - the focus is efficiency, string from list may be empty, the string we search in is never empty:
public static function includes_from_array($stringArray, $searchInMe, $offset=0) : bool {
// if not array just check if $stringArray exists in our $searchInMe string.
if(!is_array($stringArray)) {
return strpos($stringArray, $searchInMe) !== false;
}
foreach($stringArray as $query) {
if ($query === '') {
continue;
}
else if(strpos($searchInMe, strtolower($query), $offset) !== false) {
return true;
}
}
return false;
}
Is there a way to shorten the code quickly without harming efficiency or even improving it?
The code was written as it is currently to be able to retrieve the position if required (changing from true or false to pos returned in check), however it is not required in the shortened version.
Thanks!
As per my understanding you soul purpose is to check any bad word is present in sentence or URL and if present then return Boolean true.
Kindly try like this:
<?php
function includes_from_array($stringArray, $searchInMe){
if(is_array($stringArray)){
foreach($stringArray as $arr){
if(strpos($searchInMe,$arr) !== false){
return true;
}
}
}else{
if(strpos($searchInMe,$stringArray) !== false){
return true;
}
}
return false;
}
Sample Output : https://3v4l.org/3Pefn

use the functions without its parameters

I am using 2 regex functions here and I wanna make another function which returns false when the 2 regex are both false and if not, then true.
The problem here is when I wanna use the 2 regex functions in the third one, I have to give them parameters, which is not necessary I think, because the third function will only return a simple true or false. I get an undefined variable whenever I give parameters to the 2 regex functions in the 3rd one.
I tried using global variables which works but since its a bad practice I am looking for a better solution.
Code:
function regex1($input)
{
$regex= "/^[A-Za-z0-9 ]*$/";
if (!preg_match($regex, $input))
{
return false;
}
else
{
return true;
}
}
function regex2($input)
{
$regex= "/^[A-Za-z0-9 ]*$/";
if (!preg_match($regex, $input))
{
return false;
}
else
{
return true;
}
}
function checkBoth()
{
if (regex1($input) === false || regex2($input) === false)
{
return false;
}
else
{
return true;
}
}
EDIT:
The checkBoth function I am using in my other file like this together with the other 2 regex functions:
if (!regex1($input))
{
// show error at the same time
}
if (!regex2($input))
{
// show error at the same time
}
if(checkBoth())
{
// success
}
function regex2($input,$secondVar=false)
{....
Later in code in place where you need just add:
if($secondVar !== false){
// do whatever...
}
If you can't user "false" you can just empty string '' or any other value that will not appear there.

How to check (strpos(...)) all elements of an array efficiently?

I want to check all elements of an array and find out, whether at least one of them is prefixed by a given string:
public function validateStringByPrefix(string $string, $prefix)
{
$valid = false;
if (is_string($prefix)) {
if (strpos($string, $prefix) === 0) {
$valid = true;
}
} elseif (is_array($prefix)) {
foreach ($prefix as $partPrefix) {
if (strpos($string, $partPrefix) === 0) {
$valid = true;
break;
}
}
}
return $valid;
}
Is it possible / How to to achieve the same a more efficient way?
(It's a cheap method, but it's called a lot of times in my application, so even a minimal improvement might appreciably increase the application's performance.)
You can try next solution:
public function validateStringByPrefix(string $string, $prefix)
{
return (bool)array_filter((array)$prefix, function($prefix) use ($string) {
return strpos($string, $prefix)===0;
});
}
P.S. In case you have few large arrays (with prefixes), my solution is less efficient and you can combine our approaches like this:
public function validateStringByPrefix(string $string, $prefix)
{
if($string=='') {
return false;
}
foreach ((array)$prefix AS $subprefix) {
if (strpos($string, $subprefix)===0) {
return true;
}
}
return false;
}
There are many ways to rome....
//your array to test for
$array=[];
//set valid to false
$valid=false;
//setup prefixes array or not
$prefix='whatever';
//make array if you dont have one
!is_array($prefix) AND $prefix=array($prefix);
//prepare for use as REGEX
$prefix=implode('|',$prefix);
//do the work
array_walk($array,function($val,$key) use(&$valid,$prefix){
if (!$valid && preg_match("#^($prefix)#",$key)) {
$valid = true;
}
});
var_export($valid);
I used preg_match here, because $prefix can be an array, so the math would be: n+ strpos() calls vs. one preg_match() call
And after a single item matches, no more preg_match are called, just iteration to the end and out.

How to use preg_match to find html tags?

I'm trying to make this code work by seeing if the returned data contains any html tags with php but it doesn't seem to work..
function returnRegflow()
{
$data = "<html><body>";
return $data;
if(preg_match('html', $data))
{
echo 'error';
} else if(strpos($data, 'regflow') !== false){
echo $data;
}
}
echo returnRegflow();
Some issues:
Your function is exiting too early by the return before the if, so it never got to checking for tags;
preg_match expects the first argument to be regular expression, so it needs a delimiter at the start and the end (like /);
A function that tests the variables it has set itself is a bit useless, you should pass it the string that should be tested: that way it can be used for different strings;
using echo to output the result is not how functions work. Instead return the result, as a string or maybe as a boolean;
You seem to be looking for something like this:
function returnRegflow($data) {
if (preg_match('/<\w/', $data)) {
return 'contains tags';
}
if (strpos($data, 'regflow') !== false){
return $data;
}
return 'does not contain "regflow"';
}
echo returnRegflow("<html><body>");
The check for tags is quite rudimentary: it checks whether the text contains anything that looks like <a, where a can be any letter.

Regex for unicode character like \xe2\x98\xba

Can any one help me to write a preg_match rules to detect whether an input string is a unicode code character?
Here is the list of characters:
http://www.utf8-chartable.de/unicode-utf8-table.pl?start=9728&number=128&utf8=string-literal
I want to write a methods to detect whether the input string is a emoticons
function detectEmoticons($input) {
if (preg_match("/REGEX/", $input)) {
return TRUE;
} else {
return FALSE;
}
}
If the input is a string like "\xe2\x98\x80" or "\xe2\x98\x81"... etc (all the chacracter available in the list http://www.utf8-chartable.de/unicode-utf8-table.pl?start=9728&number=128&utf8=string-literal) then it should return
TRUE
Thanks in Advance,
Uttam
First, use the u modifier if you want your regular expression to work with unicode. Second, use a character class for all characters in the range [\x{2600}-\x{267F}] (i.e. U+2600 to U+267F). Now you can write your function as:
function detectEmoticons($input){
if(preg_match("/[\x{2600}-\x{267F}]/u", $input)){
return TRUE;
}
else{
return FALSE;
}
}
For match Unicode characters in a Regular expressions you must add the u Modifier
Example:
function detectEmoticons($input) {
if (preg_match("/REGEX/u", $input)) {
return TRUE;
} else {
return FALSE;
}
}
If you must retrieve one of the set you could pass the character range like
/[\x{START}-\x{END}]/u
Or check all characters with the mb_strpos function
Example
function detectEmoticons($input) {
$characters = array("\xe2", "\x98", ...);
foreach ($characters as $v) {
if (mb_strpos($input, $v) !== false)
return true;
}
return false;
}
You can find the documentation here:
http://ch1.php.net/manual/en/reference.pcre.pattern.modifiers.php
http://ch2.php.net/manual/en/function.mb-strpos.php
try this
preg_match("/\\[a-zA-Z0-9_-]{1,}\\[a-zA-Z0-9_-]{1,}\\[a-zA-Z0-9_-]{3}/", $input);
Use preg_replace to scape
preg_replace("/\\[a-zA-Z0-9_-]{1,}\\[a-zA-Z0-9_-]{1,}\\[a-zA-Z0-9_-]{3}/",'', $input);

Categories