Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP更改字符串匹配上的数组值?_Php_Arrays - Fatal编程技术网

PHP更改字符串匹配上的数组值?

PHP更改字符串匹配上的数组值?,php,arrays,Php,Arrays,我想知道是否有可能操纵与特定字符串匹配的数组中的数据?但老实说,我不知道从哪里开始谷歌搜索,或者我的声明是否有意义 我有一个PHP页面,其中一个数组存储在变量($user)中。默认情况下,当调用$user变量生成图像时,数组显示“欢迎来到地球”,后跟“人口0”。我还创建了从另一个页面上的表单更改数组“name”和“description”的功能 <?php // link to the font file on the server $fontname = 'font/Helvetica

我想知道是否有可能操纵与特定字符串匹配的数组中的数据?但老实说,我不知道从哪里开始谷歌搜索,或者我的声明是否有意义

我有一个PHP页面,其中一个数组存储在变量($user)中。默认情况下,当调用$user变量生成图像时,数组显示“欢迎来到地球”,后跟“人口0”。我还创建了从另一个页面上的表单更改数组“name”和“description”的功能

<?php

// link to the font file on the server
$fontname = 'font/HelveticaNeue-BoldCond.otf';
// controls the spacing between text
$i=30;
//JPG image quality 0-100
$quality = 90;

function create_image($user){

        global $fontname;   
        global $quality;
        $file = "covers/welcome.jpg";   

    // if the file already exists dont create it again just serve up the original   
    //if (!file_exists($file)) {    


            // define the base image that we lay our text on
            $im = imagecreatefromjpeg("pass.jpg");

            // setup the text colours
            $color['blue'] = imagecolorallocate($im, 13, 42, 102);
            $color['orange'] = imagecolorallocate($im, 245, 130, 32);

            // this defines the starting height for the text block
            $y = imagesy($im) - $height - 365;

        // loop through the array and write the text    
        foreach ($user as $value){
            // center the text in our image - returns the x value
            $x = center_text($value['name'], $value['font-size']);  
            imagettftext($im, $value['font-size'], 0, $x, $y+$i, $color[$value['color']], $fontname,$value['name']);
            // add 32px to the line height for the next text block
            $i = $i+32; 

        }
            // create the image
            imagejpeg($im, $file, $quality);

    //}

        return $file;   
}

function center_text($string, $font_size){

            global $fontname;

            $image_width = 800;
            $dimensions = imagettfbbox($font_size, 0, $fontname, $string);

            return ceil(($image_width - $dimensions[4]) / 2);               
}



    $user = array(

        array(
            'name'=> 'Welcome to Planet Earth', 
            'font-size'=>'27',
            'color'=>'blue'),

        array(
            'name'=> 'Population 0', 
            'font-size'=>'27',
            'color'=>'orange'),

    );


    if(isset($_POST['submit'])){

    $error = array();

        if(strlen($_POST['name'])==0){
            $error[] = 'Please enter Planet Name';
        }

    if(count($error)==0){

    $user = array(

        array(
            'name'=> $_POST['name'], 
            'font-size'=>'27',
            'color'=>'blue'),

        array(
            'name'=> $_POST['description'],
            'font-size'=>'16',
            'color'=>'orange'),

    );      

    }

    }

// run the script to create the image
$filename = create_image($user);

?>

现在我的问题是:有没有办法将“地球”中字母“A”的颜色改为橙色,而将文本的其余部分保留为蓝色

奖励:有没有办法确保“星球”和/或“人口”中的“A”除了“地球”中的“A”之外,不会改为橙色


如果有人能帮我指出正确的方向,那太棒了!我被难住了,觉得我使用了错误的搜索词来确定这是否可行。

您应该提供更多关于预期输出的详细信息,因为看起来您试图生成包含所需文本的图像,但在您的描述中没有提及这一点。甚至提供一个你想制作的图像样本。