Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically escaping quotation marks

Status
Not open for further replies.

EagleM

Programmer
Jan 17, 2007
31
0
0
US
I am trying to find a way to automatically escape (\) all quotation marks in a string with many quotation marks. I think, in ASP there is an object that takes care of all this tedious work for you, but is there something similar in PHP?

I was thinking of using addslashes(), but it's not as easy as it sounds. I have the following string to echo, how can I do it without manually adding backslashes in front of all (") and (') ?

Before:
"<a href=" src="{$row['link']}"></a>"

After:
"<a href=\" src=\"{$row['link']}\"></a>
 
i can see no easy way of fixing this. the code has been badly written.

i suspect the easiest workaround is to use the heredoc syntax
Code:
echo <<<EOL
<a href="[URL unfurl="true"]http://localhost"><img[/URL] src="{$row['link']}"></a>
EOL;
alternatively change the outer quotes to single quotes and use the dot notation for values
Code:
echo '<a href="[URL unfurl="true"]http://localhost"><img[/URL] src="'.{$row['link']}.'"></a>';

or lastly it is possible to do the inverse
Code:
echo "<a href='[URL unfurl="true"]http://localhost'><img[/URL] src='{$row['link']}'></a>";

to automate the task that you are wanting (and this is not easy), you might be able to dream up a suitable regular expression. it would look something like this

1. find all incidences of 'echo " ... "';
2. within each incidence find all double quotes and replace with an escaped double quote. you could do this with addslashes.

the reason why this is not easy is that it will not cater for examples like this
Code:
echo "<a href="[URL unfurl="true"]http://somepage">click[/URL] me</a>".getSomeText() . " and now add some more text";

if the mistakes are all in an identical definable format you may be in luck.
 
Thanks. I'm a n00b at this - please don't mind me.

You said that the code has been badly written. Why? What's a good way of writing it?

Also, the following gives an error: it doesn't like the {} curly braces, but it works fine without them:

echo '<a href=" src="'.{$row['link']}.'"></a>';

When are the curly braces used (with string values)? I can't find it in the PHP manual.
 
Curly braces are used with variable references in a couple of places. One is what the PHP online manual calls "variable variables", described . The other is in situations where the parser might require disambiguation for array references in side strings:
In the context of the line you have posted:

echo '<a href=" src="'.{$row['link']}.'"></a>';

do not use the braces:

echo '<a href=" src="' . $row['link'] . '"></a>';


Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top