I was working with a joomla site and found that quotes are creating problem with search. Single quotes are printed back with three backslashes with zero result, and double quotes are simply chopped off and string before the beginning of the double quote are getting matched while returning a result.
Any help, tips here ? Anyone has found and solved this before ?
Your problem occur because simple quote is noticed like an sql injection, if you change your 42 line in com_search
if (substr($searchword, 0, 1) == '"' && substr($searchword, -1) == '"')
with
if (substr($searchword, 0, 1) == '"' && substr($searchword, -1) == '"')||(substr($searchword, 0, 1) == "'" && substr($searchword, -1) == "'")
try it and tell me what it returns
Related
if you look at the pseudo code, i am trying to make a new string without certain elements.
thesentence = 'i need help!*'
bettersentence = ''.join([char for char in thesentence if char != '!' and '*'])
print(bettersentence)
comparing a character with two strings at the same time doesnt work. But im wondering wether there isnt any easy approach to this?
You can turn your string into a list and then use a for loop to see which characters in the string equal to * or ! and replace them with an empty string
def bettersentence(text):
text = list(text)
for i, character in enumerate(text):
if character == '*' or character == '!':
text[i] = ''
return ('').join(text)
I want in this part of my code, to cut out any non alphabetical symbol in the words I get from a read file.
I get that there is probably an empty string being tested on, that the error is happening,
but I can't figure out why after numerous different codes I tried.
Here's what I have now for it:
for i in given_file:
cut_it_out = True
while cut_it_out:
if len(i) == 0:
cut_it_out = False
else:
while (len(i) != 0) and cut_it_out:
if i.lower()[0].isalpha() and i.lower()[len(i) - 1].isalpha():
cut_it_out = False
if (not i.lower()[len(i) - 1].isalpha()):
i = i[:len(i) - 2]
if (not i.lower()[0].isalpha()):
i = i[1:]
Can anyone help me figure this out?
thanks.
Thanks for the interesting answers :), I want it to be even more precise, but there is an endless loop problem on I can't seem to get rid of.
Can anyone help me figure it out?
all_words = {} # New empty dictionary
for i in given_file:
if "--" in i:
split_point = i.index("--")
part_1 = i[:split_point]
part_2 = i[split_point + 2:]
combined_parts = [part_1, part_2]
given_file.insert(given_file.index(i)+2, str(part_1))
given_file.insert(given_file.index(part_1)+1, str(part_2))
#given_file.extend(combined_parts)
given_file.remove(i)
continue
elif len(i) > 0:
if i.find('0') == -1 and i.find('1') == -1 and i.find('2') == -1 and i.find('3') == -1 and i.find('4') == -1\
and i.find('5') == -1 and i.find('6') == -1 and i.find('7') == -1 and i.find('8') == -1 and i.find('9') == -1:
while not i[:1].isalpha():
i = i[1:]
while not i[-1:].isalpha():
i = i[:-1]
if i.lower() not in all_words:
all_words[i.lower()] = 1
elif i.lower() in all_words:
all_words[i.lower()] += 1
I think your problem is a consequence of an over complicated solution.
The error was pointed by #tobias_k. And anyway your code can be very inefficient.
Try to simplify, for example try: (I have not tested yet)
for i in given_file:
beg=0
end=len(i)-1
while beg<=end and not i[beg].isalpha():
beg=beg+1
while beg<=end and not i[end].isalpha():
end=end-1
res=""
if beg<=end:
res=i[beg:end]
There are a few problems with your code:
The immediate problem is that the second if can strip away the last character in a string of all non-alpha characters, and then the third if will produce an exception.
If the last character is non-alpha, you strip away the last two characters.
There is no need for those two nested loops, and you can use break instead of that boolean variable
if i.lower()[x] is non-alpha, so is i[x]; also, better use i[-1] for the last index
After fixing those issues, but keeping the general idea the same, your code becomes
while len(i) > 0:
if i[0].isalpha() and i[-1].isalpha():
break
if not i[-1].isalpha():
i = i[:-1]
elif not i[0].isalpha(): # actually, just 'else' would be enough, too
i = i[1:]
But that's still a bit hard to follow. I suggest using two loops for the two ends of the string:
while i and not i[:1].isalpha():
i = i[1:]
while i and not i[-1:].isalpha():
i = i[:-1]
Or you could just use a regular expression, somethink like this:
i = re.sub(r"^[^a-zA-Z]+|[^a-zA-Z]+$", "", i)
This reads: Replace all (+) characters that are not ([^...]) in the group a-zA-Z that are directly after the start of the string (^) or (|) before the string's end ($) with "".
Is there a simple function f such that
f(' hello, world! ' ) == 'hello, world!'
I can strip out the spaces (or any character for that matter) using regexes, but this seems like applying a hammer to the problem. I'd just like to know if there is something simple which I've missed.
No need to use a hammer, simply use strtrim.
From the documentation:
S = strtrim(str) returns a copy of string str with all leading and
trailing white-space characters removed. A white-space character is
one for which the isspace function returns logical 1 (true).
To remove the spaces on the side of the string, use the strtrim command.
Since Matlab version 2016b, you can use the built-in strip() function.
For those who do not have the newer matlab version, here is my self-defined function, which also does not have the limitation of exact one char when strCharacter is passed in as strip()
function result = trim(s,varargin)
% Merge multiple spaces to single space in the middle
% remove trailing/leading spaces
% trim(s [, how [,chars]])
% s: a string
% how: a num 1=left only;
% 2=right only;
% 3=left and right;
% 4 (default)=left and right and merge middle
% chars: if not given (default), space
% if given, remove consecutive character instead
%
if nargin == 1
how = 4;
chars = ' ';
elseif nargin == 2
how = varargin{1};
chars = ' ';
elseif nargin == 3
how = varargin{1};
chars = varargin{2};
end % end if nargin
if strcmp(chars,' '), chars='\s'; end
if how==1
expression = sprintf('^(%s)+',chars);
elseif how==2
expression = sprintf('(%s)+$',chars);
elseif how==3
expression = sprintf('^(%s)+|(%s)+$',chars,chars);
elseif how==4
expression = sprintf('(?<=[(%s)])(%s)*|^(%s)+|(%s)+$',chars,chars,chars,chars);
end % end if how
result = regexprep(s, expression, '');
end
I use the following code in PHP to validate if a password includes invalid characters
function validate_password($str) {
if(strlen($str) < 5 || strlen($str) > 30) {
return false;
} else {
return preg_match('/^[-._A-Za-z0-9]*$/',$str);
}
}
I'm trying to do the same in Lua but not sure the best way to check if a string contains only certain characters. My valid characters are alphanumeric with the addition of "-" and "."
local function validate_password(str)
if string.len(str) < 5 or string.len(str) > 30 then
return false
else
if(str:match("%W")) then
-- this isn't right. need to check if alpha numeric or - or .
return false
else
return true
end
end
end
Appreciate any help!
return #str >= 5 and #str <= 30 and str:find('^[%-%.%w_]+$') ~= nil
Note 1: as mentioned in the comments, ~= nil part can be removed as the position returned by find will be evaluated as true (when it's not nil) in logical operators. However, since it's a number, it will fail when used in validate_password(str) == true comparisons (since 1 == true is not true). An alternative is to use not str:find('[^...]') as suggested by Egor.
Note 2: as mentioned in the comments, - and . don't need to be escaped in this particular case. However, since - is a range operator inside square brackets [_-.] is not the same as [-._], as it will match nothing as the range of symbols between _ and . is empty. It's safe to escape - and . even inside square brackets.
I'm looking for a Visual FoxPro function which is similar to the PHP function is_numeric().
I have found this, but I could not use VARTYPE or TYPE because the variable is always a character string which contains digits only.
I found ISDIGIT() function, but the manual says that it only checks the first character.
Determines whether the leftmost character of the specified character
expression is a digit (0 through 9).
ISDIGIT(cExpression)
Parameters
cExpression
Specifies the character expression that ISDIGIT( ) tests. Any
characters after the first character in cExpression are ignored.
I would create my own function using the regular expression object VBScript.RegExp
FUNCTION isNumeric( tcValue )
LOCAL oRE
oRE = CreateObject("VBScript.RegExp")
oRE.Pattern = '^[0-9]+$'
RETURN oRE.test( tcValue )
ENDFUNC
? isNumeric( '123' )
But, is there any function provided by FoxPro for this purpose?
Am I just overlooking?
Also same for ISALHPA() which determines whether the leftmost character in a character expression is alphabetic. I want to check if the variable contain only alphabets.
You can create your own function like this.
FUNCTION IsAllDigits
LPARAMETERS tcSearched, tcOptionalSearch
* tcSearched = the string of characters to test.
* tcOptionalSearch = optional, additional characters to allow.
LOCAL lcSearch
m.lcSearch = "01234567989" + IIF(VARTYPE(m.tcOptionalSearch) = "C", m.tcOptionalSearch, "")
LOCAL lcRemaining
m.lcRemaining = CHRTRAN(m.tcSearched, m.lcSearch, "")
RETURN ( LEN(m.lcRemaining) = 0 )
ENDFUNC
FUNCTION ISNUMERIC
LPARAMETERS cVal
LOCAL llNumeric, lnLen, lcChr, lnDecs, lnVal
llNumeric = VARTYPE(cVal) = "N" && Donkey has sent a numeric value
lnDecs = 0
DO CASE
CASE llNumeric
CASE VARTYPE(cVal)<>"C" && Not a character
OTHERWISE
cVal = ALLTRIM(cVal) && Trim spaces
lnLen = LEN(cVal) && How many characters
llNumeric = .T. && Assume
i = 0
DO WHILE llNumeric AND i<lnLen
i = i+1
lcChr = SUBSTR(cVal,i,1) && Get next char
lnVal = VAL(lcChr)
DO CASE
CASE lcChr = "0" && Allowed
CASE lnVal>0 && 1 - 9 OK
CASE INLIST(lcChr, "-", "+") && Allowed but ONLY at the start
llNumeric = i = 1
CASE lcChr = "." && Decimal point but ONLY one
lnDecs = lnDecs+1
llNumeric = lnDecs = 1
OTHERWISE
llNumeric = .F.
ENDCASE
ENDDO
ENDCASE
RETURN llNumeric
ENDFUNC
This could work for ISDIGIT() or ISALPHA().
Function IsAllDigits(myValue)
lReturn = .t.
FOR i = 1 TO LEN(myvalue)
IF !ISDIGIT( SUBSTR(myValue, i, 1) )
lReturn = .f.
EXIT
ENDIF
ENDFOR
RETURN lReturn
ENDFUNC
How about a one liner?
Function IsNumeric
Lparameters pString
Return m.pString == Chrtran(m.pString, Chrtran(m.pString, "0123456789", ""), "")
EndFunc
You can any other valid characters to "0123456789" like "." or ","
There is more simple to test if a string is numeric or not :
If String="123" => val('String')>0
If String="AB123" => val('String')=0
That's all...
Using only Visual Fox Pro, you can do something like this:
FUNCTION is_numeric(var_name)
error_trigger=.f. &&** first initialization
&&** evaluate("any_char") will generate an error so I'm using TRY-CATCH-ENDTRY
TRY
EVALUATE(var_name)
CATCH &&** evaluate() generates an error then the string is character
WAIT WINDOW "character"
error_trigger=.t. &&** there was an error returned by evaluate()
EXIT && stop and jump after ENDTRY
ENDTRY
IF error_trigger=.f. &&** there was no error returned by evaluate() then the string was numeric
WAIT WINDOW "numeric"
ENDIF
ENDFUNC
Then call to the function:
is_numeric("333") will show: numeric
is_numeric("aaa") will show: character
is_numeric("333a333") will show: character
I hope it will help you