johnnyjohnny Posted June 21, 2009 Share Posted June 21, 2009 Question: Say I have a include file with a function that produce html //include.php <?php function foo() { ?> <div id="hello"> hello </div> <?php } ?> and another file calls the foo() function //controller.php foo(); Is it possible to add slashes to the html produced by foo() in controller file? i.e. turn <div id="hello"> to <div id=\"hello\"> I tried to use addslashes(foo()), didn't seem to work thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/163156-php-html-function/ Share on other sites More sharing options...
.josh Posted June 21, 2009 Share Posted June 21, 2009 Why not just escape the quotes inside the function in the first place? But anyways, in order to add slashes, you have to put the html in a variable first. You can add the addslashes(...) inside your function but if you're going to do it there, then ^^ why not just hardcode that way in the first place. If you're wanting to do it outside of the function, you can do something like this: function foo() { $string = <<<STUFF <div id="hello"> hello </div> STUFF; return $string; } echo addslashes(foo()); but if you're going to be doing that all the time anyways, then we're back to square one, in that why not just hardcode it that way to begin with Quote Link to comment https://forums.phpfreaks.com/topic/163156-php-html-function/#findComment-860801 Share on other sites More sharing options...
pkedpker Posted June 21, 2009 Share Posted June 21, 2009 I have no idea what <<<STUFF means is that a new feature in php? using <<< 's? left shifts by the looks of it.. <?php //colors for all! but from what I know if you use double quotes like this to add more double quotes inside that you have to use a slash $test = "<someTag id=\"blahblah\">"; but if you use single quotes.. you will have no problems with HTML only with CSS.. $test = '<someTag id="blahblah">'; only bad thing about this is in double quotes you can add a variable right inside like this $blah = "blahblah"; $test = "<someTag id=\"$blah\">"; but in single quotes you can only do this. $blah = "blahblah"; $test = '<someTag id="'.$blah.'">'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/163156-php-html-function/#findComment-860813 Share on other sites More sharing options...
.josh Posted June 21, 2009 Share Posted June 21, 2009 I have no idea what <<<STUFF means is that a new feature in php? using <<< 's? left shifts by the looks of it.. heredoc Quote Link to comment https://forums.phpfreaks.com/topic/163156-php-html-function/#findComment-860814 Share on other sites More sharing options...
pkedpker Posted June 21, 2009 Share Posted June 21, 2009 I have no idea what <<<STUFF means is that a new feature in php? using <<< 's? left shifts by the looks of it.. heredoc I see now is it better to just do leave HTML without php scripting or using this? I mean yah either way its parsed Quote Link to comment https://forums.phpfreaks.com/topic/163156-php-html-function/#findComment-860821 Share on other sites More sharing options...
johnnyjohnny Posted June 21, 2009 Author Share Posted June 21, 2009 You guys are the best. @Crayon Violent: The reason why i don't want to hardcode " with \", which is what I did, I guess is because the html inside the foo() function would seem identical to its html counter-part. Again, thank you very much for your reply. Why not just escape the quotes inside the function in the first place? But anyways, in order to add slashes, you have to put the html in a variable first. You can add the addslashes(...) inside your function but if you're going to do it there, then ^^ why not just hardcode that way in the first place. If you're wanting to do it outside of the function, you can do something like this: function foo() { $string = <<<STUFF <div id="hello"> hello </div> STUFF; return $string; } echo addslashes(foo()); but if you're going to be doing that all the time anyways, then we're back to square one, in that why not just hardcode it that way to begin with Quote Link to comment https://forums.phpfreaks.com/topic/163156-php-html-function/#findComment-860825 Share on other sites More sharing options...
.josh Posted June 21, 2009 Share Posted June 21, 2009 I have no idea what <<<STUFF means is that a new feature in php? using <<< 's? left shifts by the looks of it.. heredoc I see now is it better to just do leave HTML without php scripting or using this? I mean yah either way its parsed If you're just echoing something out, I don't really see the point of using heredoc over just closing your php tags and reopening them after the html/text. Only time I really ever use it is if I have a block of html/text that I need to assign to a string for whatever reason, but I want to keep the formatting nice and pretty for code readability. Not that you can't technically do that with quotes, but a) using heredoc frees you from having to escape single and double quotes, and b) most text editors I have used seem to recognize syntax highlighting for heredoc, but not sysntax highlighting for multi-line single or double quoted things. In actual practice, I'd say 99% of the time I ever actually use it is for example, email body strings. Quote Link to comment https://forums.phpfreaks.com/topic/163156-php-html-function/#findComment-860856 Share on other sites More sharing options...
pkedpker Posted June 21, 2009 Share Posted June 21, 2009 haha sweet you learn something new everyday! Quote Link to comment https://forums.phpfreaks.com/topic/163156-php-html-function/#findComment-860861 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.