Question

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?

Was it helpful?

Solution

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>

OTHER TIPS

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.

<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.

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?

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>/, '');

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, 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.

How about the simple expedient of some CSS?

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

You can use character encoding instead of the tag, e.g. &trade;, &#153;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top