Welcome to WebmasterWorld Guest from 23.22.206.103

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

substr with echo <<<END

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

Junior Member

5+ Year Member

joined:Feb 15, 2011
posts: 53
votes: 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
11:05 pm on July 26, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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().
11:37 pm on July 26, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 15, 2011
posts: 53
votes: 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
2:04 pm on July 27, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 15, 2011
posts: 53
votes: 0


That worked like a charm and I like basename().
That will help a lot.
Thanks...
JJ
3:44 pm on July 27, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 15, 2011
posts: 53
votes: 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
4:33 pm on July 27, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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.
5:58 pm on July 27, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 15, 2011
posts: 53
votes: 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
8:07 pm on July 27, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


You should also perhaps look at sprintf() [am.php.net] which you can combine with the HEREDOC syntax.
9:25 pm on Aug 5, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 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()}";
?>
2:18 pm on Aug 6, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 15, 2011
posts: 53
votes: 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