SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast Claudek's Avatar
    Join Date
    Apr 2006
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Inserting alt attribute inside img tag

    Hello,

    I am trying to implement the following code but am finding that it does not work for some reason. It converts any <img> tags to XHTML 1.0 compliant format and adds an alt tag with a predefined value. I hope someone can point me in the correct part to modify/correct/replace please. $source contains the html code being fed to it.
    Code:
    Function convertToXHTML_img($source) {
      // Since <img> has not closing tag, remove all
      $newDescription = str_replace("</img>", "", "$source");
      
      // Use regular expressions to find <img ... >
      if (preg_match_all("/<img[^>]*>/i", $newDescription, $match) !== FALSE) {
        // Make sure it truely was a match
        for ($x = 0; $x < count($match[0]); $x++) {
          if ($match[0][$x] != "") {
            // Save the image
            $theImage = $match[0][$x];
            
            // If the tag isn't closed with />, make it so
            if (stristr(str_replace(" ", "", $theImage), "/>") === FALSE) {
              $theImage = str_replace(">", "/>", $theImage);
            }
            
            // Check for the alt="" tag
            if (stristr(str_replace(" ", "", $theImage), "alt=") === FALSE) {
              $theImage = str_replace("/>", " alt=\"$xyz\" />", $theImage);
            }
            
            // Remove the image from the description
            $newDescription = str_replace($match[0][$x], $theImage, $newDescription);
          }
        }
      }
      
      return $newDescription;
    Thanks,
    Claudek

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    What does "doesn't work" mean? No output at all? Or an output that isn't what you want?
    Can you give an example of the input, the output you're getting right now, and the output you want?

  3. #3
    SitePoint Enthusiast Claudek's Avatar
    Join Date
    Apr 2006
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't work means it is not adding the /> tag or adding the Alt attribute as it should in the code.

    I have tested it with pages where the html includes <img> tags and no alt tags but for some reason that code does not add the alt attribute for some reason. I am sure it is something simple in the code but I have been looking at it so long, I appear to be blinded to it.

    Quote Originally Posted by guido2004 View Post
    What does "doesn't work" mean? No output at all? Or an output that isn't what you want?
    Can you give an example of the input, the output you're getting right now, and the output you want?

  4. #4
    SitePoint Member
    Join Date
    May 2009
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it works...make sure you're actually setting $xyz


Bookmarks

Posting Permissions

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