PHP在字符串匹配时更改数组值?

时间:2015-11-19 17:44:02

标签: php arrays

我想知道是否可以操作匹配特定字符串的数组中的数据?但老实说,我不确定从哪里开始谷歌搜索,或者我的陈述是否有意义。

我有一个PHP页面,其中一个数组存储在一个变量($ user)中。默认情况下,阵列显示"欢迎来到地球行星"其次是"人口0"调用$ user变量生成图像时。我也创造了改变阵列的能力" name"和"描述"从另一页上的表格。

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

?>

现在我的问题是:有没有办法改变字母的颜色&#34; A&#34;在&#34;地球&#34;到橙色,而剩下的文字蓝色?

奖金:任何确保&#34; A&#34;在&#34; Planet&#34;和/或&#34;人口&#34;除了&#34; A&#34;在&#34;地球&#34;?

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

0 个答案:

没有答案