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

发布于 2024-10-09 23:59:44 字数 3892 浏览 3 评论 0原文

这是错误:

对于:PHP 5.2+

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

,以下是该行的代码:

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);
            }
        }
    }

Here's the error:

For: PHP 5.2+

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

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);
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花伊自在美 2024-10-16 23:59:44

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

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

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

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

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

$ 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.

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

戏剧牡丹亭 2024-10-16 23:59:44

将第 445 行从 更改

似乎

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

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

get('base') 没有返回任何内容。这在你的脚本中可能吗?也许这表明程序的另一个区域以前存在错误。

Change line 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.

Oo萌小芽oO 2024-10-16 23:59:44

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

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

╰沐子 2024-10-16 23:59:44

我遇到了同样的问题:

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

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

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

所以尝试:

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

猜测 htmlentities 要求其参数位于(分隔符)内。

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 htmlentities requires its parameters to be within (delimiters).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文