I've got a field in my MySQL table with several params. I want to just get the filename.jpg or filename.gif as a new variable.
Here's the info in the table (Joomla banner module)
$string = '{"imageurl":"images\/banners\/foo.gif","width":"","height":"","alt":""}';
$theimage = find(gif or jpg, $string); // Or something.. :-)
You can use the json_decode() method to decode the string and convert it into an object. And then use the pathInfo() method to get the extension as shown below.
<?php
$string = '{"imageurl":"images\/banners\/foo.gif","width":"","height":"","alt":""}';
$obj = json_decode($string);
$names = pathInfo($obj->imageurl);
echo $names['extension']; //gif
echo $names['basename']; //foo.gif
echo $names['filename']; //foo
?>
If you want to use regex:
$input_lines = '{"imageurl":"images\/banners\/foo.gif","width":"","height":"","alt":""}';
preg_match_all("/[A-Za-z0-9]+\.(gif|jpg)/", $input_lines, $output_array);
print_r($output_array);
Result:
Array
(
[0] => Array
(
[0] => foo.gif
)
[1] => Array
(
[0] => gif
)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.