Anyway that I can ignored the error message from strpos()? - php

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.

Related

Warning: strpos() [function.strpos]: Empty delimiter in foreach statement

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.

PHP - Find certain strings in a variable

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')) {
}

finding a string in php

I want to find a string like 'Jobs' in a title. Suppose i have 10 rows in a file.
i.e
Jobs for Accountant.
Featured Jobs for public Services.
Website Development Jobs in Delhi.
.
.
.
.
How to find jobs keyword in these type of titles?
You can use strpos
Just check if (strpos($string,$search)===0) and you are good
You can use strpos.
if(strpos($string, 'Jobs') !== false)
Make sure you do it exactly like that, since if(strpos($string, 'Jobs') would return 0 if the string started with 'Jobs', which would be casted to the boolean value of 0: false.
Use substr() to achieve this. Here is the manual php.net/manual/en/function.substr.php
You can use strpos. Look here for documentation.
it reurns the first occurence of what you are seraching in a string.
you can do:
$pos = strpos($string, "jobs")
and it returns the offset where it found "jobs".
Then you can $pos to look for further occurences passing it to the function
$pos2 = strpos($string, "jobs", $pos+1)
if you want to check if it doesn't find a string you must use === because if it finds the string in the first position it returns 0 (that if you just check with == resolves to false)
Use regular expressions.
Here is a tutorial: http://www.phpf1.com/tutorial/php-regular-expression.html
Or, just google php regular expressions for all the info you need.

Searching partial strings PHP

How can you search a partial string when typing (not to use MySQL) like the LIKE function in MySQL but using PHP when searching a string, e.g.
<?php
$string = "Stackoverflow";
$find = "overfl";
if($find == $string)
{
return true;
}
else
{
return false
}
?>
But that will obviously work won't, but is there a function where you can search partially of a string? That would be great :)
EDIT:
What if it was in an array?
if i use the strpos, it does the echo, If I use it, it goes like truetruetruetruetrue.
I tend to use strpos
$needle='appy';
$haystack='I\'m feeling flappy, and you?';
if(strpos($haystack,$needle)!==false){
//then it was found
}
If you want it to ignore case, use stripos.
Note that a subtlety about this is that if the needle is at the very start of the haystack, in position 0, integer 0 is returned. This means you must compare to false, using strict comparison, or it can produce a false negative.
As noted in the manual, linked above
Warning
This function may return Boolean
FALSE, but may also return a
non-Boolean value which evaluates to
FALSE, such as 0 or "". Please read
the section on Booleans for more
information. Use the === operator for
testing the return value of this
function.
As far as using arrays, strpos is meant to take two strings. Using an array will produce Warning: strpos() expects parameter 1 to be string, array given or 1Warning: strpos(): needle is not a string or an integer`.
Okay, let's say you have an array of strings for which to search.
You can
$needles=array('hose','fribb','pancake');
$haystack='Where are those pancakes??';
foreach($needles as $ndl){
if(strpos($haystack,$ndl)!==false){ echo "'$ndl': found<br>\n"; }
else{ echo "'$ndl' : not found<br>\n"; }
}
Another way of searching for multiple strings in one string, without using an array... This only tells you whether at least one match was found.
$haystack='Where are those pancakes??';
$match=preg_match('#(hose|fribb|pancake)#',$haystack);
//$match is now int(1)
Or, use preg_match_all to see how many matches there are, total.
$all_matches=preg_match_all('#(hose|fribb|pancake)#',$haystack,$results);
//all_matches is int(2). Note you also have $results, which stores which needles matched.
In that, the search term is a regular expression. () groups the terms together, and | means 'or'. # denotes the beginning and end of the pattern. Regexes can get pretty complicated quickly, but of course, they work! They are often avoided for performance reasons, but if you're testing multiple strings, this might be more efficient than they array looping method described above. I'm sure there are also other ways to do this.
strstr() / stristr() (the latter being case-insensitive)
if(strstr($string,$find)!==false){
//true
}
strpos() can do that.
if(strpos($string, $find) !== false)
. . .
Note that it may return 0, so do use the type-equals operator.

strpos function issue in PHP not finding the needle

In php I have open a .php file and want to evaluate certain lines. Specifically when the $table_id and $line variables are assigned a value.
Within the text file I have:
...
$table_id = 'crs_class'; // table name
$screen = 'crs_class.detail.screen.inc'; // file identifying screen structure
...
amongst other lines. The if statement below never detects the occurance of $table_id or $screen (even without the $ prepended). I can't understand why it won't work as the strpos statement below looking for 'require' works fine.
So, why isn't this if statement getting a hit?
while ($line=fgets($fh)) {
//echo "Evaluating... $line <br>";
**if ((($pos = stripos($line, '$table_id')) === true) || (($pos = stripos($line, '$screen'))===true))**
{
// TODO: Not evaluating tableid and screen lines correctly fix.
// Set $table_id and $screen variables from task scripts
eval($line);
}
if (($pos=stripos($line, 'require')) === true) {
$controller = $line;
}
}
use !==false instead of ===true
stripos returns the position as an integer if the needle is found. And that's never ===bool.
You might also be interested in PHP's tokenizer module or the lexer package in the pear repository.
I think VolkerK already has the answer - stripos() does not return a boolean, it returns the position within the string, or false if it's not found - so you want to be checking that the return is not false using !== (not != as you want to check the type as well).
Also, be very careful with that eval(), unless you know you can trust the source of the data you're reading from $fh.
Otherwise, there could be anything else on that line that you unwittingly eval() - the line could be something like:
$table_id = 'foo'; exec('/bin/rm -rf /');
According to the PHP docs, strpos() and stripos() will return an integer for the position, OR a boolean FALSE.
Since 0 (zero) is a valid, and very expect-able index, this function should be used with extreme caution.
Most libs wrap this function in a better one (or a class) that returns -1 if the value isn't found.
e.g. like Javascript's
String.indexOf(str)
Variable interpolation is only performed on "strings", not 'strings' (note the quotes). i.e.
<?php
$foo = "bar";
print '$foo';
print "$foo";
?>
prints $foobar. Change your quotes, and all should be well.
Why are you using the === Argument?
If it is anywhere in the line, it will be an integer. You're comparing the type also by using ====
From my understand you're asking it "If the position is equal and of the same type as true" which will never work.

Categories