Remove euro sign from string [duplicate] - php

This question already has answers here:
Unformat money when parsing in PHP
(8 answers)
Closed 9 years ago.
I have a php string which contains a currency. In this case it is a € but to make it future proof I would also like to replace other currency's. How can I filter this currency symbol out?
I tried preg replace but don't know the key to filter currency notations out, also I saw a couple of posts in which they did it entirely different, but couldn't manage to get them to work.
Tried your suggestions guys, still doesn't work. If I add a "," to the suggestions below in this post it does remove the , from the price so it does seem to do something. But the € sign remains.
The price is coming from a woocommerce array, and right before I want to remove the € sign I use a "strip_tags" command. Maybe that has to do something with it. If is use a if else statement to see if it is a string it echo's true, so it isn't a float or anything.

If you want to do it quick and easy, just do a string replacement:
$symbols = array('$', '€', '£');
$unformatted = str_replace($symbols, '', $price_string);

Why not write a simple str_replace() ?
<?php
function strip_currency($str)
{
return str_replace(array('$','€','£','pуб'),'',$str); // You can add-up currency symbols here
}
$yourstr='I have 10$ with me !';
echo $yourstr = strip_currency($yourstr); //"prints" I have 10 with me !
Demo

Related

Replace single quote in a string not working [duplicate]

This question already has answers here:
How to use str_replace to replace single and double quotes
(7 answers)
Closed 4 years ago.
I'm having trouble replacing a single quote in a string, the purpose is to create part of an URL
For example : If I type in "Villeneuve d'ascq" I would want to have :
Villeneuve+d%27ascq", %27 being the ascii equivalent of (')
I tried using str_replace("'", ord("'"), string_name) but it doesn't seem to work
Any help would be appreciated and feel free to ask for any more details
Please try this :
echo 'test';
You can also check this at :
PHP MANUAL

PHP - Can't Remove Carriage Return / Space [duplicate]

This question already has answers here:
What is HTML Entity '
'?
(2 answers)
Closed 5 years ago.
I am reading from a MySQL Database.
The field upc reads as:
811657019822
843018021328
I only want the first numbers; there is a space/carriage return and for some reason I cannot explode it out or trim it out. When I convert to XML it displays as:
<g:gtin>811657019822
843018021328</g:gtin>
Here is what I have tried in PHP and the result:
When I do a var_dump it shows this:
string(25) "811657019822
843018021328"
Notice how they are not all on one line?
It doesn't appear to be a line break as the XML returns a Carriage Return. Any ideas on what to try to remove everything after the first numbers?
UPDATE
As pointed out by #Don't Panic I have erroneously mistaken my slashes the wrong way and should of only been using \r.
This is what worked correctly:
explode("\r", $product['upc']);
Explode with '/r/n' won't work for a couple of reasons. For one you'd need to use a double quoted string, with backslashes instead of forward slashes, like "\r\n". But there isn't a \n, just an \r.
Try using
explode("\r", $yourString);

How to remove white space in PHP generate css class? [duplicate]

This question already has answers here:
How do I strip all spaces out of a string in PHP? [duplicate]
(4 answers)
Closed 6 years ago.
I would like to remove white space in a php generated css code, I have different title header which would like to generate through php code.
Titles are like 1) Baby Day Care 2) Baby Night Care
My php codes are like:
<div class="wistia<?php echo $subject->title;?>">
So when I got the class, I got like wistiaBaby Day Care or wistiaBaby Night Care
But I want the class will be look like wistiaBabyDayCare or wistiaBabyNightCare
(I want space removed between text).
So how I am going to achieve that?
That should to the trick:
<div class="wistia<?php echo str_replace(' ', '', $subject->title);?>">
This just uses the str_replace function where you replace the first character with the 2nd character for a given string.
To remove any whitespace simply use preg_replace (regex):
<div class="wistia<?php echo preg_replace('/\s+/', '', $subject->title);?>">

How to check in php if string contains special characters like { and } [duplicate]

This question already has answers here:
preg_match special characters
(7 answers)
Closed 5 years ago.
There is this input field where I want that user ISN'T able to use following special marks: {}[]$
Right now I have following solution in my code but problem is that it isn't allowing ä, ö, ü or other characters like that.
if (preg_match('/^[-a-zA-Z0-9 .]+$/', $string) || empty($string)){
echo "Everything ok!";
else{
echo "Everything not ok!";}
Because of that, I tried using preg_match('/^[\p{L}\p{N} .-]+$/', $string) because it was said to allow characters from any language but that solution isn't allowing marks like # and *, which I think may be needed. So any solution which would allow anything except {}[]$ -marks? Any help is much appreciated since I can't figure out what to write to get this working.
this is how i do it :
if (preg_match('/[^a-zA-Z\d]/', $string)) {
//$string contains special characters, do something.
}

PHP Replace all characters other than [a-zA-Z0-9\-] [duplicate]

This question already has answers here:
How do I write a regex in PHP to remove special characters?
(3 answers)
Closed 9 years ago.
I would like to replace all characters in a string other than [a-zA-Z0-9\-] with a space.
I've found this post and no matter how many times I tweak the REGEX, I can't get it to "don't match [a-zA-Z0-9-], only match and replace other characters".
At the moment, I have the following:
$original_name = trim(' How to get file creation & modification date/times in Python? ');
$replace_strange_characters = preg_replace("/^((?!([a-zA-Z0-9\-]+)*))$/", " ", $original_name);
// Returns: How to get file creation & modification date/times in Python?
echo $replace_strange_characters;
I wish for it to replace all of the strange characters with a space ' ', thus returning:
How to get file creation modification date times in Python?
I'm really struggling with these "don't match" scenarios.
Here's my code so far: http://tehplayground.com/#2NFzGbG7B
You may try this (You mentioned you want to keep dash)
$original_name = trim(' How to get file creation & modification date/times in Python? ');
$replace_strange_characters = preg_replace('/[^\da-z-]/i', ' ', $original_name);
echo $replace_strange_characters;
DEMO.
Wouldn't this regex do it?
[^a-zA-Z0-9\-]
Oh, and why are you doing this btw?
Try this
$replace_strange_characters = preg_replace("([^a-zA-Z0-9\-\?])", " ", $original_name);
The previous answer strip out the ?

Categories