0

I have string values related to images in a variable $photo covered by curly braces. I get this value from MySQL table. It has Image name, alt text, and image caption together in the string variable $photo. I want to extract image name, alt, and image caption covered by curly brackets.

I've added code that has the string value below,

<?php
$photo =' {"imagename":
    {"filename":"imagename.jpg","alt":"Hunting Rifle","caption":"This is Hunter Image"}
}';
?> 

I want to get filename and alt stings separately and store it in new variables separately[$filename,$alt] .

Anyone knows how to extract these particular strings from a string variable ?

2 Answers 2

3

That's JSON, so use json_decode.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Matt Ball, Its Extracting well. I've never used json in my previous projects. Thank you.
1
$jdphoto = json_decode($photo);

$imagename = $jdphoto->imagename;

$filename = $imagename->filename;
$alt = $imagename->alt;
$caption = $imagename->caption;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.