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);
Related
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
I have the following function to do a "exact match" of a pattern($searchPat)
in a sentence ($sourceStr)
function isUsed($sourceStr, $searchPat) {
if (strpos($sourceStr, $searchPat) !== false) {
return true;
} else {
return false;
}
}
However, this doesn't do an exact match. I changed the function as follows but this doesn't even execute.
function isUsed($sourceStr, $searchPat) {
if (preg_match("~\b[$sourceStr]\b~", $searchPat)) {
return true;
} else {
return false;
}
}
How could I do an exact match please?
The [] is a character class. That lists characters you want to allow, for example [aeiou] would allow a vowel. Your variables are also in the inverted order, pattern first, then string to match against. Try this:
function isUsed($sourceStr, $searchPat) {
if (preg_match("~\b$searchPat\b~", $sourceStr)) {
return true;
} else {
return false;
}
}
Additional notes, this is case sensitive, so Be won't match be. If the values you are passing in are going to have special characters the preg_quote function should be used, preg_quote($variable, '~'). You also may want to concatenate the variable so it is clear that that is a variable and not part of the regex. The $ in regex means the end of the string.
Try This.
function isUsed($sourceStr, $searchPat) {
if (preg_match("/\b".preg_quote($sourceStr)."\b/i", $searchPat)) {
return true;
} else {
return false;
}
}
Please try "preg_match" for matches.
$string = 'test';
if ( preg_match("~\btest\b~",$string) )
echo "matched";
else
echo "no match";
Or try like this
if(stripos($text,$word) !== false)
echo "no match";
else
echo "match";
You can try this one:
function isUsed($string_to_search, $source_String) {
if (preg_match("/$string_to_search/", $source_String)) {
return true;
} else {
return false;
}
}
You can change according to your need.
Case insensitive: preg_match("/$string_to_search/i", $source_String)
Boundry condition: preg_match("/\b$string_to_search\b/i", $source_String)
Special characters: if you have any special characters in your string for your safe side replace it with '\special_character'
I have been working on a requirement (in PHP), where I had stuck with the below.
I need to search for the numbers between 6800 to 6899. How can I search starts with the string ‘68’. So that I will get all the numbers in that range.
Now, I have an idea like:
if ($Model== 'Jazz6800i') return true;
if ($Model== 'Jazz6801i') return true;
......
if ($Model== 'Jazz6899i') return true;
Do we have any alternate in PHP instead of writing hell lot of code, to search starting with 68?
Use regexp:
function isInCorrectRange($item)
{
return preg_match('/Jazz68[0-9][0-9]i/', $item);
}
$result = isInCorrectRange('Jazz6801i');
You can use str_replace, in_array, range for this.
$model = str_replace(array('Jazz', 'i'), '', $model);
if(in_array($model, range(6800, 6899))) {
return true;
}
Something like that :
for ($i=0;$i<=99;$i++)
{
If ($Model == 'Jazz68'.$i.'i')
{
echo "Found ! for Jazz68".$i."i";
break;
}
}
My pattern matching expression is like this i want to check and return error message.
public function checkthe($str)
{
if(preg_match('/^\(?[+]?[0-9]{3,4}[-. ]?[0-9]{8,10}$/', $str)==TRUE)
{
return true;
}
else
{
$this->form_validation->set_message('checkthe', 'The %s should be in format XXX-XXXXXXXX');
return false;
}
}
I need display error if other than + / and 0-9 in the string
If that is indeed the case your regex code should be:
public function checkthe($str) {
return preg_match('~^[+/0-9]+$~', $str);
}
Though it is always better to include your sample inputs.
I have this block of code
public function onlyLetters($string) {
if(preg_match("/[a-zA-Z]/", $string)) {
return true;
} else {
return false;
}
}
but it always returns false, what would the reason be?
Your string most likely doesn't contain what you expect, and doesn't have a single upper or lower case a-z letter.
OR - you aren't handling the return value correctly
If you check these assumptions, you'll probably solve it yourself :)