Jump to content


Photo

Stripping £ and adding...


  • Please log in to reply
10 replies to this topic

#1 Ezekiel

Ezekiel
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 02 August 2006 - 03:10 PM

I am writing a form/php script to automate the process of making an xml.
In this process I am trying to strip the '£' from a particular variable, and add '& # 163 ;' (sic) in it's place. With that I don't seem to be having very much luck.
This is the code I am using at the moment. Any help is greatly appreciated.

$sign = '&#163';

$pos = strpos($salary, '£');

if ($pos === false){
 
  $newsalary = $sign.$salary; // add '&#163' to the $salary var

}else{

  $newsalary = trim($salary, '£'); // remove the £ and add '&#163'; instead
}

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 02 August 2006 - 03:15 PM

Use str_replace to convert £ into & #163 (without the space):
$sign = '& #163;'; // remove the space betweeen & and #
$salary = '£100';

$pos = strpos($salary, '£');

if ($pos === false){

   $newsalary = $sign.$salary; // add £ to the $salary var

}else{

      $newsalary = str_replace('£', $sign, $salary); // remove the £ and add £ instead
}

echo $newsalary;


#3 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 02 August 2006 - 03:16 PM

In statement
$newsalary = trim($salary, '£'); // remove the £ and add £ instead
I can see that you remove the char, but where do you add the new one instead?

Anyway, you could have done this more compact:
$sign = '&#163';
$newsalary = $sign.trim($salary, '£'); 

Ronald  ;D
RTFM is an almost extinct art form, it should be subsidized.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 02 August 2006 - 03:20 PM

Why don't you use str_replace() instead:
<?php $newsalary = str_replace('£','&#163',$salary); ?>
You can also try using htmlentities():
<?php $newsalary = htmlentitiels($salary); ?>

Ken

#5 Ezekiel

Ezekiel
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 02 August 2006 - 03:25 PM

Thankyou very much.

It works, tho there is a small niggle.

This is what seems to be echod with $newsalary after the £ has been changed. (for a £20 salary)

¦#163;20

which seems to indicate that there is something wrong with the &, possibly because this is a character that php itself uses? or...

#6 Ezekiel

Ezekiel
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 02 August 2006 - 03:30 PM

In statement

$newsalary = trim($salary, '£'); // remove the £ and add £ instead
I can see that you remove the char, but where do you add the new one instead?

Anyway, you could have done this more compact:
$sign = '&#163';
$newsalary = $sign.trim($salary, '£'); 

Ronald  ;D


Trim doesn't seem to work. I added your code to what I had, re submitted the data and came out with ££20

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 02 August 2006 - 03:38 PM

This worked for me:
<?php
$salary = '£20';
$newsalary = str_replace('£','£',$salary);
echo $salary . ' ' . $newsalary;
echo '<pre>' . htmlentities($salary . ' ' . $newsalary) . '</pre>';
?>

BTW, if you use
<?php $newsalary = htmlentities($salary); ?>
then $newsalary would contain the string '&pound;' instead of the '£'.

Ken

#8 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 02 August 2006 - 03:38 PM

Oh yes, the trim works allright! But the pound is not in the string, not the sign you typed in anayway. See the following piece of code:
<?php
$a="£50";
$sign = '£';
$newa = $sign.trim($a, '£'); 
echo $newa;
?>
The result is Pound50.

Ronald  8)
RTFM is an almost extinct art form, it should be subsidized.

#9 Ezekiel

Ezekiel
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 02 August 2006 - 03:43 PM

ah thankyou, getting closer :)

but what are the Âs :

£40 £40

#10 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 02 August 2006 - 04:33 PM

What is the charset you are using?
I'm just a guy who likes to code for fun...

#11 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 02 August 2006 - 05:23 PM

utf8_encode
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users