语法错误,意外的'功能'在12号线上

时间:2015-12-12 03:34:01

标签: php regex function

我有一个url,其中包含各种POST-DATA和最后一个图像文件。我的示例链接是:http://website-link.com/?page=gf_signature&signature=565dbca63791e5.87676354.png

我想从网址中分离565dbca63791e5.87676354.png并从中分隔扩展名(.png)。

我试过这个:

<?php
    $images = array();
    $imagesNew = array();
    $imgUrls = array(
                    'ptSignature' => 'http://website-link.com/?page=gf_signature&signature=5668695879dc84.35037895.png',
                    'pSignature' => 'http://website-link.com/?page=gf_signature&signature=5668694f80aa55.79055562.png',
                    'witness1Signature' => 'http://website-link.com/?page=gf_signature&signature=5668695875c6e5.03917128.png',
                    'witness2Signature' => 'http://website-link.com/?page=gf_signature&signature=5668695879dc84.35037895.png',
                )


    function make_without_ext($str)
        {
            $regex = "/signature=(?<signature>[^&]+)/";

            preg_match($regex, $str, $matches);
            $signature = $matches["signature"];
            $ext = substr(strrchr($signature, '.'), 1);

            $without_extension = basename($signature, '.png');
            return $without_extension;
        }


    foreach ($imgUrls as $imgUrl) {
        $imgWithoutExt = make_without_ext($imgUrl);
        array_push($images, $imgWithoutExt);
    }

    foreach ($images as $image) {
        $content = file_get_contents($image);
        $data = base64_encode($content);
        array_push($imagesNew, $data) 
    }

    print '<pre>';
    print_r ($imagesNew);
    print '<pre>';

但它显示syntax error, unexpected 'function' (T_FUNCTION)

1 个答案:

答案 0 :(得分:1)

您之后丢失了分号:

$imgUrls = array(
                    'ptSignature' => 'http://website-link.com/?page=gf_signature&signature=5668695879dc84.35037895.png',
                    'pSignature' => 'http://website-link.com/?page=gf_signature&signature=5668694f80aa55.79055562.png',
                    'witness1Signature' => 'http://website-link.com/?page=gf_signature&signature=5668695875c6e5.03917128.png',
                    'witness2Signature' => 'http://website-link.com/?page=gf_signature&signature=5668695879dc84.35037895.png',
                )

使用分号可以解决问题。