Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #8718 > unrolled thread

Thorny string translation issue.

Started byThe Natural Philosopher <tnp@invalid.invalid>
First post2013-03-18 23:24 +0000
Last post2013-03-19 07:58 -0500
Articles 9 — 6 participants

Back to article view | Back to comp.lang.php


Contents

  Thorny string translation issue. The Natural Philosopher <tnp@invalid.invalid> - 2013-03-18 23:24 +0000
    Re: Thorny string translation issue. Christoph Becker <cmbecker69@gmx.de> - 2013-03-19 01:26 +0100
      Re: Thorny string translation issue. The Natural Philosopher <tnp@invalid.invalid> - 2013-03-19 09:33 +0000
        Re: Thorny string translation issue. BootNic <bootnic.bounce@gmail.com> - 2013-03-19 07:45 -0400
        Re: Thorny string translation issue. Christoph Becker <cmbecker69@gmx.de> - 2013-03-20 00:56 +0100
    Re: Thorny string translation issue. SwissCheese <swisscheese@cfl.rr.com> - 2013-03-18 20:40 -0400
      Re: Thorny string translation issue. The Natural Philosopher <tnp@invalid.invalid> - 2013-03-19 09:38 +0000
    Re: Thorny string translation issue. Jerry Stuckle <jstucklex@attglobal.net> - 2013-03-19 09:01 -0400
    Re: Thorny string translation issue. "Peter H. Coffin" <hellsop@ninehells.com> - 2013-03-19 07:58 -0500

#8718 — Thorny string translation issue.

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-03-18 23:24 +0000
SubjectThorny string translation issue.
Message-ID<ki87ne$s68$1@news.albasani.net>

The problem.

I want the user to enter raw text and embedded HTML into a database. No 
trouble with getting it in, but when getting it out, it needs massaging 
for display..

Now I cant find a function in the library that will skip html styles 
like <b>...</b> or <a...></a> code, and yet still tranlsatee e.g. '>' to 
'&gt;' etc etc.

Anyone done this before?


-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to 
lead are elected by the least capable of producing, and where the 
members of society least likely to sustain themselves or succeed, are 
rewarded with goods and services paid for by the confiscated wealth of a 
diminishing number of producers.

[toc] | [next] | [standalone]


#8722

FromChristoph Becker <cmbecker69@gmx.de>
Date2013-03-19 01:26 +0100
Message-ID<ki8bbd$rrk$1@speranza.aioe.org>
In reply to#8718
The Natural Philosopher wrote:

> I want the user to enter raw text and embedded HTML into a database. No
> trouble with getting it in, but when getting it out, it needs massaging
> for display..
> 
> Now I cant find a function in the library that will skip html styles
> like <b>...</b> or <a...></a> code, and yet still tranlsatee e.g. '>' to
> '&gt;' etc etc.

You can strip the tags with strip_tags() (note the $allowable_tags
parameter) and later escape the HTML special characters with
htmlspecialchars() (note the $encoding parameter, which may be necessary
to prevent UTF-7 injection attacks).  Simplified:

  >>> $str='alpha <b>beta <  gamma</b> delta';
  >>> htmlspecialchars(strip_tags($str));
  'alpha beta &lt;  gamma delta'

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#8731

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-03-19 09:33 +0000
Message-ID<ki9bcd$gu7$1@news.albasani.net>
In reply to#8722
On 19/03/13 00:26, Christoph Becker wrote:
> The Natural Philosopher wrote:
>
>> I want the user to enter raw text and embedded HTML into a database. No
>> trouble with getting it in, but when getting it out, it needs massaging
>> for display..
>>
>> Now I cant find a function in the library that will skip html styles
>> like <b>...</b> or <a...></a> code, and yet still tranlsatee e.g. '>' to
>> '&gt;' etc etc.
>
> You can strip the tags with strip_tags() (note the $allowable_tags
> parameter) and later escape the HTML special characters with
> htmlspecialchars() (note the $encoding parameter, which may be necessary
> to prevent UTF-7 injection attacks).  Simplified:
how do I put the tags I want back though.


Specifically consider the example

<b> <= </b>

where I want to display a bold 'arrow'

strip tags will just remove the tags I want.

But htmlspecialchars simply turns all tags into (displayed ) raw html.


>
>    >>> $str='alpha <b>beta <  gamma</b> delta';
>    >>> htmlspecialchars(strip_tags($str));
>    'alpha beta &lt;  gamma delta'
>

yes, but that's not the output I want.




-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to 
lead are elected by the least capable of producing, and where the 
members of society least likely to sustain themselves or succeed, are 
rewarded with goods and services paid for by the confiscated wealth of a 
diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#8733

FromBootNic <bootnic.bounce@gmail.com>
Date2013-03-19 07:45 -0400
Message-ID<20130319074520.4756c2eb@bootnic.server>
In reply to#8731

[Multipart message — attachments visible in raw view] — view raw

In article <ki9bcd$gu7$1@news.albasani.net>, The Natural Philosopher
<tnp@invalid.invalid> wrote:

>> The Natural Philosopher wrote:

>>> I want the user to enter raw text and embedded HTML into a database.
>>> No trouble with getting it in, but when getting it out, it needs
>>> massaging for display..

>>> Now I cant find a function in the library that will skip html styles
>>> like <b>...</b> or <a...></a> code, and yet still tranlsatee e.g. '>'
>>> to '&gt;' etc etc.

[snip]

> Specifically consider the example

> <b> <= </b>

> where I want to display a bold 'arrow'

[url] http://php.net/manual/en/book.tidy.php [/url]

