I have read all topics regarding strpos() issues but I cant find one that will fix mine.
So here is the code.
foreach($titles_array as $k=>$v) {
if(strpos($subject,$v) !== false) {
$i++;
$asd[$titles['id']] = $i;
}
}
The script works good and I get the results I'm looking for but this error comes up :
Warning: strpos() [function.strpos]: Empty delimiter
I read that it might be an empty string or value in array but I've checked them both and I didnt found any empty string/value.
I appreciate any help.
All the best !
The "empty delimiter" warning occurs when the second parameter to strpos is empty. You definitely have an empty value in $titles_array.
I have reproduced the warning here: http://3v4l.org/RnU3q
Try print_r($titles_array) right before your foreach loop.
Related
We have an old version of CakePHP that we've moved to a new server running PHP 5.6 and we've started to recieve this error when adding a product to the basket:
Warning (2): Illegal string offset 'AddBasketData'
[APP/controllers/personalisation_controller.php, line 848]
Here is line 848 within the file:
if (is_array($this->data['AddBasketData'])) {
$personalisation_data['Personalise'] = $this->data['AddBasketData'];
}else {
$personalisation_data['Personalise'] = array();
}
Could anyone shed any light on this, I think it's down to the specific PHP version we're running now but any help would be great.
Thanks
Transforming my comments into an answer :
The problem here seems to be that $this->data is a string and not an array.
You should test this first, then check if the offset AddBasketData exists, and finally if the offset AddBasketData is an array :
if (is_array($this->data) && isset($this->data['AddBasketData']) && is_array($this->data['AddBasketData'])) {
$personalisation_data['Personalise'] = $this->data['AddBasketData'];
} else {
$personalisation_data['Personalise'] = array();
}
Of course, that will only correct the symptoms (which is the raised warning), you might have some code debugging to do to find out why $this->data is a string instead of an array.
As noted by #roberto06 in the comments to your question, the reason you're getting the error is because you're trying to treat a string value as an array.
The reason for that specific error message is because you can use the array-offset notation to fetch a single character from the string. Just like you'd do in C's string arrays. But this only support numerical indices, and not a string index as shown in the code you posted.
Now, the easy way to stop the error from occurring is to simply test the type of the data, and whether or not the given index actually exists.Like so:
if (is_array ($this->data) && !empty ($this->data['AddBasketData'])) {
$personalisation_data['Personalise'] = $this->data['AddBasketData'];
} else {
$personalisation_data['Personalise'] = array();
}
However, seeing as you're not suddenly getting this error after update hints towards something else being the issue. Especially since the code you posted expects the value stored in the Personalise index to be an array. I'd trace where the $this->data member gets set, and changed, to see if you can find the underlying reason for why this apparent change in behavior. This might be the side-effect of a more nefarious subtle bug, after all.
I am a little rusty in PHP after several years without programming with this language.
I am fetching data from an array in mySQL using PHP.
My problem is that at some point I fetch data from a table, using this:
$myArray = mysql_fetch_array( $data );
but at some point, the data base may be empty, so $myArray will be null or contain no elements.
Then I have to check if a particular string that is generated on-the-fly is on $myArray. If it is, another string must be generated and verified if it already exists on $myArray.
$myString = generateString();
if (in_array($myString, $myArray)) {
$myString = generateString();
}
this code gives me this error:
Warning: in_array() [function.in-array]: Wrong datatype for second argument
To prevent in_array from running when $myArray is empty I did this:
if (count($myArray) > 0) {
if (in_array($myString, $myArray)) {
$myString = generateString();
}
}
but count is giving me 1 when the array is empty (?)...
How do I solve that?
just another question: $myString is being modified inside the IF that is already testing it. Is this possible in PHP?
Well, if your query failed, you'll have... wait for it... no array!
So that in_array call would give you a warning about $myArray not being an array (but false).
You need to check your query for errors, and better yet:
Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
I'm getting this error:
Warning: stristr() expects parameter 1 to be string, array given in
/nfs/c09/h03/mnt/12345/domains/mydomainname.com/html/inyoni/fabrics.php
on line 385
from this code:
foreach ($_POST as $field => $value) {
if ($value && (stristr($value,"Content-Type: "))){
header("Location: error.php");
exit;
}
}
and I have no idea why. In fact, I don't see the error, but my client does. My understanding is that this happens when an image file was included in the form (however, this error didn't appear before, only now).
The site is hosted on Mediatemple and a PHP notification had been sent out to customers about the time this started about PHP5 being used across the board on their servers--is this a PHP5 conflict or something?
A bit puzzled--any help appreciated!
The error is pretty clear. It's saying that the first parameter of stristr() has to be a string. However, you're supplying an array instead which is why the code produces an error. To fix it, simply make sure you're just checking for string values.
if (is_string($value))
{
if (stristr(...))
}
Well the $value contains an array, not a string. This can happen when you have a form something like this:
<input type="text" name="form[foo]"/>
<input type="text" name="form[bar]"/>
The $_POST['form'] will hold an array of those two variables. Check if something like this occurs.
It can also happen with file uploads, multiple checkboxes, etc.
As Kemal Fadillah suggested, make a simple type check of the $value variable.
Do a var_dump($value) in the loop. If any of the forms contains an array, such as an uploaded file http://www.php.net/manual/en/features.file-upload.post-method.php or a bunch of checkboxes, then $value would be an array, instead of a string.
My latest issue involves trying to find "http://" in a variable. This variable contains the contents of a comments section on a clients website. I have seen all kinds of answers but none of them seem to work. I looked at a few other posts on here and I have yet to get the best answer. Here is what I have so far:
if(strpos($comments, 'http://') == true) {
// Does stuff here
}
I noticed other people use preg_match and some said to do it in an array. I am getting confused, too many options. Just kidding. I would like some clarification though and any advice would be greatly appreciated.
You'll need to say:
if(strpos($comments, 'http://') !== false) {
...since it can return 0 (which is falsey) if http:// is at the beginning of the string.
NOTE: This will only find the first occurrence of http:// in the string.
Take a close look at the reference: http://php.net/manual/en/function.strpos.php
You need to change code like that:
if(strpos($comments, 'http://') === false) {
//no link
}
because strpos return integer which is position your string.
Example:
full string: "http://stackoverflow.com hello"
you finding: "http"
Naturally it return 0.
But
full string: "ahttp://stackoverflow.com"
you finding: "http"
it return 1.
So you must use === operator to check is really 'boolean false'.
If you try to check with == operator, you maybe get fail because it get 0 as false.
more detail: http://php.net/strpos
I found this was a better match: (recommended by phpstorm ide)
if(str_contains($e, '1062 Duplicate entry')) {
}
Does anyone know any other way that I can use (beside of #strpos()) to ignored the error message that display from strpos() (Warning: strpos() [function.strpos]: Offset not contained in string in ....)
Instead of doing something like this:
$pos = strpos($haystack, $needle, $offset);
do something like
if($offset < strlen($haystack))
$pos = strpos($haystack, $needle, $offset);
else
$pos = false; // Mimics the actual output of strpos
Hope this helps!
Sounds like your third argument is not valid. From the docs
The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack.
So, how about fixing your code instead of ignoring errors?
To answer the question, you can write your own error handler and catch that specific warning and suppress it... but I agree with the others; it's better that you just go and fix your code.