php 如何解决 strpos() “空分隔符”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4568016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:29:58  来源:igfitidea点击:

How do I resolve a strpos() "empty delimiter" error?

phpjoomla1.5strpos

提问by Jamison

Here's the error:

这是错误:

For: PHP 5.2+

适用于:PHP 5.2+

Warning: strpos() [function.strpos]: Empty delimiter in /helper.php on line 445

警告:strpos() [function.strpos]:第 445 行 /helper.php 中的空分隔符

and here is the code on that line:

这是该行的代码:

if($src = $img->getAttribute('src') AND strpos($src,$fgParams->get('base')) === false) { // prevents repeat processing
            EgivaUtility::profiling('Processing Image SRC: '.$src);
            // fix rel paths
            $src = EgivaUtility::encode_url(EgivaUtility::makeAbsUrl($origLink,$src));
            if($image_details = @getimagesize($src) AND !in_array($image_details[0],array(1,2)) AND !in_array($image_details[1],array(1,2))) {
                EgivaUtility::profiling('Image Details: '.print_r($image_details,true));
                $title = $img->getAttribute('title');
                $alt = $img->getAttribute('alt');
                if($fgParams->get('save_img')) { // consider restoring the JPath::clean()
                    // find image name and extension
                    $name = $title ? EgivaUtility::stringURLSafe($title) : EgivaUtility::stringURLSafe($alt);
                    preg_match('#[/?&]([^/?&]*)(\.jpg|\.jpeg|\.gif|\.png)#i',$src,$matches);
                    $ext = isset($matches[2]) ? strtolower($matches[2]) : '';
                    if(!$name) $name = isset($matches[1]) ? EgivaUtility::stringURLSafe($matches[1]) : md5($src);
                    unset($matches);
                    //create image file    
                    $filename = $fgParams->get('name_prefix').$name.$ext;
                    $filepath = $fgParams->get('savepath').'images'.DS.$filename;
                    if(!file_exists($filepath)) {
                        if($contents = EgivaUtility::getUrl($src,$fgParams->get('scrape_type'),'images',$filepath)) {
                            $saved = true;
                            //if(EgivaUtility::savefile($contents,$name,$update=false,$header=null,$fgParams->get('savepath').'images')) $saved = true;
                        }
                    } else {
                        $saved = true;
                    }
                    if($saved) $img->setAttribute('src', $fgParams->get('srcpath').'images/'.$filename);
                } else {
                    $img->setAttribute('src',$src);
                }
                EgivaUtility::profiling('Final Image SRC: '.$img->getAttribute('src'));
            //    $class = $img->getAttribute('class');
            //    $width = $img->getAttribute('width');
            //    $height = $img->getAttribute('height');
                if(strlen($alt) >= JString::strlen($content['title']) OR !$alt) {
                    $img->setAttribute('alt',$content['title']);
                }
                if($fgParams->get('rmv_img_style')) {
                    $img->removeAttribute('class');
                    $img->removeAttribute('style');
                    $img->removeAttribute('align');
                    $img->removeAttribute('border');
                    $img->removeAttribute('width');
                    $img->removeAttribute('height');
                }
                if($fgParams->get('img_class')) {
                    $img->setAttribute('class',$fgParams->get('img_class'));
                }
                $new_img = $dom2->importNode($imgs->item($k),true);
                $dom2->appendChild($new_img);
                $images[$k] = $dom2->saveHTML();
                $dom2->removeChild($new_img);

                // hack to avoid encoding problems
                $text = preg_replace($regex,'fg_img'.$k,$text,$limit=1);
                $replace[$k] = 'fg_img'.$k;
                $k++;
            } else {
                EgivaUtility::profiling('Image Rejected');
                $text = preg_replace($regex,'',$text,1);
            }
        }
    }

回答by Brian Clapper

This error occurs when the second parameter to strposis empty. For instance, I can easily simulate this error at the command line:

strpos的第二个参数为空时会发生此错误。例如,我可以在命令行轻松模拟此错误:

$ php
<?php
echo strpos("foo", "");
?>
^D
Warning: strpos(): Empty delimiter in - on line 2

In your code, it means that $fgParams->get('base')is empty.

在您的代码中,这意味着它$fgParams->get('base')是空的。

Add some checks to your code to ensure that the values you pass to strposare valid, and the error will go away.

向您的代码添加一些检查以确保您传递给strpos的值有效,并且错误将消失。

回答by Jason

Change line 445

更改第 445 行

from

if($src = $img->getAttribute('src') AND strpos($src,$fgParams->get('base')) === false) { // prevents repeat processing

to

if($src = $img->getAttribute('src') AND $fgParams->get('base')!="" AND strpos($src,$fgParams->get('base')) === false) { // prevents repeat processing

Seems like that get('base') is returning nothing. Is this possible in your script? perhaps it's the indication of a previous error in another area of the program.

似乎 get('base') 没有返回任何内容。这在您的脚本中可能吗?也许这是程序另一个区域中先前错误的指示。

回答by Malay Ladu

Please make sure that value of $fgParams->get('base') is not blank as json mentioned in condition.

请确保 $fgParams->get('base') 的值不为条件中提到的 json 空白。

回答by Erwin

I had the same problem with:

我有同样的问题:

if (htmlentities($controleren[$aantaltekens] == htmlentities($trans[$tellen])

The error disappeared when I added the two ()delimiters:

当我添加两个()分隔符时,错误消失了:

if (htmlentities($controleren[$aantaltekens]) == htmlentities($trans[$tellen]))

So try:

所以尝试:

if(($src = $img->getAttribute('src') AND strpos($src,$fgParams->get('base'))) === false)

Guess htmlentitiesrequires its parameters to be within (delimiters).

Guesshtmlentities要求其参数在(分隔符)内。