PHP Help Needed - How do I add dynamic alt tag to this code?

Discussion in 'PHP' started by muncle, Jul 7, 2008.

  1. #1
    I have a custom scripted forum and need to automatically add an alt tag to all images. The alt tag should be the same as forum title. This is what code in my commons.php that handles BBCode for images looks like:

    $patterns[] = "#\[img\]([^?](?:[^\[]+|\[(?!url))*?)\[/img\]#i";
    			$replacements[] = '<img src="\\1" border="0">';
    Code (markup):
    I need to add alt="$topicname" to it. How do I do that? Obviously, if I added this exact text string to the code, each image will have a static alt tag which will be this text string - $topicname. Can anyone help a newb out? How do I make this alt tag dynamic, so it shows the actual value of $topicname.

    Thank you ;)
     
    muncle, Jul 7, 2008 IP
  2. hermione_dies

    hermione_dies Peon

    Messages:
    309
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You have to concatenate the variable $topicname into the replacement.

    $replacements[] = '<img src="\\1" border="0" alt='" . $topicname . "'>';

    Something like that.. I always for get how PHP concatenation works.
     
    hermione_dies, Jul 7, 2008 IP
  3. muncle

    muncle Guest

    Messages:
    1,195
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Doesn't seem to work. This is what I get with the following:

    alt="' . $topicname . '" --> alt=""

    alt=" . $topicname . " --> alt=" . $topicname . "
     
    muncle, Jul 7, 2008 IP
  4. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try $replacements[] = '<img src="\\1" border="0" alt="' . $topicname . '">';
     
    RyanDoubleyou, Jul 7, 2008 IP
  5. hermione_dies

    hermione_dies Peon

    Messages:
    309
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Is the variable set?
    
    alt="' . $topicname . '" --> alt=""
    
    PHP:
    It looks like PHP evaluated the variable.. but it was empty so it returned a null value.
     
    hermione_dies, Jul 7, 2008 IP
  6. muncle

    muncle Guest

    Messages:
    1,195
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This is where newb n me gets in the way and I'm not sure I know what it is :eek:
     
    muncle, Jul 7, 2008 IP
  7. muncle

    muncle Guest

    Messages:
    1,195
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #7
    this is what I tried but it returns empty value. Obviously I'm missing something. I'm too much of a beginner to know what :eek:
     
    muncle, Jul 7, 2008 IP
  8. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Where exactly is your $topicname located? is it on the same file?
     
    RyanDoubleyou, Jul 7, 2008 IP
  9. hermione_dies

    hermione_dies Peon

    Messages:
    309
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Towards the top of the script add:

    
    $topicname = 'Test String';
    
    Code (markup):
    After that the alt tag of all the images should be "Test String." If it is that means the variable isn't being set by the script. If it isn't that means there is something wrong with the concatenation of the replacement array.
     
    hermione_dies, Jul 7, 2008 IP
  10. muncle

    muncle Guest

    Messages:
    1,195
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Good catch, no, it's not. I've been using that variable in another file and it worked there. What do I need to do?
     
    muncle, Jul 7, 2008 IP
  11. RyanDoubleyou

    RyanDoubleyou Peon

    Messages:
    86
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Try
    include("file that $topicname is in");
    PHP:
    Put that on your commons.php page at the top.
     
    RyanDoubleyou, Jul 7, 2008 IP
    muncle likes this.
  12. hermione_dies

    hermione_dies Peon

    Messages:
    309
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Are you sure you aren't using something like phpBB? I noticed you said you edited commons.php.. there is also a commons.php found in PHPBB3

    If it's custom you should be able to just ask the coder to define the $topicname variable in commons.php
     
    hermione_dies, Jul 7, 2008 IP
  13. muncle

    muncle Guest

    Messages:
    1,195
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #13
    You're absolutely right. I think I'm messing with something that's above my capabilities and probably should leave it, cause I don't know where to pull the dynamic content I need from. The alt tag is supposed to display the title of the thread, which it $topicname - as it works whenused in other files. Obviously my common.php file misses something that's why it's not working for me. After adding the code above, my images display an alt="Test String" tag :(
     
    muncle, Jul 7, 2008 IP
  14. muncle

    muncle Guest

    Messages:
    1,195
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #14
    It's a custom script and I could ask a coder who created it, but I thought I was gonna try to learn something on my own, but I'm failing miserably :(
     
    muncle, Jul 7, 2008 IP
  15. hermione_dies

    hermione_dies Peon

    Messages:
    309
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Yes, I would just contact the coder. It's not wise to start randomly including other files without knowing what they specifically do. (security/performance issues) Good luck :)
     
    hermione_dies, Jul 7, 2008 IP
    muncle likes this.
  16. muncle

    muncle Guest

    Messages:
    1,195
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Thanks a lot guys, I'm gonna leave it as I'm not up to par with the task I thought I was gonna do. You've been both helpful and green rep is on the way for the both of you. Keep rocking.

    BTW - Hermione didn't die.... Not yet :p
     
    muncle, Jul 7, 2008 IP