Jump to content

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
}
Link to comment
https://forums.phpfreaks.com/topic/16334-stripping-%C2%A3-and-adding/
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]
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
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...
[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
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
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)
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.