传入php函数的字符串在字符串中的第一个空格后断开

I've created a simple function that outputs an image with the correct html markup, seen below. The problem I am facing is when I pass into the alt text that has a space in it, such as "My cat is great", the alt breaks and shows alt="My" like <img src="blah.jpg" alt="My" cat is great class="home">. I'm having a hard time trying to figure this out. Any thoughts?

function image_creator($image_url, $alt=false, $class=false) {
    $string = '<img src='.$image_url.' alt='.$alt.' class='.$class.'>';
    return $string;
}

A little user failure. No problem!

Make sure you have your <img> tag correctly.

$image_url = 'https://example.com/image';
$alt       = 'My cat is awesome';
$class     = 'image';

So:

function image_creator($image_url, $alt=false, $class=false) {
    $string = '<img src='.$image_url.' alt='.$alt.' class='.$class.'>';
    return $string;
}

Outcome:

<img src="https://example.com/image" alt="My" cat="" is="" awesome="" class="image">

Should be:

function image_creator($image_url, $alt=false, $class=false) {
    $string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
    return $string;
}

Outcome:

<img src="https://example.com/image" alt="My cat is awesome" class="image">

Do you see the differences? Look carefully at the quotes.

Documentation:

Some examples about using quotes:


Personally I would like to use:

// $image below is being sent to the function
$image = [
    'image_url' => 'https://example.com/image',
    'alt'       => 'My cat is awesome',
    'class'     => 'image',
];

function image_creator($image = [])
{
    if(empty($image))
    {
        return 'No image';
    }

    return '<img src="' . $image['image_url'] . '" alt="' . $image['alt'] . '" class="' . $image['class'] . '">';
}

You're not actually outputting <img src="blah.jpg" alt="My" cat is great class="home"> - that's just how the browser is interpreting it.

You're outputting <img src=blah.jpg alt=My cat is great class=home>

You need to output some quotes:

$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
$string = "<img src='".$image_url."' alt='".$alt."' class='".$class."'>';

try this i think it will work

//Multiple solution with multiple way
$image_url = 'https://example.com/image';
$alt       = 'My cat is awesome';
$class     = 'image';

$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
//also you can direct echo 
echo '<img src="',$image_url,'" alt="',$alt,'" class="',$class,'">';

//also you can direct echo 
$string=  "<img src='{$image_url}' alt='{$alt}' class='{$class}' >";

//also you can direct echo 
$string =  "<img src='${image_url}' alt='${alt}' class='${class}' >";

echo $string;