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!

Urgent help need with inserting £ symbol

Status
Not open for further replies.

barneyblackburn

Programmer
Apr 7, 2008
8
0
0
GB
I have some perl code that does a find and replace in an HTML document to strip out the HTML of it. It works really well except when it tries to insert a £ symobl. The code I have is:

my ($pound) = $_;
$pound =~ s/£/£/g;
$_ = $pound;

What is does is replace £ with £. How do I get it to just insert £.
Ive tried running a further find and replace to find Â, but to no luck, as £ seems to be a symbol by itself, so can seperate it out!

Thanks so much!
 
What editor are you using to edit your code? That looks like a '£' inserted by a word processor, not a code editor...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I am using Text Pad to edit it. Where do i find a non word processed £ symbol?

Thanks - Barney
 
That shouldn't be it, what's the language of your keyboard.

To eliminate Steve's suggestion, try editing the file in Notepad, and run the script again.

I assume you're running this locally, if you're copying the file to a webserver, are you ftp'ing using ASCII?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Thanks Paul - Just tried editing it in Notepad but still get the same problem. My keyboard is fully set to English (UK), and I am running it locally.

Thanks again - Barney
 
Maybe some kind of code page or Unicode issue. The £ could be a utf-8 wide character being displayed by something that is expecting raw ASCII and hence doesn't interpret the data stream correctly.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hi,

You must have something weird going on - this test.cgi script works fine:

Code:
#!/usr/bin/perl -w

 my $pound = "testing £ stuff here";
     $pound =~ s/£/£/g;

 print "FOO: $pound";

Prints out:

ultranerds@east cgi-bin $ perl test.cgi
FOO: testing £ stuff here

Can you post a bit more of the code you are using, to print the data into your text file (or whatever your trying to do with it)

BTW, why are you trying to convert from HTML valid £ , into it symbol? =)

Cheers

Andy
 
This is my code I have

Code:
package TeamSite::rayXML;

use strict;

sub removeHTML 
{ 

 my ($tags) = $_;
        $tags =~ s/\<[^\<]+\>//g;
        $_ = $tags;

 my ($pound) = $_;
        $pound =~ s/&pound;/£/g;
        $_ = $pound;

 my ($amp) = $_;
        $amp =~ s/&amp;/&/g;
        $_ = $amp;
 }

1;

The reason for it is to produce text versions of HTML emails, so it stips out all HTML tags but I also need it add the correct symbols.

Thanks - Barney
 
Dear barneyblackburn, Can you give a try to ASCII code instead of usin £ symbol if the problem persist?

I tried this

Code:
#!/usr/bin/perl -w

 my $pound = "testing &pound; stuff here";
     $pound =~ s/&pound;/£/g;

 print "FOO: $pound";
[/cod]

on my local suse 10.2 installation and it work fine
 
How would I use ASCII with Perl - I dont know perl at all, I just copied and pasted this find replace script.
 
barneyblackburn

It is normally considered bad form to set $_ explicitly. As $_ is the default target, you should find that
Code:
package TeamSite::rayXML;

use strict;

sub removeHTML {

   s/\<[^\<]+\>//g;
   s/&pound;/£/g;
   s/&amp;/&/g;
}

1;
has the same effect. Obviously this doesn't fix your problem, but it does at least clean up the code... [smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
On my system ord('£') is 163. So try
Code:
my $pound = chr(163);
s/&pound;/$pound/g;
and see what happens...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Same thing still happens. But I have been playing and discovered its fine if i replace with any ASCII under 127, but 128 and above it inserts this  before the symbol. I have also read:
"Note that characters from 128 to 255 (inclusive) are by default internally not encoded as UTF-8 for backward compatibility reasons."
Does that provide any clues?
 
Once you have replaced the entities, what do you do with the data? Does it get stored anywhere like on a database before you see it?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
This seems to have worked

Code:
 use HTML::Entities;
 
 my ($symbols) = $_;
        $symbols = decode_entities($symbols, "\200-\377");
        $_ = $symbols;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top