Beginner here...I just can't figure this out!


#1

Here’s the situation. I’ve got a wordpress plugin running, but I am having to fix some of the bugs myself. Basically, I’m trying to pull am image width from the wordpress outputted HTML code and insert it into the PHP code. Here’s what I’ve got:

There is a function get_between that is supposed to get some text between certain characters I specify:

[php]function get_between ($input_text, $left_limit, $right_limit) {
$ll = strpos($input_text, $left_limit) + strlen ($left_limit);
$text1 = substr($input_text, $ll);
$rl = strpos($text1, $right_limit);
$text2 = substr($text1, 0, $rl);
return $text2;

}[/php]

To get the characters I want (the width) from this image tag (I want 170 outputted):

[php]Man of the Year 1980[/php]

The code is supposed to look in that image tag, grab the characters between what I specify, and then input it into a HTML div tag, the max-width selector. Here is that code:

[php]// Some global variables
$current_settings = get_option(‘gg_alttolgnd_options’);
$img_open = ‘<img’;
$img_close = ‘>’;

/**

  • This will search the text, look for the alt description within the

  • img HTML tag and duplicate it after the IMG tag

  • @param $data string The content of the post.

  • @return string The new content with alttolgnds generated.
    */
    function gg_alttolgnd($data) {
    global $img_open, $img_close, $current_settings;

     $div_close = '</div>';
     $img_incipit ='<img style="max-width: 300px; max-height: 280px; height:auto !important; "';
     $text_style = '<p style="text-align:'.$current_settings['text_align'].'; font-style: italic; font-family: Arial, sans; font-size: 11px; color: #A4B7D0;" />';
     $counter = 0;
    
     // Use Alt or Title?
     if ($current_settings['use_title'] == true) {
     $lgnd_open = 'title=';
     } else {
     $lgnd_open = 'alt=';
     }
     $lgnd_close = '"';
     $lgnd_open_len = strlen($lgnd_open);
     $lgnd_close_len = strlen($lgnd_close);
    

if (substr_count($data, $img_open)) {

// Look for IMG tags, then for ALT within and create the legend
while($img_start = strpos($data, $img_open) AND ($counter < (substr_count($data, $img_open)+1))) {
    //extract the string containing the whole img tag
    $img_text = substr($data, $img_start);
    $img_end = strpos($img_text, $img_close);
    $img_text = substr($img_text, 0, $img_end+1);

    // extract the src of the image and set the width of the box as big as the image width
    $image_src = get_between ($img_text, 'width="', '"');

    // set floating left, right or according to the align parameter
    if ((substr_count($img_text, 'align=') > 0) AND ($current_settings['image_float'] == 'align')) {
    $image_align = get_between ($img_text, 'align="', '"');
    } elseif ((substr_count($img_text, 'align=') == 0) AND ($current_settings['image_float'] == 'align')) {
    $image_align = 'left';
    } else {
    $image_align = $current_settings['image_float'];
    }

    // this solves the URL file-access problem by taking away the sitename URI if image
    // is hosted locally
    $site_name = 'http://' . get_settings('siteurl') . '/';
    if (substr_count($image_src, $site_name) > 0) {
    $image_src = str_replace ($site_name,'',$image_src);
    }

  //  list ($img_width) = $image_src;
    
    // Set the apperarance of the borders and the image according to user settings
    if ( $current_settings['use_border'] == false ) { $current_settings['border_style'] = 'none' ;}
    $div_open = '<div id="img_lgnd" style="position: relative;  font-size: '.$current_settings['font_size'].'; border: '.$current_settings['border_size'].'px '.$current_settings['border_style'].' '.$current_settings['border_color'].'; margin: 1em; padding: 0em 0em 0em 0em; float: '.$image_align.'; max-width: '.$img_src.'px; max-height: 400px; height:auto !important;">';[/php]

$img_src is supposed to be the 170 width that was grabbed.

However, this doesn’t work. The HTML output I get is:

[php]

[/php]

The width is px, obviously not right.

Any ideas? I’m sure this is a really easy issue to take care of, I’m just so new at this. Thanks!


#2

I copied the function and tried it myself and I think I know what is wrong. Try changing $image_src to:
[php]

<?php $image_src = get_between( $img_text, "width="", """ ); ?>

[/php]

I tried to do that with single quotes but it didn’t work, only with double quotes.


#3

Thanks for the help, but I already fixed it myself…seems I’m a bit better at PHP then I though! :)