function tUp($content) {
 $config = array(
'indent'                        => 0,
'output-html'                   => 1,
'wrap'                          => 0,
'doctype'                       => 'strict',
'sort-attributes'               => 'alpha',
'output-encoding'               => 'utf8',
'char-encoding'                 => 'utf8'
);
  if (extension_loaded("tidy")) {

    $tidy = new tidy;
    $tidy->parseString($content, $config, 'utf8');
    $tidy->cleanRepair();
    $body = $tidy->Body();
    $content = $body->value;
    $content = trim(preg_replace("`<(/)?body>`", "", $content));

  }
  return $content;
}
print tUp('<b> <= </b>');

[snip]



-- 
BootNic                                               Tue Mar 19, 2013 07:45 am
  The only thing wrong with immortality is that it tends to go on forever.
  *Herb Caen*

[toc] | [prev] | [next] | [standalone]


#8746

FromChristoph Becker <cmbecker69@gmx.de>
Date2013-03-20 00:56 +0100
Message-ID<kiatuv$kjc$1@speranza.aioe.org>
In reply to#8731
The Natural Philosopher wrote:

> yes, but that's not the output I want.

Sorry, I had misunderstood your request.  If you want to keep all
(X)HTML tags and replace special characters not belonging to tags,
BootNic's solution using tidy is appropriate.  But be aware that tidy
may not filter possibly malicious user input:

  >>> tUp('<img src="" onclick="doSomethingBad()">')
  '<img src="" onclick="doSomethingBad()">'

If you need this, you may have a look at <http://htmlpurifier.org/>.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#8723

FromSwissCheese <swisscheese@cfl.rr.com>
Date2013-03-18 20:40 -0400
Message-ID<ki8c20$foi$1@dont-email.me>
In reply to#8718
On 03/18/2013 07:24 PM, The Natural Philosopher wrote:
>
>
> The problem.
>
> I want the user to enter raw text and embedded HTML into a database. No
> trouble with getting it in, but when getting it out, it needs massaging
> for display..
>
> Now I cant find a function in the library that will skip html styles
> like <b>...</b> or <a...></a> code, and yet still tranlsate e.g. '>' to
> '&gt;' etc etc.
>
> Anyone done this before?
>
>

http://www.php.net/manual/en/function.htmlspecialchars.php#101592

-- 
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

[toc] | [prev] | [next] | [standalone]


#8732

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2013-03-19 09:38 +0000
Message-ID<ki9bmn$hjt$1@news.albasani.net>
In reply to#8723
On 19/03/13 00:40, SwissCheese wrote:
> On 03/18/2013 07:24 PM, The Natural Philosopher wrote:
>>
>>
>> The problem.
>>
>> I want the user to enter raw text and embedded HTML into a database. No
>> trouble with getting it in, but when getting it out, it needs massaging
>> for display..
>>
>> Now I cant find a function in the library that will skip html styles
>> like <b>...</b> or <a...></a> code, and yet still tranlsate e.g. '>' to
>> '&gt;' etc etc.
>>
>> Anyone done this before?
>>
>>
>
> http://www.php.net/manual/en/function.htmlspecialchars.php#101592
>
That of course was my first port of call, but it doesn't do the needed job.

I want a version of that that is intelligent, and knows what are HTML 
tags, and leaves those alone, or what are simply isolated < and > chars.

I know a lot of sites use things like [URL] and [B] to encode markup for 
this reason I suppose. to a special chars on them and THEN replace [ 
with < and so on..but then what happens if you actually want to encode a 
'[' ?




-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to 
lead are elected by the least capable of producing, and where the 
members of society least likely to sustain themselves or succeed, are 
rewarded with goods and services paid for by the confiscated wealth of a 
diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#8734

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-03-19 09:01 -0400
Message-ID<ki9ngm$2ll$1@dont-email.me>
In reply to#8718
On 3/18/2013 7:24 PM, The Natural Philosopher wrote:
>
>
> The problem.
>
> I want the user to enter raw text and embedded HTML into a database. No
> trouble with getting it in, but when getting it out, it needs massaging
> for display..
>
> Now I cant find a function in the library that will skip html styles
> like <b>...</b> or <a...></a> code, and yet still tranlsatee e.g. '>' to
> '&gt;' etc etc.
>
> Anyone done this before?
>
>

Yup.  Pay someone who knows how to program to do it.  It's well beyond 
your capabilities.

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

[toc] | [prev] | [next] | [standalone]


#8737

From"Peter H. Coffin" <hellsop@ninehells.com>
Date2013-03-19 07:58 -0500
Message-ID<slrnkkgo79.m9f.hellsop@nibelheim.ninehells.com>
In reply to#8718
On Mon, 18 Mar 2013 23:24:31 +0000, The Natural Philosopher wrote:
>
>
> The problem.
>
> I want the user to enter raw text and embedded HTML into a database. No 
> trouble with getting it in, but when getting it out, it needs massaging 
> for display..
>
> Now I cant find a function in the library that will skip html styles 
> like <b>...</b> or <a...></a> code, and yet still tranlsatee e.g. '>' to 
> '&gt;' etc etc.
>
> Anyone done this before?

Sorry to say, either it's HTML or it's not and bare anglebraces are
not HTML. There's no way to reliably tokenize only some angles without
serious context analysis, and even a reasonably complex regular
expression isn't going to be able to get EVERYTHING right. Your only
path that I see is to load a DTD and essentially pre-render the document
in memory to handle the real tags, convert everthing that's left to
entities, and then compare/build the two copies, taking the tags from
the raw file and the entities from the rendered-and-converted one. And
even that could screw things up at least differently from some other
means of rendering things, so results could vary.

-- 
Surely the 98% of DNA we share with monkeys must be enough to stop
people from sinking this low.
              -- Frossie

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web