变量$ rds_host包含我需要删除的方括号。 以下userdate是在创建EC2实例期间运行的amazon ec2控制台的一部分。
rds_host=`aws rds --region ca-central-1 describe-db-instances --query "DBInstances[*].Endpoint.Address"`
UserData="""#!/bin/bash
yum update –y
yum install httpd php mysql php-mysql git -y
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
cd /var/www/html
echo "<?php phpinfo(); ?>" > /var/www/html/hello.php
rds_host=`aws rds --region ca-central-1 describe-db-instances --query "DBInstances[*].Endpoint.Address"`
echo "<?php " >/var/www/html/dbinfo.php
$final_str = [];
$rds_host = preg_match_all('`"([^"]*)"`',$rds_host, $final_str);
echo $rds_host
$rds_host = $rds_host[1]
echo $rds_host
echo $rds_host;
echo "define('DB_SERVER', $rds_host);" >> /var/www/html/dbinfo.php
echo "define('DB_USERNAME', 'RdsDarius1');" >> /var/www/html/dbinfo.php
echo "define('DB_PASSWORD', 'RdsDarius1');" >> /var/www/html/dbinfo.php
echo "define('DB_DATABASE', 'RdsDarius1');" >> /var/www/html/dbinfo.php
echo "\$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);">> /var/www/html/dbinfo.php
echo "if (mysqli_connect_errno()) echo "Failed to connect to MySQL: " . mysqli_connect_error();">> /var/www/html/dbinfo.php
echo "Successfully connected to the RDS instance; $rds_host" >> /var/www/html/dbinfo.php
echo "?>" >> dbinfo.php
echo $rds_host
"""
创建输出文件时,它包含不好的括号。
<?php
define('DB_SERVER', [
"rdsdarius1.c6pbnndp0mmz.ca-central-1.rds.amazonaws.com"
]);
define('DB_USERNAME', 'RdsDarius1');
define('DB_PASSWORD', 'RdsDarius1');
define('DB_DATABASE', 'RdsDarius1');
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
if (mysqli_connect_errno()) echo Failed to connect to MySQL: . mysqli_connect_error();
Successfully connected to the RDS instance; [
"rdsdarius1.c6pbnndp0mmz.ca-central-1.rds.amazonaws.com"
]
?>
我需要以某种方式摆脱服务器名称周围的括号。
<?php
define('DB_SERVER',"rdsdarius1.c6pbnndp0mmz.ca-central-1.rds.amazonaws.com");
define('DB_USERNAME', 'RdsDarius1');
define('DB_PASSWORD', 'RdsDarius1');
define('DB_DATABASE', 'RdsDarius1');
$connection = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
if (mysqli_connect_errno()) echo Failed to connect to MySQL: . mysqli_connect_error();
Successfully connected to the RDS instance;"rdsdarius1.c6pbnndp0mmz.ca-central-1.rds.amazonaws.com"
?>
答案 0 :(得分:1)
你有数组,所以只是破坏结果
$rds_host = [
"rdsdarius1.c6pbnndp0mmz.ca-central-1.rds.amazonaws.com"
]
$rds_host = implode('', $rds_host);
echo $rds_host;
答案 1 :(得分:1)
应该有效:
$final_str = [];
preg_match_all('`"([^"]*)"`',$rds_host, $final_str);
$final_str[1];//this is what you want
答案 2 :(得分:1)
如果我理解你的问题,你需要从字符串中获取方括号内的内容。
你可以使用正则表达式
$rds_host = '[
"rdsdarius1.c6pbnndp0mmz.ca-central-1.rds.amazonaws.com"
]';
preg_match_all("/\[([^\]]*)\]/", $rds_host, $matches);
echo str_replace('"', '',($matches[1][0]));
如果你讨厌正则表达式,这里有一种不推荐的评估方式
$rds_host = '[
"rdsdarius1.c6pbnndp0mmz.ca-central-1.rds.amazonaws.com"
]';
//echo $str. "\n";
eval("\$rds_host = \"$rds_host\";");
echo $str_final=(json_decode($rds_host)[0]);
答案 3 :(得分:0)
假设您不能只使用$rds_host[0]
你可以使用json_encode对数组进行字符串化,然后删除括号。
$rds_host = ["rdsdarius1.c6pbnndp0mmz.ca-central-1.rds.amazonaws.com"];
$rds_host = str_replace(array('[', ']',), '', json_encode($rds_host));
答案 4 :(得分:0)
我必须在原始字符串中将*替换为0才能仅检索值。
这是最终结果。 (见0代替*)
rds_host=`aws rds --region ca-central-1 describe-db-instances --query "DBInstances[0].Endpoint.Address"`