我想从路径中删除其他反斜杠(\
)。
我有这段代码。
$imagePath='/images/';
$query=mysqli_query($conn,"select sid,fname,mname,lname,did,ppic from hrm_staff ");
if(mysqli_num_rows($query) >0) {
$response["hrm_staff"] = array();
while ($row = mysqli_fetch_array($query)) {
$recp = array();
$recp["sid"]=$row["sid"];
$fullName=$row['fname']."".$row['mname']."".$row['lname'];
$recp["fname"]= $fullName;
$recp["did"]=$row['did'];
$ppic=$row['ppic'];
$ppic=$imagePath.$ppic;
$ppic = str_replace('\\','',$ppic);
$recp["ppic"]= $ppic;
array_push($response["hrm_staff"], $recp);
}
echo json_encode($response);
当我print
或echo
时,我会收到此输出。
{"hrm_staff":[
{"sid":"1","fname":"GaneshMB","did":"6","ppic":"\/images\/ prof_pic\/sb.jpg"},
{"sid":"2","fname":"SwatiK","did":"5","ppic":"\/images\/ prof_pic\/ulogo.png"},
{"sid":"3","fname":"KomalC","did":"2","ppic":"\/images\/ prof_pic\/p1.jpg"},
{"sid":"4","fname":"KarthikR","did":"2","ppic":"\/images\/ prof_pic\/p3.jpg"},
{"sid":"5","fname":"RenuP","did":"5","ppic":"\/images\/ prof_pic\/p6.jpg"},
{"sid":"6","fname":"RahulMA","did":"5","ppic":"\/images\/ prof_pic\/p4.jpg"}
],"success":1,"message":"display records"}
即使使用str_replace()
后,我在图像路径中也会收到额外的反斜杠(\
)。我无法找到我犯错误的地方。
请帮帮我。
答案 0 :(得分:0)
在json格式上使用json_decode()
。这应该可以解决问题。
$a = '{"hrm_staff":[
{"sid":"1","fname":"GaneshMB","did":"6","ppic":"\/images\/ prof_pic\/sb.jpg"},
{"sid":"2","fname":"SwatiK","did":"5","ppic":"\/images\/ prof_pic\/ulogo.png"},
{"sid":"3","fname":"KomalC","did":"2","ppic":"\/images\/ prof_pic\/p1.jpg"},
{"sid":"4","fname":"KarthikR","did":"2","ppic":"\/images\/ prof_pic\/p3.jpg"},
{"sid":"5","fname":"RenuP","did":"5","ppic":"\/images\/ prof_pic\/p6.jpg"},
{"sid":"6","fname":"RahulMA","did":"5","ppic":"\/images\/ prof_pic\/p4.jpg"}
],"success":1,"message":"display records"}';
Print_r(json_decode($a));
参见演示here
答案 1 :(得分:0)
我得到了答案。 trim($ variable) 为我完成了这项工作。
这就是我所做的和它的工作。
$imagePath='www.abc.com/hrm';
$ppic1=$row['ppic'];
$ppic21=$imagePath."/".trim($ppic1); // here trim($ppic1) removed that extra character (\) from the url.