Nickmadd Posted September 13, 2014 Share Posted September 13, 2014 Hey guys I'm having an issue trying to get my PHP to display the first url of my exploded string and non of the others. The SQL table column contains strings like this: http://www.imgurl.com/image.jpg,http://www.imgurl.com/image2.jpg,http://www.imgurl.com/image3.jpg,http://www.imgurl.com/image4.jpg I am currently using this code to display the table column onto my front end: <img src="'.implode('"/><img src="',explode(',', $row["PictureRefs"])[0]).'"/> However I am getting a parse error due to the fact I have added [0] to the code to try and display only the first exploded part of the string. Any idea how I can only display the first exploded part of the string? Thanks Link to comment https://forums.phpfreaks.com/topic/291048-displaying-only-the-first-part-of-an-exploded-php-string/ Share on other sites More sharing options...
kicken Posted September 13, 2014 Share Posted September 13, 2014 If you're only displaying one, then you don't need the implode statement. echo '<img src="'.(explode(',', $row["PictureRefs"])[0]).'"/>'; If you get a syntax error due to the [0] then your PHP version is not new enough for that syntax. You will need to assign the results of the explode to a variable then use that variable instead. $var = explode(',', $row["PictureRefs"]); echo '<img src="'.$var[0].'"/>'; Note that having delimiter-separated values stored in the database like that is typically a sign of bad design and you should re-think your database design. Link to comment https://forums.phpfreaks.com/topic/291048-displaying-only-the-first-part-of-an-exploded-php-string/#findComment-1491002 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.