Forum Moderators: coopster

Message Too Old, No Replies

string replace for bold tag

         

phpguy54

3:17 pm on Jun 2, 2008 (gmt 0)

10+ Year Member



Hey guys i'm trying to figure how to make text appear bold in an email with the string replace.

For example,

If someone submits <b>text<b> in the module i made, the bold tags need to be replaced with something to show up in a plain text email since the <b> tags only show up in a html email.

Does anybody know what the bold tag is for a plain text email?

I tried <strong> but that didn't work.

Thanks

dreamcatcher

6:58 am on Jun 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<strong> is an HTML tag, so if the e-mail isn`t HTML formatted, it won`t render.

I`m not sure if what you are asking is even possible.

dc

deMorte

12:56 pm on Jun 3, 2008 (gmt 0)

10+ Year Member



I found a site that said the following:
"To use bold face in plain text email messages: Use asterisks at the beginning and at the end of *the passage you want to put in bold face*. Some email clients will even use a bold face to display such passages."

The site can be found by googling: plain text email bold.

eelixduppy

3:53 pm on Jun 3, 2008 (gmt 0)



Haven't heard of that before, but if it's true, then a simple str_replace [php.net] should do the trick. I would got the HTML email route, though. Just remember to set the headers correctly.

phpguy54

6:26 pm on Jun 3, 2008 (gmt 0)

10+ Year Member



Thanks, unfortunately i can only use plain text formatting since some users request non-html emails.

So i have to make this thing work with plain text. Here is what my str_replace looks like:

$formating = array( "<br>", "<b>");
$html = array("", "*");
$message = "";

$body .= str_replace($formating, $html, $message);

The message is pulled from a table in a database and i fetch this through a db query.

The <br> as you can see above works..the output is just a space

However, when i replace the <b> with a * the output is in the literal format like this: *text*

In other words the output is what you see above not a bolded word

Receptional Andy

6:32 pm on Jun 3, 2008 (gmt 0)



Some email clients (e.g. thunderbird) will bold *text* and a few other bits of 'plain text formatting' when the recipient views the email. If nothing else, it does emphasise those words in plain text anyway, regardless of client.

phpguy54

6:41 pm on Jun 3, 2008 (gmt 0)

10+ Year Member



How would the tokenizer method work?

Receptional Andy

6:47 pm on Jun 3, 2008 (gmt 0)



To state the obvious (sorry!) plain text is only ever going to output plain text (with the exception of a few client tricks like the asterisk for bold). If you want formatting, you need html email, I'm afraid.

phpguy54

8:28 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



ok well thats what i need help with. The asterisks and trying to get text to become bold. As i stated when i do a str replace or even this:

$token= strtok($message, "<b>");
$tmp = "";
while ($token !== false) {
$tmp .= "*";
$tmp .= $token;
$tmp .= "*";
$token = strtok("<b>");
}

The output looks like this: *text* instead of the actual word "text" being bold.

Receptional Andy

8:34 pm on Jun 4, 2008 (gmt 0)



Perhaps I've misunderstood, and apologies if so.

If you send an email with *text* in it, and open it up in, say, Mozilla Thunderbird, it will appear as text. It will always be *text* in the email you create and the output of your function, but for a small percentage of recipients with certain email clients, they get an actual bold text effect.

If you want recipients using, say, Outlook to see bold text, you need to send email to them in HTML format. In plain text, they will always see *text* within the email they receive, no matter what you do.

phpguy54

8:40 pm on Jun 4, 2008 (gmt 0)

10+ Year Member



thanks, so even though the asterisks make some plain text bold, it is very limited as far as email clients are concerned? appreciate the help

Receptional Andy

8:43 pm on Jun 4, 2008 (gmt 0)



It would require testing a few email clients to get a good idea of support, but as far as I'm aware Outlook and Outlook Express are the most widely used, and neither will bold *text* style formatting.

A side issue is that I actually use an asterisk to *emphasise* words in plain text anyway, so it could achieve it's purpose even if the text isn't made into bold ;)

PHP_Chimp

8:49 pm on Jun 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The *bold* text is very limited in support. As plain text is generally exactly that...plain, no formatting, no bold, italic, larger, smaller...just text.

As has been said there are a few clients that allow some small tricks to add formatting to plain text. The *'s will highlight a section of text even if the client doesnt display it in bold, so that is still a valid way to highlight text.

If you want proper formatting then you need to look at an HTML email. As that is the only real way you can guarantee that your format will be obeyed.
You can use very basic formatting, like line breaks, in plain text. Other than that I wouldnt rely on any proprietary formatting that may or may not be on the client.

You could always get your clients to let you know if they want plain text or html emails. Then you can do your fancy formatting for the html stuff.