views:

788

answers:

8

I work on a database driven website. Most pages' titles are the names of partners we have, and served from a database using php.

For example one page might be called "Experian". The problem is that some of the values for partner names don't work because the values are things like

  $partnername22 = Discover<sup>tm</sup> Magazine

I can't use this for a title because superscript can't show up in a title, and I can't change the value because many other things depend on it. Is there a trick I can pull to make the browser ignore the superscript tags if they are in the title?

+1  A: 

Not AFAIK, but if you're already scripting out a string from the DB, why can't you script out the same string regex replacing tags (or tag pairs, as you wish) with empty strings?

annakata
+1  A: 

In a word, no.

Ideally you would do $partnername22 = strip_tags('Discover<sup>tm</sup> Magazine'); (or something fancier), but if you can't to that then I'm afraid you're stuck with a Javascript solution.

Something like:

window.title = window.title.replace(/<sup.*?</sup>/, '');
Greg
+1  A: 

Regular Expression in PHP that will replace <sup>text</sup> with (text):

$title = preg_replace('|<sup>(.*?)</sup>|', '\($1\)', $title);

It would be similar to do this in javascript.

Mike
In response to Bryan (I can't respond to his for lack of points) I did mean to include the parans so that the result has something distinguishing the text. But the slashes should be removed. So the result looks like:Discover(tm) MagazineIt wouldn't look right if it was:Discovertm Magazine
Mike
Ah cool, thanks for clarifying. I didn't understand what you had been trying to do there. Cheers.
bryan kennedy
+2  A: 

It would be very simple (and probably advisable) to use PHP's strip_tags() command to remove all of the html from your string before posting it as a title.

bryan kennedy
+1  A: 

Mike, I think you meant this:

$title = preg_replace('|<sup>(.*?)</sup>|', '$1', $title);

The way you had it before prints some slashes and parenthesis around the tagged word. Right?

pg, you could also just do

$title = preg_replace('|<sup>(.*?)</sup>|', '', $title);

if you wanted to completely remove the superscripted info from the title.

bryan kennedy
I don't understand the usage here. How do I use this $title value to remove the supercripted text? now the text of the document is: <title><?php echo $offer22name ?>is a magazine</title> Where do I put the $title value and what do I do with it to make it take out everything in the tags?
pg
@pg, bryan meant that $partnername22 == $title. He reworded it in his post to make it clearer that you are editing the Web page's title, and not the partner's name.
strager
+1  A: 

For:

<title><?php echo $offer22name ?>is a magazine</title>

change to:

<title><?php echo preg_replace('|<sup>(.*?)</sup>|', '($1)', $offer22name) ?> is a magazine</title>

But like Bryan mentioned, you should use PHP's strip_tags() in this scenario. So:

<title><?php echo strip_tags($offer22name) ?> is a magazine</title>
Mike
In case you miss the other comment I made, The regexp I used would result in: Discover(tm) Magazine.
Mike
Thank you to everyone who has helped me!
pg
+2  A: 
<title><?PHP echo str_replace('<sup>tm</sup>', '&trade;', $offer22name); ?> is a magazine</title>

Or, more safer:

<title><?PHP echo strip_tags(str_replace('<sup>tm</sup>', '&trade;', $offer22name)); ?> is a magazine</title>

Or, catching more cases (not sure on this one exactly):

<title><?PHP echo strip_tags(preg_replace('/<sup.*?>\s+[Tt][Mm]\s*</(sup|)>', '&trade;', $offer22name)); ?> is a magazine</title>

You can replace the '&trade;' with '&#8482;', '&#x2122;', or '™' if you wish (but make sure encodings match for the last one), depending on the situation.

strager
A: 

How about the simple expedient of some CSS?

h2.title sub, h2.title sup { vertical-align: baseline; font-size:100%; }
garrow