Warning: strpos() [function.strpos]: Empty delimiter - php

I have a plugin that uses strpos() method and on one site I'm getting this error
Warning: strpos() [function.strpos]: Empty delimiter. in /home/mysite/public_html/wp-includes/compat.php on line 55
Any ideas what the likely cause of this could be?
excerpt from compat.php
if (!function_exists('stripos')) {
function stripos($haystack, $needle, $offset = 0) {
return strpos(strtolower($haystack), strtolower($needle), $offset);
}
}
My code...
function myFunction($thePost)
{
$theContent = $thePost->post_content;
$myVar1 = array();
preg_match_all('/<a\s[^>]*href=\"([^\"]*)\"[^>]*>(.*)<\/a>/siU',$theContent,$myVar1);
$myVar2 = 0;
foreach ($myVar1[1] as $myVar3)
{
$myVar4 = $myVar1[2][$myVar2];
$myVar5 = FALSE;
$myVar6 = get_bloginfo('wpurl');
$myVar7 = str_replace('http://www.','',$myVar3);
$myVar7 = str_replace('http://','',$myVar7);
$myVar8 = str_replace('http://www.','',$myVar6);
$myVar8 = str_replace('http://','',$myVar8);
if (strpos($myVar3,'http://')!==0 || strpos($myVar7,$myVar8)===0) return TRUE;
$myVar2++;
}
return FALSE;
}

Something is passing an empty string as the second argument to Wordpress' implementation of stripos() (and it's not the code you've pasted above).
Can I ask why you a using PHP 4?

TRY ADDING double quotes on the on the variable
change this line:
if (strpos($myVar3,'http://')!==0 || strpos($myVar7,$myVar8)===0) return TRUE;
into:
if (strpos(".$myVar3.",'http://')!==0 || strpos(".$myVar7.",".$myVar8.")===0) return TRUE;

Related

Helper does not function properly for Custom Lang.php core in Codeigniter v2.x.x

Let say the input of my custom language was:
$lang['number.you.got'] = 'Number you got was %1, %2 and %3';
I've modified the Lang.php in /system/core/ codeigniter 2 folder. Here is the code:
function line($line = '')
{
//get the arguments passed to the function
$args = func_get_args();
//count the number of arguments
$c = count($args);
//if one or more arguments, perform the necessary processing
if ($c)
{
//first argument should be the actual language line key
//so remove it from the array (pop from front)
$line = array_shift($args);
//check to make sure the key is valid and load the line
$line = ($line == '' OR ! isset($this->language[$line])) ? FALSE : $this->language[$line];
// Because killer robots like unicorns!
if ($line === FALSE)
{
log_message('error', 'Could not find the language line "'.$line.'"');
}
//if the line exists and more function arguments remain
//perform wildcard replacements
if ($line && $args)
{
$i = 1;
foreach ($args as $arg)
{
$line = preg_replace('/\%'.$i.'/', $arg, $line);
$i++;
}
}
}
else
{
//if no arguments given, no language line available
$line = false;
}
return $line;
}
The modified codes above is to implement the language in the website, let say:
$this->lang->line('number.you.got','number1','number2','number3');
and the output will be
Number you got was number1, number2 and number3
Now, I used language_helper in codeigniter v2
function lang($line)
{
$CI =& get_instance();
$line = $CI->lang->line($line);
$line = '<label>'.$line."</label>";
return $line;
}
But the result was not the same as I got before.
My expected result that if I can easily use helper lang('number.you.got','number1','number2','number3')...
Anybody can help me?

Error using substr()

I am getting error while using substr:
Warning: substr() expects parameter 3 to be long
I am new to php and could not locate the problem. I would appreciate any help.
Here is the code:
function prepare_string($passed_string,$length)
{
$matches = array("`","!","#","®","©","~","#","$","%","^","&","*","-","=","+","|","\\","[","{","]","}","(",")",";",":","\"","'",",","<",">",".","?","/","\'","\\","'","’");
$passed_string =substr($passed_string,0,$length);
for($i=0;$i<count($matches);$i++)
{
$passed_string = str_replace($matches[$i],"_",$passed_string);
}
$passed_string = str_replace(" ","_",$passed_string);
return $passed_string;
}
var_dump($length) and see what's in there.
remove foreach loop and instead put just this line $passed_string = str_replace($matches,"_",$passed_string);
add " " in $matches array too and then you can get rid of this line $passed_string = str_replace(" ","_",$passed_string);
Sounds like substr() is not receiving a well-formed value for some reason... One option would be to add a line inside the function that casts $length as an integer. Example:
function prepare_string($passed_string,$length)
{
// Add this line
$length = (int) $length;
$matches = array("`","!","#","®","©","~","#","$","%","^","&","*","-","=","+","|","\\","[","{","]","}","(",")",";",":","\"","'",",","<",">",".","?","/","\'","\\","'","’");
$passed_string =substr($passed_string,0,$length);
for($i=0;$i<count($matches);$i++)
{
$passed_string = str_replace($matches[$i],"_",$passed_string);
}
$passed_string = str_replace(" ","_",$passed_string);
return $passed_string;
}

