Jump to content

Recommended Posts

I just don't know PHP! I'm trying to learn, but its slow going.

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:

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

}[/code]

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

[code]<img src="http://www.xxx.com/wp-content/uploads/2007/01/593761.jpg" title="Man of the Year 1980" alt="Man of the Year 1980" align="right" height="250" width="170" />[/code]

I use this call:

[code]$image_src = get_between ($img_text, 'width="', '"');[/code]

Then, this:

[code]list ($img_width) = $image_src;[/code]

(I'm not sure why this is used above, it was in the code to begin with)

Then finally this:

[code]$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_width.'px; max-height: 400px; height:auto !important;">';[/code]

(where $img_width) should be replaced by 170 in the outputted code.

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

[code]<div id="img_lgnd" style="position: relative;  font-size: ; border: px none ; margin: 1em; padding: 0em 0em 0em 0em; float: right; max-width: 1px; max-height: 400px; height:auto !important;">[/code]

The width is 1px, 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!
Link to comment
https://forums.phpfreaks.com/topic/36230-noob-here-this-has-got-to-be-easy-but/
Share on other sites

Thanks for the reply!

I thought that line was the problem as well, but its not the case. I comment it out, switch the variables, and the output HTML I get is:

[code]<div id="img_lgnd" style="position: relative;  font-size: ; border: px none ; margin: 1em; padding: 0em 0em 0em 0em; float: right; max-width: px; max-height: 400px; height:auto !important;">[/code]

No 1, now it is just empty: max-width: px;
ok can you confirm for me in that case, that the line that contains the code is actually in $img_text.  As your code doesn't say that!

You have this...
[code=php:0]<img src="http://www.xxx.com/wp-content/uploads/2007/01/593761.jpg"....[/code]

I'd expect it to say this...
[code=php:0]$img_text = '<img src="http://www.xxx.com/wp-content/uploads/2007/01/593761.jpg"....';[/code]

Regards
Huggie
I believe that's how it works. The text for getting the variable $img_text is:

[code]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);[/code]
Yeah, just my thought. Here it is from the beginning up to the <div> call in question. (that get_between) function appears right after:

[code]// 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;">';
[/code]
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.