Jalz Posted July 14, 2009 Share Posted July 14, 2009 Hello all, Need a little assistance again please. Within a database I have a field which is storing similar data to the following underneath. The page renders fine and displays two images if I view the article. However I am trying to create an index page and within the index page would like to extract the first image (or the path) into a variable (but Im open to ideas?), so I can reference that single image and a summary text on the index page ignoring the any other images. Is this possible and if so any example code please to get me off and running? I'm completely new to this but learning from examples and your kind help Jalz <p class="PageTitleHeading"><img class="left_10" title="Library outside" alt="Library outside" src="/Academic/Library/lib_external.jpg?n=9331" />AJK Library</p><p>The Library is one of the finest facilities of its kind in the country, which is testimony to the quality of the vision that saw to its opening in 1994.</p> <p>The Library houses in excess of 30,000 books, complemented by a wide range of periodicals and online resources, and combines the best elements of academic and public libraries. Downstairs <img class="right_10" title="library" style="width: 169px; height: 169px; " alt="library" src="/Academic/Library/DSC_0096a.jpg?n=7146" vspace="8" />seats 56 and successfully balances the need for a comfortable recreational reading space with space for collaborative work, and includes a staffed Issue Desk, 20 networked computers, an A3 colour printer/copier/scanner and a dedicated OPAC terminal. There are rooms, both upstairs and downstairs, that can be booked for library-based lessons. The Library is open and staffed 7 days a week for a total of 76.5 hours. The Library also houses the Careers Centre.</p> <p> </p> Quote Link to comment https://forums.phpfreaks.com/topic/165982-solved-trying-to-get-the-first-image-out-of-text-field/ Share on other sites More sharing options...
.josh Posted July 14, 2009 Share Posted July 14, 2009 If you just want the first img src: preg_match('~<img[^>]*src\s?=\s?[\'"]([^\'"]*)~i',$string,$imgsrc); echo $imgsrc[1]; If you want the whole tag: preg_match('~<img[^>]*>~i',$string,$imgsrc); echo $imgsrc[0]; Quote Link to comment https://forums.phpfreaks.com/topic/165982-solved-trying-to-get-the-first-image-out-of-text-field/#findComment-875432 Share on other sites More sharing options...
Jalz Posted July 15, 2009 Author Share Posted July 15, 2009 Hi CV, Thanks again for your prompt response. for my $string, im using the text field, but I cant find any references as to what I should be loading up in $imgsrc. When I try to echo $imgsrc, i get the following error Undefined offset: 1 which presumably means $imgsrc is not set. J Quote Link to comment https://forums.phpfreaks.com/topic/165982-solved-trying-to-get-the-first-image-out-of-text-field/#findComment-875697 Share on other sites More sharing options...
.josh Posted July 15, 2009 Share Posted July 15, 2009 The patterns are correct. Which one of those regexes are you using? The first one you should be using $imgsrc[1] the 2nd $imgsrc[0]. If you are using the correct $imgsrc element, then something else wrong. Echo $string to make sure it contains what you expect it to contain. Also, it would give you that message if there just wasn't any images to capture. Quote Link to comment https://forums.phpfreaks.com/topic/165982-solved-trying-to-get-the-first-image-out-of-text-field/#findComment-875717 Share on other sites More sharing options...
Jalz Posted July 15, 2009 Author Share Posted July 15, 2009 Hi CV, This is the code I'm using, which is the first one from your suggestion. <?php $string = $news_page_row->getField('text'); echo $string; preg_match('~<img[^>]*src\s?=\s?[\'"]([^\'"]*)~i',$string,$imgsrc); echo $imgsrc[1]; ?> I get the following output underneath from $string: <p>Testing this to get the first image out</p> <p><img height="150" alt="" width="200" src="/OO_online/image/Dock.jpg" /><img height="150" alt="" width="200" src="/OO_online/image/Humpback%20Whale.jpg" /></p> <p> </p> <p> </p> Notice: Undefined offset: 1 in C:\inetpub\wwwroot\OO\news_view.php on line 59 Line 59 is the echo $imgsrc[1]; line. Quote Link to comment https://forums.phpfreaks.com/topic/165982-solved-trying-to-get-the-first-image-out-of-text-field/#findComment-875737 Share on other sites More sharing options...
.josh Posted July 15, 2009 Share Posted July 15, 2009 Ah I know what the problem is. I think your $news_page_row->getField('text') method is using htmlentities. Rightclick > viewsource your echoed string to confirm. If so, do this: $string = $news_page_row->getField('text'); $string = html_entity_decode($string); preg_match('~<img[^>]*src\s?=\s?[\'"]([^\'"]*)~i',$string,$imgsrc); echo $imgsrc[1]; Quote Link to comment https://forums.phpfreaks.com/topic/165982-solved-trying-to-get-the-first-image-out-of-text-field/#findComment-875750 Share on other sites More sharing options...
Jalz Posted July 15, 2009 Author Share Posted July 15, 2009 Thanks CV, Thats working, should've realised as I'm using html_entity_decode() to display all my other fields on a layout - I thought the putting in the raw value into $string would work. J P.S How do I mark as Solved? Quote Link to comment https://forums.phpfreaks.com/topic/165982-solved-trying-to-get-the-first-image-out-of-text-field/#findComment-875756 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.