Cannot return value from function

I have a PHP function:
function unserialize_recursive($data, $i = 0) {
$unserialized = unserialize($data);
if ($unserialized) {
$i++;
}
if (!is_string($unserialized) || unserialize($unserialized) === FALSE) {
/* placeholder - see explanation below */
return array($i, $unserialized);
} elseif (unserialize($unserialized) !== FALSE) {
unserialize_recursive($unserialized, $i);
}
return FALSE;
}
I call this function with:
$data = unserialize_recursive($serialized_string);
var_dump($data);
But the output of var_dump($data) is bool(false).
However, if I add var_dump($unserialized) in the position of the text /* placeholder - see explanation below */, I get the expected output.
So why can I not return that variable ($unserialized)? If I use gettype() on it at that point in the function, it returns array.
I'm using Netbeans and all the syntax highlighting indicates the code is properly formed with no typos. I'm baffled. Have I missed something really obvious?
My guess is that you forgot a return:
function unserialize_recursive($data, $i = 0) {
$unserialized = unserialize($data);
if ($unserialized) {
$i++;
}
if (!is_string($unserialized) || unserialize($unserialized) === FALSE) {
/* placeholder - see explanation below */
return array($i, $unserialized);
} elseif (unserialize($unserialized) !== FALSE) {
return unserialize_recursive($unserialized, $i);
}
return FALSE;
}
You use recursion in your function.
So your var_dump($unserialized) is called from a recursed invocation, but main invocation returns false.
You probably need to change "unserialize_recursive($unserialized, $i);" to
return unserialize_recursive($unserialized, $i);
You are missing return in front of
unserialize_recursive($unserialized, $i);
So it should be like this
return unserialize_recursive($unserialized, $i);
Without return function would run itself but then leave if condition and execute return FALSE. By placing return in front you end the current function and start another one.

preg_match error and debugging php code

Please help. I'm getting this error:
( ! ) Warning: preg_match(): Unknown modifier 'b' in C:\wamp\www\pmd\install\ioncube_checker.php on line 22
When I run the following code:
function system_info($php_info) {
$server_info = array();
$server_info['thread_safe'] = 'false';
$server_info['debug_build'] = 'false';
$server_info['php_ini_path'] = '';
foreach (explode("\n",$php_info) as $line) {
if (preg_match('/command/',$line)) {
continue;
}
if (preg_match('/thread safety.*(enabled|yes)/Ui',$line)) {
$server_info['thread_safe'] = 'true';
}
if (preg_match('/debug.*(enabled|yes)/Ui',$line)) {
$server_info['debug_build'] = 'true';
}
if (preg_match("/configuration file.*(</b></td><TD ALIGN=\"left\">| => |v\">)([^ <]*)(.*</td>*)?/",$line,$match)) {
$server_info['php_ini_path'] = $match[2];
if (!#file_exists($php_ini_path)) {
$server_info['php_ini_path'] = '';
}
}
$cgi_cli = ((strpos(php_sapi_name(),'cgi') !== false) || (strpos(php_sapi_name(),'cli') !== false));
$cgi_cli ? $server_info['cgi_cli'] = 'true' : $server_info['cgi_cli'] = 'false';
}
return $server_info;
}
Since you are using / as the regex delimiter you need to escape any / with a \. However, it's much easier to use a different delimiter when dealing with HTM:
preg_match("/configuration file.*(</b></td><TD ALIGN=\"left\">| => |v\">)([^ <]*)(.*</td>*)?/",$line,$match)
should be
preg_match("#configuration file.*(</b></td><TD ALIGN=\"left\">| => |v\">)([^ <]*)(.*</td>*)?#",$line,$match)
However, you should consider not using regexes to parse HTML at all - using a DOM engine is much better for it, and PHP already has one.

Using a keyword as variable name in an INI file

