Ezekiel Posted August 2, 2006 Share Posted August 2, 2006 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 = '£';$pos = strpos($salary, '£');if ($pos === false){ $newsalary = $sign.$salary; // add '£' to the $salary var}else{ $newsalary = trim($salary, '£'); // remove the £ and add '£'; instead} Quote Link to comment https://forums.phpfreaks.com/topic/16334-stripping-%C2%A3-and-adding/ Share on other sites More sharing options...
wildteen88 Posted August 2, 2006 Share Posted August 2, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/16334-stripping-%C2%A3-and-adding/#findComment-67856 Share on other sites More sharing options...
ronverdonk Posted August 2, 2006 Share Posted August 2, 2006 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 = '£';$newsalary = $sign.trim($salary, '£'); [/code]Ronald ;D Quote Link to comment https://forums.phpfreaks.com/topic/16334-stripping-%C2%A3-and-adding/#findComment-67858 Share on other sites More sharing options...
kenrbnsn Posted August 2, 2006 Share Posted August 2, 2006 Why don't you use str_replace() instead:[code]<?php $newsalary = str_replace('£','£',$salary); ?>[/code]You can also try using htmlentities():[code]<?php $newsalary = htmlentitiels($salary); ?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/16334-stripping-%C2%A3-and-adding/#findComment-67861 Share on other sites More sharing options...
Ezekiel Posted August 2, 2006 Author Share Posted August 2, 2006 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;20which seems to indicate that there is something wrong with the &, possibly because this is a character that php itself uses? or... Quote Link to comment https://forums.phpfreaks.com/topic/16334-stripping-%C2%A3-and-adding/#findComment-67866 Share on other sites More sharing options...
Ezekiel Posted August 2, 2006 Author Share Posted August 2, 2006 [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 = '£';$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 Quote Link to comment https://forums.phpfreaks.com/topic/16334-stripping-%C2%A3-and-adding/#findComment-67871 Share on other sites More sharing options...
kenrbnsn Posted August 2, 2006 Share Posted August 2, 2006 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 '£' instead of the '£'.Ken Quote Link to comment https://forums.phpfreaks.com/topic/16334-stripping-%C2%A3-and-adding/#findComment-67874 Share on other sites More sharing options...
ronverdonk Posted August 2, 2006 Share Posted August 2, 2006 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) Quote Link to comment https://forums.phpfreaks.com/topic/16334-stripping-%C2%A3-and-adding/#findComment-67875 Share on other sites More sharing options...
Ezekiel Posted August 2, 2006 Author Share Posted August 2, 2006 ah thankyou, getting closer :)but what are the Âs :£40 £40 Quote Link to comment https://forums.phpfreaks.com/topic/16334-stripping-%C2%A3-and-adding/#findComment-67877 Share on other sites More sharing options...
Ifa Posted August 2, 2006 Share Posted August 2, 2006 What is the charset you are using? Quote Link to comment https://forums.phpfreaks.com/topic/16334-stripping-%C2%A3-and-adding/#findComment-67911 Share on other sites More sharing options...
effigy Posted August 2, 2006 Share Posted August 2, 2006 [url=http://us2.php.net/utf8_encode]utf8_encode[/url] Quote Link to comment https://forums.phpfreaks.com/topic/16334-stripping-%C2%A3-and-adding/#findComment-67955 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.