PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > single quote in string constant
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
single quote in string constant

Réponse
 
LinkBack Outils de la discussion
Vieux 02/12/2007, 19h41   #1
MangroveRoot
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut single quote in string constant

I've looked in various manuals and tutorials,
and they cover the obvious situations, but not this one:

At the very top of my .php file, I define a variable as follows:
======
$title = "Doesn't Matter";
------
Note the apostrophe (or, single quote) in the string value.

Further down, in the <HEAD>...</HEAD>, I have the following:
======
<?php
echo "<TITLE>";
echo $title;
echo "'</TITLE>";
?>
------
If I bookmark the page, the name is what I hoped: "Doesn't Matter".

Further down, in the <BODY>...</BODY>, I have the following:
======
<?php echo "
<H1><IMG SRC='foo.gif' ALT='$title'></H1>
"; ?>
------
This fails, producing only "Doesn" in whatever format corresponds to H1.
I'm guessing that's because the singlequote in the variable
is somehow interacting with the singlequotes in the echoed string.

However, if I try this:
======
<?php echo '
<H1><IMG SRC="_images/_rock/Zacs World.gif" ALT="$title"></H1>
'; ?>
------
(which I perhaps prefer,
because the HTML looks the way I would like it to look)
this also fails, producing "$title" in whatever format corresponds to H1.

Is there some way I can "escape" or "quote" the singlequote in the variable
so that it will just be taken as a value (I guess)
rather than as something to be concatenated with what's around it
and thus interacting with the quotes around it?

I've tried doubling it and prefacing it with backslash,
but that just displays (if at all) as the same thing
only with an extra quote or with a backslash.
  Réponse avec citation
Vieux 02/12/2007, 20h12   #2
Ian Hobson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: single quote in string constant

MangroveRoot wrote:
> I've looked in various manuals and tutorials,
> and they cover the obvious situations, but not this one:
>
>
>
> Is there some way I can "escape" or "quote" the singlequote in the variable
> so that it will just be taken as a value (I guess)
> rather than as something to be concatenated with what's around it
> and thus interacting with the quotes around it?

Indeed there are many ways to do what you want.

I won't repeat the manual here - it does a reasonable job of explaining
the options - its just that there are so many it gets complicated.

What you need is either to escape the double quotes, and let the double
quote processing convert $var into its value. Here you echo a single
processed string, thus.

<?php echo "
<H1><IMG SRC=\"_images/_rock/Zacs World.gif\" ALT=\"$title\"></H1>
"; ?>

Or you can use single quotes, and the concatenation operator dot (.)
which joins the strings it appears between. This means you echo the join
of three strings thus.

<?php echo '
<H1><IMG SRC="_images/_rock/Zacs World.gif" ALT="'.$title.'"></H1>
'; ?>

Regards

Ian


  Réponse avec citation
Vieux 02/12/2007, 20h22   #3
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: single quote in string constant

MangroveRoot wrote:
> I've looked in various manuals and tutorials,
> and they cover the obvious situations, but not this one:
>
> At the very top of my .php file, I define a variable as follows:
> ======
> $title = "Doesn't Matter";
> ------
> Note the apostrophe (or, single quote) in the string value.
>
> Further down, in the <HEAD>...</HEAD>, I have the following:
> ======
> <?php
> echo "<TITLE>";
> echo $title;
> echo "'</TITLE>";
> ?>
> ------
> If I bookmark the page, the name is what I hoped: "Doesn't Matter".
>
> Further down, in the <BODY>...</BODY>, I have the following:
> ======
> <?php echo "
> <H1><IMG SRC='foo.gif' ALT='$title'></H1>
> "; ?>
> ------
> This fails, producing only "Doesn" in whatever format corresponds to H1.
> I'm guessing that's because the singlequote in the variable
> is somehow interacting with the singlequotes in the echoed string.
>
> However, if I try this:
> ======
> <?php echo '
> <H1><IMG SRC="_images/_rock/Zacs World.gif" ALT="$title"></H1>
> '; ?>
> ------
> (which I perhaps prefer,
> because the HTML looks the way I would like it to look)
> this also fails, producing "$title" in whatever format corresponds to H1.
>
> Is there some way I can "escape" or "quote" the singlequote in the variable
> so that it will just be taken as a value (I guess)
> rather than as something to be concatenated with what's around it
> and thus interacting with the quotes around it?
>
> I've tried doubling it and prefacing it with backslash,
> but that just displays (if at all) as the same thing
> only with an extra quote or with a backslash.


This isn't a PHP problem. Look at your page source and you'll see why
it's occurring.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  Réponse avec citation
Vieux 02/12/2007, 20h37   #4
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: single quote in string constant

MangroveRoot wrote:

> ======
> <?php
> echo "<TITLE>";
> echo $title;
> echo "'</TITLE>";
> ?>

This should result in a title: Doesn't Matter'
Not what you really wanted, just drop the single quote at the end tag of title.

