www.webdeveloper.com
Results 1 to 3 of 3

Thread: Perl - Refining content

  1. #1
    Join Date
    Jun 2008
    Posts
    219

    Perl - Refining content

    How do I get the first image in a record?

    For instance:
    Code:
    ### the record
    10|My Title|This is my text content<br><br>Another paragraph with lotsa words<br><br>Even more text and images may follow.<br><img src="/images/one.jpg"><br><img src="/images/two.jpg"><br><br>etc-etc-etc.
    
    ### So I have the content into an array, fine no probem.
    @content= split (/\s+/, $bc[2]);
    $mytext = join(" ", @content[0..12]); # gets first 12 words of content text (works great)
    
    ### how do I get one.jpg, and have it in front of the $mytext?
    ### and if no images present in record, the just show the text.
    ### (CSS will handle sizing just for this show of the image, so will need room to do that).
    print qq ~$mytext~;
    Can find nothing about this anywhere. Want to avoid using a module on this, if possible.

    Would it be a good idea to assign an id to the first image when I do the entry? That way I could look for the image with the id and ignore all the rest. Just thinking as I'm typing.

  2. #2
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    387
    Code:
    my ($before_img1, $img1, $after_img1) = $bc[2] =~ /(.*?)(<\s*img\b[^>]*>)(.*)/;

  3. #3
    Join Date
    Jun 2008
    Posts
    219
    That works. I managed to resize the image (by changing style class) and it works in the browsers I've got - IE, Chrome (Safari), Ffx.

    This is my final:
    Code:
    @content= split (/\s+/, $bc[2]);
    my ($before_img1, $img1, $after_img1) = $bc[2] =~ /(.*?)(<\s*img\b[^>]*>)(.*)/;
    $mytext = join(" ", @content[0..12]);
     if ($img1 =~ s/class\=\"mim\"/class=\"tzim\"/) {print qq ~$img1 $mytext..... ~;}
      else {print qq ~$mytext..... ~;}
    Entries without images work as normal. Just means taking care about which my first image is.

    I assume the before and after variables are reserved words in Perl under these circumstances.

    This has been a brain killer for some weeks to achieve, I would never have thought of doing it this way.

    Thank you very much Sixtease.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles