homepage Welcome to WebmasterWorld Guest from 54.198.94.76
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
substr with echo <<<END
doubleJ




msg:4344256
 9:49 pm on Jul 26, 2011 (gmt 0)

Hello...
I'm just starting to work with echo <<<END and am having a bit of a problem getting substr() to pass correctly.
I have this xml...

<item>
<guid>http://www.moorelife.org/rss/MarriageEnrichment2011.xml</guid>
</item>

I have this php...

echo <<<END
<img src="images/ProductCovers/{substr($item->guid,29,-4)}.jpg" />
END;

The resultant source code is...

<img src="images/ProductCovers/{substr(http://www.moorelife.org/rss/MarriageEnrichment2011.xml,29,-4)}.jpg" />

Obviously, that's not what I want.
I'm trying to get src="images/ProductCovers/MarriageEnrichment2011.jpg" />
I've also tried ".substr." and it didn't fix it.
I can do echo "bleh".substr."bleh"; and it works just fine.
The only reason that I'm using <<<END is to simplify my html within the echo.
Feel free to recommend other options, too. Today is my first day working with php and xml, together.
JJ

 

penders




msg:4344279
 11:05 pm on Jul 26, 2011 (gmt 0)

PHP's variable parsing [am.php.net] ability in double quoted and HEREDOC strings (as you are using above) unfortunately does not extend to parsing all PHP syntax, just variables. So you would need to assign your statement to a variable first and use that...


$filename = basename($item->guid,'.xml');
echo <<<END
<img src="images/ProductCovers/$filename.jpg" />
END;


basename() [am.php.net] would seem to be a better suited function in your case, than substr().

doubleJ




msg:4344287
 11:37 pm on Jul 26, 2011 (gmt 0)

I'll have to look at basename(). I haven't seen that, before.
It's entirely possible that I could be more efficient.
Hehehe...
JJ

doubleJ




msg:4344488
 2:04 pm on Jul 27, 2011 (gmt 0)

That worked like a charm and I like basename().
That will help a lot.
Thanks...
JJ

doubleJ




msg:4344522
 3:44 pm on Jul 27, 2011 (gmt 0)

How do you .PHP_EOL with HEREDOC?
This works...

echo <<<END
<div>stuff</div>\n
END;

And this works...

echo "<div>stuff</div>".PHP_EOL;

But this doesn't...

echo <<<END
<div>stuff</div>.PHP_EOL
END;

And neither does this...

echo <<<END
<div>stuff</div>
END.PHP_EOL;

JJ

penders




msg:4344549
 4:33 pm on Jul 27, 2011 (gmt 0)

I don't think you can include global constants with HEREDOC syntax. It will be treated as a literal string eg. "PHP_EOL". It's always looking for a '$' as the first char, as for variables. You need to assign it to a variable IMO.

However, be careful....
echo <<<END 
one\n
two\n
three\n
END;


Lines 'one' and 'two' automatically have newlines appended to the end of the lines, as they appear in the source code. So the first two lines will have 2 newlines appended to the end. ie. Double spaced.

doubleJ




msg:4344607
 5:58 pm on Jul 27, 2011 (gmt 0)

Yes, I've seen that, already.
Thanks for the input.
I keep looking at the source to make sure it's the way I want.
I looked at the source for google's website and I realized how bad automated code could look.
Hehehe...
JJ

penders




msg:4344668
 8:07 pm on Jul 27, 2011 (gmt 0)

You should also perhaps look at sprintf() [am.php.net] which you can combine with the HEREDOC syntax.

penders




msg:4348418
 9:25 pm on Aug 5, 2011 (gmt 0)

This has just cropped up again in something I'm working on....

In your original post you are attempting to execute a global function as part of a variable parsed string. My suggestion was to assign this to a variable and use that instead. Whilst this is still correct, you can actually call functions using complex syntax (in PHP 5), but the result is probably not what you'd expect. The output of that function is treated as a variable and that variable is parsed into the resulting string (like variable variables [am.php.net]), which is of limited use IMO.

However, you can call methods of class instances and these are parsed as expected. eg.
<?php 
class DummyClass {
public function exampleMethod() {
return base64_decode('SGVsbG8gV29ybGQ=');
}
}
$instance = new DummyClass();
// Outputs "...: Hello World"
echo "Class instance method output: {$instance->exampleMethod()}";
?>

doubleJ




msg:4348611
 2:18 pm on Aug 6, 2011 (gmt 0)

Hehehe...
That's some nify code.
That might be a little out of my pay grade.
I did understand the "Hello World" part.
JJ

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved