Use "<" and ">" in a string in php
I have a section in my code looking like this:
$accounts[] = array("id" => 1, "fullName"=>"Lorem ipsum", "email"=>"lorem@example.com");
$accounts[] = array("id"开发者_JAVA技巧 => 2, "fullName"=>"Lorema ipsum", "email"=>"lorema@example.com");
$toUsers = array();
foreach($accounts as $account){
$toUsers[] = $account['fullName'] . "<" . $account['email'] . ">";
}
$toString = implode(", ",$toUsers);
This loop is supposed to give me a string, formatted like an email header (I want to use the mail function).
I expect the result to be $toString = "Lorem ipsum<lorem@example.com>, Lorema ipsum<lorema@example.com>";
but all I get is $toString = "Lorem ipsum, lorema Ipsum";
What do I need to do to get "<" and ">" working with a string in php?
Thanks, JNK
Outputting it to an HTML document are we? Chances are your browser's interpreting it as markup. Check the view->source and see if it's there.
Nothing at all. They have no special significance in PHP.
If you are using you PHP to generate HTML, on the other hand, then they will have special significance in the outputted HTML document. Use htmlspecialchars()
.
e.g.
$toString = htmlspecialchars(implode(", ",$toUsers));
Best practice is to convert data for presentation as HTML in this way at the point where you insert the into the HTML document (and not before).
The code you posted works exactly as expected. You've probably fixed the mistake by reducing your original code to this post.
精彩评论