How to escape % symbol in Where statement - laravel-5

How do i escape the '%' in the sense that i want to remove any statements with the '%' symbol in it.
The following code is an example ->where('dst', 'not like', '%#%') in Laravel Eloquent, where the strings matching anything with a '#' is removed.
I want to do the same for '%' where any strings with % in the string like %4214231%423 is removed from the query result.

All you should do is to add '\' before percent sign.
->where('dst', 'not like', '%\%%')

Related

escape single quote or aphosthrope in filter datastage

How to escape single qupte or apostrophe in filter stage-DataStage?
I've tried escape the symbol with backslash in any order of the regex below but it returns same result
nama_lgkp_ibu like '^((?=.*[-!$##^&*()_+|~=`{}\[\]\%\':";<>?,\\.\/])).{1,}$'
return:
Filter_83: Parse error: Expected 'is' statement ('is true', 'is false') or boolean operator (and, or) got: ":".
Is there any way to escape the single qupte character in datastage-filter?

Special character Oracle REGEXP

I need to allow only set of characters i.e.,
a to z A to Z 0 to 9 . !##$% *()_=+|[]{}"'';:?/.,-
but When I add dash(-) character to below query it is not working please help me at earliest.
SELECT :p_string FROM dual
WHERE NOT REGEXP_LIKE (translate(:p_string,chr(10)||chr(11)||chr(13), ' '),'[^]^A-Z^a-z^0-9^[^.^{^}^!^#^#^$^%^*^(^)^_^=^+^|^\^{^}^"^''^;^:^?^/^,^-^ ]' );
[.-.] will work fine on this query .
The extra ^ symbols inside the bracket expression in your pattern are not, as I think you expect, negations; only the first ^ inside the brackets does that.
The main issue that is causing, apart from allowing that actual circumflex symbol to be matched when you didn't seem to want it, is that you end up with ^-^ being treated as a range.
To include a literal - it has to be the first or last thing in the brackets; from the docs:
To specify a right bracket (]) in the bracket expression, place it first in the list (after the initial circumflex (^), if any).
To specify a hyphen in the bracket expression, place it first in the list (after the initial circumflex (^), if any), last in the list, or as an ending range point in a range expression.
So as you need to do both, make the hyphen last; you can change your pattern to:
'[^]A-Za-z0-9[.{}!##$%*()_=+|\{}"'';:?/, -]'
You could also skip the tralsnate step by including those special characters in the pattern too:
'[^]A-Za-z0-9[.{}!##$%*()_=+|\{}"'';:?/, '||chr(10)||chr(11)||chr(13)||'-]'
Looks like you need to permit only (7-bit) ASCII characters with exception of ~ and ^
In this case I would try it like this:
WHERE CONVERT(p_string, 'US7ASCII') = p_string
AND NOT REGEXP_LIKE(p_string, '~|\^')
Instead of CONVERT(p_string, 'US7ASCII') = p_string you can also use ASCIISTR(REPLACE(p_string, '\', '/')) = REPLACE(p_string, '\', '/')

Oracle/ PLSQL condition like 'string' || '%'

Why this
SELECT * FROM STUDENT
WHERE FULLNAME LIKE 'Nguyen' || '%'
as the same
SELECT * FROM STUDENT
WHERE FULLNAME LIKE 'Nguyen%'
How does the first one work?
|| is concatenation operator. Oracle will first perform concatenation and then will use LIKE to match the pattern. Hence operationally it will be same as the second one.
However you should use the second one as it will be more efficient in performance and easy to read.
First one has extra overhead to append two strings before using LIKE to match the pattern.
the first one catenate 'Nguyen' and '%' through the pipes '||' in first place.
Because you don't have any space like 'Nguyen ' or ' %', it's the same as 'Nguyen%'.
There is no difference between these two:
Here the double pipe(||) is just a concatenation of expression.
Before db evaluate against like parameter, it concatenates
Hence both of those are same.
Both are same, as || operator concats 'Nguyen' and '%', it will be more helpful if you want to concat parameterize variable like below
SELECT * FROM STUDENT
WHERE FULLNAME LIKE :name || '%'

preg replace remove the same repeated non-word character

I want to remove the same repeated non-word character.
My code looks like this:
<?php
$name = 'Malines - Blockbuster (prod. Malines) ***** (((((( &%^$';
echo preg_replace('/[^\pL\pN\s]{2,}/u', '', $name);
?>
Malines - Blockbuster (prod. Malines) ***** (((((( &%^$ should be Malines - Blockbuster (prod. Malines) &%^$
Only repeated non-word character should be removed. Could you help me?
You must use back-reference to say that the second character is the same as the one before :
/([^\pL\pN\s])\1+/u
Using the same idea than Arlaud Agbe Pierre (that is the way to do it), the pattern can be shorten using the Xan character class. \p{Xan} is the union of \p{L} and \p{N}. \P{Xan} (with an uppercase "p") is the negation of this class (i.e. all that is not a letter or a number).
$str = 'Malines - Blockbuster (prod. Malines) ààà ***** (((((( &%^$';
echo preg_replace('~(\P{Xan})\1+~u', '$1', $str);
Note: in this pattern, consecutive white characters are removed too.
an other way:
echo preg_replace('~(\P{Xan})\K\1+~u', '', $str);
where \K resets the begining of the match from the match result. (note that you can however define capturing groups before that you can use after)

How to escape special characters in sphinxQL fulltext search?

in the sphinx changelog it says for 0.9.8:
"added query escaping support to query language, and EscapeString() API call"
can i assume, that there should be support for escaping special sphinx characters (#, !,
-, ...) for sphinxQL, too? if so, maybe someone could point me to an example on this. i'm
unable to find anything about it in the documentation or elsewhere on the net.
how do you do fulltext search (using spinxQL), if the search-phrase contains one of the special characters? i don't like the idea very much to "mask" them during indexing.
thanks!
The PHP version of the sphinxapi escape function did not work for me in tests. Also, it provides no protection against SQL-injection sorts of characters (e.g. single quote).
I needed this function:
function EscapeSphinxQL ( $string )
{
$from = array ( '\\', '(',')','|','-','!','#','~','"','&', '/', '^', '$', '=', "'", "\x00", "\n", "\r", "\x1a" );
$to = array ( '\\\\', '\\\(','\\\)','\\\|','\\\-','\\\!','\\\#','\\\~','\\\"', '\\\&', '\\\/', '\\\^', '\\\$', '\\\=', "\\'", "\\x00", "\\n", "\\r", "\\x1a" );
return str_replace ( $from, $to, $string );
}
Note the extra backslashes on the Sphinx-specific characters. I think what happens is that they put your whole query through an SQL parser, which removes escape backslashes 'extraneous' for SQL purposes (i.e. '\&' -> '&'). Then, it puts the MATCH clause through the fulltext parser, and suddenly '&' is a special character. So, you need the extra backslashes in the beginning.
There are corresponding functions EscapeString in each API ( php/python/java/ruby ) but to make escaping work with SphinxQL you have to write something similar in your application as SphinxQL hasn't such function.
The function itself is onliner
def EscapeString(self, string):
return re.sub(r"([=\(\)|\-!#~\"&/\\\^\$\=])", r"\\\1", string)
you could easy translate it to code of your application.

Resources