I have the following in an INI file:
[country]
SE = Sweden
NO = Norway
FI = Finland
However, when var_dump()ing PHP's parse_ini_file() function, I get the following output:
PHP Warning: syntax error, unexpected BOOL_FALSE in test.ini on line 2
in /Users/andrew/sandbox/test.php on line 1
bool(false)
It appears that "NO" is reserved. Is there any other way I can set a variable named "NO"?
Another hack would be to reverse your ini keys with their values and use array_flip:
<?php
$ini =
"
[country]
Sweden = 'SE'
Norway = 'NO'
Finland = 'FI'
";
$countries = parse_ini_string($ini, true);
$countries = array_flip($countries["country"]);
echo $countries["NO"];
Still you will need to use quotes around NO (at least), if you do
Norway = NO
you don't get an error but value for $countries["NO"] will be an empty string.
This propably comes a little late but the way PHPs parse_ini_file works bothered me so much that I wrote my own little parser.
Feel free to use it, but use with care it has only been shallowly tested!
// the exception used by the parser
class IniParserException extends \Exception {
public function __construct($message, $code = 0, \Exception $previous = null) {
parent::__construct($message, $code, $previous);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
// the parser
function my_parse_ini_file($filename, $processSections = false) {
$initext = file_get_contents($filename);
$ret = [];
$section = null;
$lineNum = 0;
$lines = explode("\n", str_replace("\r\n", "\n", $initext));
foreach($lines as $line) {
++$lineNum;
$line = trim(preg_replace('/[;#].*/', '', $line));
if(strlen($line) === 0) {
continue;
}
if($processSections && $line{0} === '[' && $line{strlen($line)-1} === ']') {
// section header
$section = trim(substr($line, 1, -1));
} else {
$eqIndex = strpos($line, '=');
if($eqIndex !== false) {
$key = trim(substr($line, 0, $eqIndex));
$matches = [];
preg_match('/(?<name>\w+)(?<index>\[\w*\])?/', $key, $matches);
if(!array_key_exists('name', $matches)) {
throw new IniParserException("Variable name must not be empty! In file \"$filename\" in line $lineNum.");
}
$keyName = $matches['name'];
if(array_key_exists('index', $matches)) {
$isArray = true;
$arrayIndex = trim($matches['index']);
if(strlen($arrayIndex) == 0) {
$arrayIndex = null;
}
} else {
$isArray = false;
$arrayIndex = null;
}
$value = trim(substr($line, $eqIndex+1));
if($value{0} === '"' && $value{strlen($value)-1} === '"') {
// too lazy to check for multiple closing " let's assume it's fine
$value = str_replace('\\"', '"', substr($value, 1, -1));
} else {
// special value
switch(strtolower($value)) {
case 'yes':
case 'true':
case 'on':
$value = true;
break;
case 'no':
case 'false':
case 'off':
$value = false;
break;
case 'null':
case 'none':
$value = null;
break;
default:
if(is_numeric($value)) {
$value = $value + 0; // make it an int/float
} else {
throw new IniParserException("\"$value\" is not a valid value! In file \"$filename\" in line $lineNum.");
}
}
}
if($section !== null) {
if($isArray) {
if(!array_key_exists($keyName, $ret[$section])) {
$ret[$section][$keyName] = [];
}
if($arrayIndex === null) {
$ret[$section][$keyName][] = $value;
} else {
$ret[$section][$keyName][$arrayIndex] = $value;
}
} else {
$ret[$section][$keyName] = $value;
}
} else {
if($isArray) {
if(!array_key_exists($keyName, $ret)) {
$ret[$keyName] = [];
}
if($arrayIndex === null) {
$ret[$keyName][] = $value;
} else {
$ret[$keyName][$arrayIndex] = $value;
}
} else {
$ret[$keyName] = $value;
}
}
}
}
}
return $ret;
}
What does it differently? Variable names may only consist of alphanumerical characters but other than that no restrictions to them. Strings must be encapsulated with " everything else has to be a special value like no, yes, true, false, on, off, null or none. For mapping see code.
Kind of a hack but you can add backticks around the key names:
[country]
`SE` = Sweden
`NO` = Norway
`FI` = Finland
Then access them like so:
$result = parse_ini_file('test.ini');
echo "{$result['`NO`']}\n";
Output:
$ php test.php
Norway
I was getting this error when there were single quote combinations in the string such as 't or 's. To get rid of the problem, I wrapped the string in double quotes:
Before:
You have selected 'Yes' but you haven't entered the date's flexibility
After:
"You have selected 'Yes' but you haven't entered the date's flexibility"
I ran into the same problem and tried to escape the name in every possible way.
Then I remembered that because of the INI syntax both names and values will be trimmed, so the following workaround MAYBE should do the trick:
NL = Netherlands
; A whitespace before the name
NO = Norway
PL = Poland
And it works ;) As long as your co-workers read the comments (which is not always the case) and don't delete it accidentally. So, yes, the array flipping solution is a safe bet.
From the manual page for parse_ini_file:
There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, false, on, off, none.
So no, you can't set a variable NO.

Categories