Search code examples
phparraysstringspace

Image Path Space Issue - Php Array


I don't know where the issue in my code.

There is a space automatically added to the image name after i get the value from array.

i have tried with str_replace too. but that space still shows in output.

here it is my code

<?php
$domain='localhost/test';

$id=5;

$animal = array("alligator.jpg", "
ant.jpg", "
bear.jpg", "
bee.jpg", "
butterfly.jpg", "
cat.jpg", "
camel.jpg", "
cheetah.jpg", "
cockroach.jpg", "
cow.jpg", "
crow.jpg", "
deer.jpg");

$imageName=$animal[$id];
$imageName2=str_replace(" ","",$imageName);

echo "$domain/img/$imageName";
echo "<br /><br />$domain/img/$imageName2";

?>

current output: localhost/test/img/ cat.jpg -> space added before the cat name

expected output: localhost/test/img/cat.jpg


Solution

  • $imageName2=trim($imageName);

    use this

    or

    $imageName2= preg_replace('/\s+/', '', $imageName);