Jump to content

[SOLVED] Trying to get the first image out of text field


Jalz

Recommended Posts

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>&#160;</p>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]; 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.