PHP Echoing Angle Brackets

php echoing angle brackets

A "page" is written in HTML, so < means "Start a tag".

You have to represent characters with special meaning in HTML using entities.

You can write them directly, or make use of the htmlspecialchars function.

echo "<sometext>";
echo htmlspecialchars("<sometext>");

Angle brackets in php

There are two ways of embedding images in an email:

  1. As external references: <img src="http://..."; and
  2. Embedded in the email.

(1) is I guess what you're trying to do. Many mail programs will (rightly) block such images as they are used by spammers to test email accounts for liveness.

It's better to do (2). See PHP Email: Using Embedded Images in HTML Email. Basically you create a multi-part MIME email (so you can attach HTML and the images) and then reference them by cid instead of a true URL. These are far less suspicious to any mail program that receives them but obviously result in bigger emails (as the image might be sent 100,000 times instead of just sending a URL).

Get text between and outside angle brackets in PHP

You may use your pattern (or a bit modified version) to preg_split the string and get an array with the 2 values:

$s = 'Jane Doe <jane.doe@example.com>';
$res = preg_split('/\s*<([^>]*)>/', $s, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
print_r($res); // => Array ( [0] => Jane Doe [1] => jane.doe@example.com )

See the PHP demo

The \s*<([^>]*)> pattern finds 0+ whitespaces (\s*) followed with <, then captures any 0+ chars other than > into Group 1 (with [^>]*) and then matches >. The PREG_SPLIT_DELIM_CAPTURE flag makes preg_split keep the submatch (Group 1 value) in the resulting array. PREG_SPLIT_NO_EMPTY flag will discard any empty items that can occur at the beginning or end. The -1 limit argument will return all split chunks (no limit).

There is also a matching solution, and the one I'd suggest is with the named capturing groups:

$s = 'Jane Doe <jane.doe@example.com>';
if(preg_match('/^(?<name>.*\S)\s*<(?<email>.*)>$/', $s, $m)) {
echo $m["name"] . "\n";
echo $m["email"];
}

See this PHP demo and the regex demo.

Pattern details

  • ^ - start of string
  • (?<name>.*\S) - Group "name": any 0+ chars up to the last non-whitespace char followed with...
  • \s* - 0+ whitespace chars
  • < - a < char
  • (?<email>.*) - Group "email": any 0+ chars, as many as possible up to the
  • >$ - > at the end of the string.

Right angle brackets cause my php tag in html to end prematurely

Your server doesn't process html files as php. You can either add php handler to your apache configuration for html files by adding AddType application/x-httpd-php .html in mime_module, and restart apache, or you can rename your html file to php, which is better imho.

PHP cutting off angle brackets when triggered inside function

Instead of

echo $variable;

use

echo htmlentities($variable);

angle bracket mysql php

The output of from_addr will be there if you are receiving results from your query.

Take a look at this example:

$string = '<hello world>';
echo $string;
echo htmlspecialchars('<hello world>');

Whereas, echo $string will show in the source code, but is being treated as a tag, and therefore not display "on screen".

Using the htmlspecialchars() will change < > to < > which will show "on screen".

It appears that you are using this field for email, so I would NOT recommend saving to the database with htmlspecialchars, as you want the emails to send properly.

If the above didn't answer your question fully, and you wish to query the database for an email rather than the "send name", I would recommend using LIKE:

SELECT * FROM `msgs` WHERE `from_addr` LIKE '<%INSERT_EMAIL_ADDRESS%>'.

Hope this helps.



Related Topics



Leave a reply



Submit