Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
so I am trying to check if a string contains a specific set of words, and set a variable if they do. I have the code below, first it was working great except that it would not work if one of the key words was the very first word, so I looked at the php manual and found out about === and implemented that, but now it sets the variable to one every time even if none of the words are found!
So basically it reads a text file to an array, the text file contains the key words, then it checks the string to see if any of those key words are present in the sting. If none of the key words are found then wc would equal 0 and so would inc. If it finds any then wc is incremented every time a word is found, and if it is greater than 0 it will set inc to 1 to flag that key words were included.
Hopefully that all makes sense....
Here is my code:
$inc = 0;
$list = file("filter.txt", FILE_IGNORE_NEW_LINES);
$cnt = count($list);
$wc = 0;
for ($i=0; $i<$cnt; $i++)
{
if (strpos($string,$list[$i]) === false)
{
$wc ++;
}
if ($wc > 0)
{
$inc = 1;
}
}
It doesn't work because you are increasing wc if the string is not found.
You have to replace === with !==.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
<?php
$efFect=0;
$find='is';
$find_len=strlen($find);
$string22 ='this is space is function';
while ($str_position =strpos($string22,$find,$effect)){
echo $find.'the postion in '. $str_position.'<br>';
$efFect = $str_position + $find_len ;
}
?>
0 is a falsy value. You should make your check more reliable by checking if strpos actually returns false.
<?php
$effect=0; // not $efFect as variables names are case sensitive in PHP
$find='is';
$find_len=strlen($find);
$string22 ='this is space is function';
while (($str_position = strpos($string22, $find, $effect)) !== False){
echo $find.'the postion in '. $str_position.'<br>';
$effect = $str_position + $find_len ;
}
?>
From the documentation:
Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.
Returns FALSE if the needle was not found.
Someone mentioned that your code creates an infinite loop but this is not the case. strpos accepts an offset as the third parameter which you have correctly used. On each iteration the offset is incremented such that the new search will begin at the position where the last found result string ends, hence avoiding the infinite loop.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Question First: Am i going about this the wrong way?
I had trouble braking from my for each with a conditional if inside. I wrote this just to save CPU Time yet i could not brake from my foreach.
I am trying to comply with with Active Directory strict password enforcement and i don't believe in having maximum length passwords. As i have found some websites have Maximum length requirements.
foreach(count_chars($new_password, 1) as $key => $value){//Strength Test Results can be derived from $value
if(!ctype_upper(chr($key))){$check_upper=1;}//if Upper-case
if(!ctype_lower(chr($key))){$check_lower=1;}//if Lower-case
if(!ctype_digit(chr($key))){$check_digit=1;}//if Numeric
if(!ctype_punct(chr($key))){$check_punct=1;}//if Symbol
if($check_upper + $check_lower + $check_digit + $check_punct>= 3){
break;
}//Save us from checking the entire string
}
You use the wrong keyword! It should mean break
foreach(count_chars($new_password, 1) as $key => $value){//Strength Test Results can be derived from $value
if(!ctype_upper(chr($key))){$check_upper=1;}//if Upper-case
if(!ctype_lower(chr($key))){$check_lower=1;}//if Lower-case
if(!ctype_digit(chr($key))){$check_digit=1;}//if Numeric
if(!ctype_punct(chr($key))){$check_punct=1;}//if Symbol
if($check_upper + $check_lower + $check_digit + $check_punct>= 3){
break;
}//Save us from checking the entire string
}
Well that's simple: break
Brake is what a car does.
http://php.net/manual/en/control-structures.break.php
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm looking at the preg_match instructions, but struggling to gather together what I need to only allow a certain few characters to be used, and how to implement that into the pattern format
if(preg_match('[0-9.$M]', $_POST['budget']) === true) {
$errors = 'no illegal';
} else {
//Illegal character in budget
$errors = 'illegal';
}
But I've realized that for one that doesn't work for this combination
0123456789$M. as well as that it will not function as I want it to even if I get it to work. as I need to check the $_POST['budget'] and be sure it's in the correct format before moving on with it. It can ONLY contain the characters I've put forward, anything else will create a pretty big mess.
So far, I've had javascript change any entries, but if that is disabled then they can put in whatever they please.
So I need is pseudo
if (preg_match($allowed_characters, $_POST['budget'] === true && DOESNT CONTAIN ANY OTHER CHARACTERS) {
//No illegal characters
} else {
//Illegal characters!!
}
1) preg_match returns INT or BOOL false.
2) Your pattern is not valid for preg_match
should be something like:
if(preg_match('/[^0-9\.\$M]+/', '0123456789$M.')) {
$errors = 'illegal';
} else {
$errors = 'no illegal';
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My client is using PHP version 5.2.0, and I have some code that works perfectly, but uses 5.4.7 at the moment. When my code is run on their server, I receive a parse error. I've searched online for an alternative way to write this code, but I can't find anything. If anyone has any tips that would be a great help.
**$count = mysqli_query($con, "SELECT COUNT(*) FROM menuitem")->fetch_row()[0];** //I want to make sure the previous and next button are only displayed exactly to the number of items
//in my database. I have to count those items, and turn that mysqli_query object into data. fetch_row[0] stores them
//in an array, starting at 0, turning it into an integer I can use to count below.
$prev = $start - 3;
if ($prev >= 0) {
echo '<td>Previous Items</td>';// Set your $start - 3 since our offset is 3, and pass that to a variable.
} //if $start - 3 is still greater than 0, then display the previous button. Logically, that is page 2.
$next = $start + 3;
if ($next < $count) {
echo '<td>Next Items</td>';// Since i used fetch_row[0] I now know how many rows I have. If $start + 3 is less than the $count query
} // than you will have a next button. If that number exceeds the number of rows I have, then no next button.
echo "</tr>";
echo "</table>";
In the code above, the ...->fetch_row()[0]; is the section that is bringing back the error. PHP 5.2.0 does not like it.
Prior to PHP 5.4, you could not dereference arrays returned from function calls. Simply do:
$count = mysqli_query(...)->fetch_row();
$count = $count[0];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
How do I skip a loop and go to the next one? Lets say my query in a loop returns result 'Nothing', I want to skip updating the base and go to the next loop. If I have 7 loops and my 5th loop returns 'Nothing' from my base, skip it don't do nothing beyond that point and just go to the next, 6th loop.
I have a for loop like so:
for((int) $i = 0; $i < $number_of_updates; $i++)
{
$result = mysql_query($query);
list($name) = mysql_fetch_row($result);
if ($name = 'Nothing')
{
go to next loop;
}
updating my database and stuff;
}
EDIT: I have a $_SESSION variable at the end that stores all of my loops and displays them. I also don't want results with 'Nothing' to be stored there.
continue will end the current iteration and start the next one:
if ($name == 'Nothing')
// -------^^ Make sure you're using double (==).
{
continue;
}