Jump to content

php html function


johnnyjohnny

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.'">';

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.