extract image file name from large string

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
User avatar
stevedeboo
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Feb 01, 2015 4:51 am
Location: Albufeira, Algarve, Portugal

Mon Mar 13, 2017 3:10 am

Hi folks I have a load of images stored in a mysql database and I want to migrate all the data into another db but the image is inside a load of other information, see below, now each image can have any number of characters in its name and also finish with jpg,png,bmp etc etc how can I extract each of the file names so I can migrate them into another db field as a single clean filename like fxdfsmall.jpg so I am guessing I need to extract everything between these 2 pints (searchStr":") (,!:sStrEnd"}])
example below

[{"name":"mainimage\/fxdfsmall_gn44od0r.jpg","usrName":"fxdfsmall.jpg","size":219937,"type":"image\/jpeg","searchStr":"fxdfsmall.jpg,!:sStrEnd"}]


Thanks in advance
Steve

NigelRen
php-forum Fan User
php-forum Fan User
Posts: 622
Joined: Fri Aug 05, 2011 9:53 am

Mon Mar 13, 2017 4:08 am

Problem is that you have two filenames in that string(fxdfsmall_gn44od0r.jpg and fxdfsmall.jpg), do you know which one is the correct one?
If the same pattern is repeated in each record, you could do as you say and simply extract that string between those two positions.

Perhaps do something simply to check - like extract the segment of name and then check that a file with that name exists.

User avatar
stevedeboo
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Feb 01, 2015 4:51 am
Location: Albufeira, Algarve, Portugal

Mon Mar 13, 2017 4:22 am

NigelRen wrote:
Mon Mar 13, 2017 4:08 am
Problem is that you have two filenames in that string(fxdfsmall_gn44od0r.jpg and fxdfsmall.jpg), do you know which one is the correct one?
If the same pattern is repeated in each record, you could do as you say and simply extract that string between those two positions.

Perhaps do something simply to check - like extract the segment of name and then check that a file with that name exists.
It looks like the 1st one is correct fxdfsmall_gn44od0r.jpg but I am note sure how to code the extraction procces for this


[{"name":"mainimage\/fxdfsmall_gn44od0r.jpg","usrName":"fxdfsmall.jpg","size":219937,"type":"image\/jpeg","searchStr":"fxdfsmall.jpg,!:sStrEnd"}]
so between ([{"name":"mainimage\/) and (","usrName) from above.

chorn
php-forum Fan User
php-forum Fan User
Posts: 551
Joined: Fri Apr 01, 2016 2:18 am

Mon Mar 13, 2017 4:53 am

the string given is valid JSON, use json_decode(..., true) on it to get a standard PHP Array to work with.

User avatar
stevedeboo
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Feb 01, 2015 4:51 am
Location: Albufeira, Algarve, Portugal

Mon Mar 13, 2017 4:56 am

chorn wrote:
Mon Mar 13, 2017 4:53 am
the string given is valid JSON, use json_decode(..., true) on it to get a standard PHP Array to work with.
Sorry not sure what you mean, Sorry if I am lame on this, but I need to extract the file name and then copy it into another field in the database so it only has the file name of the image.

AdoptiveSolution
New php-forum User
New php-forum User
Posts: 167
Joined: Wed Jun 15, 2016 8:35 am

Mon Mar 13, 2017 5:00 am

It's a JSON string.

Save this string in a file as image.json

Code: Select all

[{"name":"mainimage\/fxdfsmall_gn44od0r.jpg","usrName":"fxdfsmall.jpg","size":219937,"type":"image\/jpeg","searchStr":"fxdfsmall.jpg,!:sStrEnd"}]
Then use this with PHP to see how it looks :

Code: Select all

<?php

$string = file_get_contents("image.json");

$json = json_decode($string, true);

echo '<h4>The decoded JSON String</h4>';

echo '<pre>';
print_r ($json);
echo '</pre>';

?>
To parse JSON, search for answers :

http://www.google.com/search?q=php+parse+json+file

http://php.net/manual/en/function.json-decode.php

User avatar
stevedeboo
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Feb 01, 2015 4:51 am
Location: Albufeira, Algarve, Portugal

Mon Mar 13, 2017 2:35 pm

Thank you yes that works but how can I use the following to extract the filename this way and insert it back into the database in a spare field.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title></title>
</head>
<body>
<?php
$con = mysqli_connect("ip.ip.ip.ip","user","pass","db") or die("Some error occurred during connection " . mysqli_error($con));
$strSQL = ("SELECT MainImage FROM table");
$result = mysqli_query($con, $strSQL);
while ($row = mysqli_fetch_array($result))
{
echo $row['MainImage']."<BR>";
}
mysqli_close($con);
?>
</body>
</html>

chorn
php-forum Fan User
php-forum Fan User
Posts: 551
Joined: Fri Apr 01, 2016 2:18 am

Mon Mar 13, 2017 10:47 pm

i see no JSON string in the latest codes.

Post Reply