Jump to content

Archived

This topic is now archived and is closed to further replies.

Ezekiel

Stripping £ and adding...

Recommended Posts

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
}

Share this post


Link to post
Share on other sites
Use str_replace to convert £ into & #163 (without the space):
[code]$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;[/code]

Share this post


Link to post
Share on other sites
In statement
[code]$newsalary = trim($salary, '£'); // remove the £ and add £ instead
[/code]
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:
[code]
$sign = '&#163';
$newsalary = $sign.trim($salary, '£');
[/code]

Ronald  ;D

Share this post


Link to post
Share on other sites
Why don't you use str_replace() instead:
[code]<?php $newsalary = str_replace('£','&#163',$salary); ?>[/code]
You can also try using htmlentities():
[code]<?php $newsalary = htmlentitiels($salary); ?>[/code]

Ken

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
[quote author=ronverdonk link=topic=102724.msg408130#msg408130 date=1154531815]
In statement
[code]$newsalary = trim($salary, '£'); // remove the £ and add £ instead
[/code]
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:
[code]
$sign = '&#163';
$newsalary = $sign.trim($salary, '£');
[/code]

Ronald  ;D
[/quote]

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

Share this post


Link to post
Share on other sites
This worked for me:
[code]<?php
$salary = '£20';
$newsalary = str_replace('£','£',$salary);
echo $salary . ' ' . $newsalary;
echo '<pre>' . htmlentities($salary . ' ' . $newsalary) . '</pre>';
?>[/code]

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

Ken

Share this post


Link to post
Share on other sites
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:
[code]
<?php
$a="£50";
$sign = '£';
$newa = $sign.trim($a, '£');
echo $newa;
?>[/code]
The result is Pound50.

Ronald  8)

Share this post


Link to post
Share on other sites
What is the charset you are using?

Share this post


Link to post
Share on other sites
[url=http://us2.php.net/utf8_encode]utf8_encode[/url]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.