> Further down, in the <BODY>...</BODY>, I have the following:
> ======
> <?php echo "
> <H1><IMG SRC='foo.gif' ALT='$title'></H1>
> "; ?>
> ------
> This fails, producing only "Doesn" in whatever format corresponds to H1.
> I'm guessing that's because the singlequote in the variable
> is somehow interacting with the singlequotes in the echoed string.


This for the web browsers parser will see the single quote in your string as
the end for the alt-option and the "t Matter'" as unknown options.
You can use double quotes for the tag options, there are still some
proletarian browsers which has trouble with single quoted tag options.

You seem to be over using the echo function too, why not just

<h1><img src="foo.gif" alt="<?PHP echo $title; ?"></h1>

of course you can use short tags for the php, but these may be removed in php6.


> However, if I try this:
> ======
> <?php echo '
> <H1><IMG SRC="_images/_rock/Zacs World.gif" ALT="$title"></H1>
> '; ?>
> ------


there is a difference between
echo '$omthing';
and
echo "$omething";

When you use single quotes around a string you want to echo, you tell php to
not parse the string at all, just echo it as it is. Double quotes tells that
you want the string to be parsed and variables replaced with the value.

even if
echo "$omething";
works, I would recommend you to use
echo "{$omething}";
as this way you will have less trouble that php parses the wrong variables
(wrong from your point of view).


> Is there some way I can "escape" or "quote" the singlequote in the variable
> so that it will just be taken as a value (I guess)
> rather than as something to be concatenated with what's around it
> and thus interacting with the quotes around it?


It's not a php problem, but the HTML syntax and as it don't take care of
escapes for quotes, you won't be able to do what you want to do, you need to
change the method you are using to a more sane one.


--

//Aho
  Réponse avec citation
Vieux 02/12/2007, 21h37   #5
Allodoxaphobia
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: single quote in string constant

On Sun, 02 Dec 2007 19:41:26 GMT, MangroveRoot wrote:
> I've looked in various manuals and tutorials,
> and they cover the obvious situations, but not this one:
>
> At the very top of my .php file, I define a variable as follows:
>======
> $title = "Doesn't Matter";


Not tested:

$title = "Doesn&apos;t Matter";
  Réponse avec citation
Vieux 02/12/2007, 23h09   #6
MangroveRoot
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: single quote in string constant

Allodoxaphobia wrote:
>>
>> At the very top of my .php file, I define a variable as follows:
>> ======
>> $title = "Doesn't Matter";

>
> Not tested:
>
> $title = "Doesn&apos;t Matter";


This s a great deal.
A *little* ugly, but only ugly in one place, not *everywhere*.
The explanation (as I see it) is that this ensures that
the punctuation mark indicating the contraction of "does not"
is an apostrope (which, grammatically, it's *supposed* to be)
and not just a singlequote
(which *happens* to look the same in most, if not all, fonts).

I then went on to use this:
======
<?php echo "
<H1><IMG SRC='_images/_rock/Zacs World.gif' ALT='{$title}'></H1>
"; ?>
------
and it worked perfectly, demonstrating that the apostrophe
is not mistaken for a singlequote, and the two do not interact.

I went on further to use this:
======
<?php echo "
<H1><IMG SRC=\"foo.gif\" ALT=\"{$title}\"></H1>
"; ?>
------
This way, the *generated* HTML has the more pleasing
(and, I gather, more backward-compatible) format.
The business of escaping all the doublequotes inside the quoted string
*does* get old after a while -- tiresome to do, prone to error, and ugly --
but it seems to be what most scripting languages do.

(Perl, sed, TECO, and probably others
allow the programmer to specify an alternate character
to quote the entire string, so that ordinary single *or* double quotes
may be used inside the string with impunity.)
  Réponse avec citation
Vieux 02/12/2007, 23h11   #7
MangroveRoot
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: single quote in string constant

MangroveRoot wrote:
> At the very top of my .php file, I define a variable as follows:
> ======
> $title = "Doesn't Matter";
> ------
> (. . .)
> Is there some way I can "escape" or "quote" the singlequote in the variable
> so that it will just be taken as a value (I guess)
> rather than as something to be concatenated with what's around it
> and thus interacting with the quotes around it?
> (. . .)


BTW, which of these groups --
alt.comp.lang.php, alt.php, comp.lang.php --
is the correct one for questions of this sort?
  Réponse avec citation
Vieux 03/12/2007, 00h16   #8
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: single quote in string constant

MangroveRoot wrote:
> MangroveRoot wrote:
>> At the very top of my .php file, I define a variable as follows:
>> ======
>> $title = "Doesn't Matter";
>> ------
> > (. . .)
>> Is there some way I can "escape" or "quote" the singlequote in the
>> variable
>> so that it will just be taken as a value (I guess)
>> rather than as something to be concatenated with what's around it
>> and thus interacting with the quotes around it?
> > (. . .)

>
> BTW, which of these groups --
> alt.comp.lang.php, alt.php, comp.lang.php --
> is the correct one for questions of this sort?
>


None of them. This is an HTML problem.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 08h01.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 1,52694 seconds with 16 queries