How do i encode all chars in a string.
Example :
A = %32 // Or something like that
B = %33
I want to do this without hardcoding every char. And i also want to be able to decode it again.
Is there a php function for this?
Thanks.
If you really want to do this, you could do it with preg_replace_callback quite easily:
echo preg_replace_callback('/./', function($char) {
return '%' . ord($char[0]);
}, 'this is probably an unnecessary step');
// %116%104%105%115%32%105%115%32%112%114%111%98%97%98%108%121%32%97%110%32%117%110%110%101%99%101%115%115%97%114%121%32%115%116%101%112
You could reverse it with the inverse, using chr:
echo preg_replace_callback('/%[^%]*/', function($seq) {
return chr(substr($seq[0], 1));
}, '%116%104%105%115');
// this
However, this is almost certainly unnecessary for whatever you're doing...
See:
chr
ord
preg_replace_callback
try with:
urlencode($string);
urldecode($string);
http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.urldecode.php
Don't you just mean ord() and chr() ?
Related
i wonder, how to convert character 水瀬いのり to %e6%b0%b4%e7%80%ac%e3%81%84%e3%81%ae%e3%82%8a in php?
also 보이프렌드 to %eb%b3%b4%ec%9d%b4%ed%94%84%eb%a0%8c%eb%93%9c ?
thanks
This one would help urlencode
See here for an example:
$str = '水瀬いのり';
var_dump( urlencode($str) );
If you must use the urlencode() function for that.
echo urlencode('보이프렌드');
// output: %EB%B3%B4%EC%9D%B4%ED%94%84%EB%A0%8C%EB%93%9C
If you really need that the result will be in lowercase then you also must to use the strtolower() function.
echo strtolower(urlencode('보이프렌드'));
// output: %eb%b3%b4%ec%9d%b4%ed%94%84%eb%a0%8c%eb%93%9c
I have an input like this:
$input = 'GFL/R&D/50/67289';
I am trying to get to this:
GFL$2fR$26D$2f50$2f67289
So far, the closest I have come is this:
echo filter_var($input, FILTER_SANITIZE_ENCODED, FILTER_FLAG_ENCODE_LOW)
which produces:
GFL%2FR%26D%2F50%2F67289
How can I get from the given input to the desired output and what sort of encoding is the result in?
By the way, please note the case sensitivity going on there. $2f is required rather than $2F.
This will do the trick: url-encode, then lower-case the encoded sequences and swap % for $ with a preg callback (PHP's PCRE doesn't support case-shifting modifiers):
$input = 'GFL/R&D/50/67289';
echo preg_replace_callback('/(%)([0-9A-F]{2})/', function ($m) {
return '$' . strtolower($m[2]);
}, urlencode($input));
output:
GFL$2fR$26D$2f50$2f67289
i have a string, like this:
'<indirizzo>Via Universit\E0 4</indirizzo>'
Whit HEX char... i need string become:
'<indirizzo>Via Università 4</indirizzo>'
So, i use:
$text= preg_replace('/(\\\\)([a-f0-9]{2})/imu', chr(hexdec("$2")), $text);
But dont work because hexdec dont use value of $2 (that is 'E0'), but use only value '2'.
So hexdex("2") is "2" and chr("2") isnt "à"
What can i do?
$text='<indirizzo>Via Universit\E0 4</indirizzo>';
function cb($match) {
return html_entity_decode('&#'.hexdec($match[1]).';');
}
$text= preg_replace_callback('/\\\\([a-f0-9]{2})/imu', 'cb', $text);
echo $text;
You need to specify your chr(hexdec()) as a callback. Just calling those functions and suppling the result as parameter to preg_replace doesn't do it.
preg_replace_callback('/\\\([a-f0-9]{2})/imu',
function ($match) { return chr(hexdec($match[1])); },
$text)
Having said that, there are probably better ways to do what you want to do overall.
You could also use
<?php
$str = preg_replace('/\\([a-f0-9]{2})/imue', '"\x$1"', '<indirizzo>Via Universit\E0 4</indirizzo>');
assuming i have these texts 'x34' , '150px' , '650dpi' , 'e3r4t5' ... how can i get only numbers ? i mean i want 34 , 150 , 650 , 345 without any other character . i mean get the numbers this string has into one variable .
$str = "e3r4t5";
$str_numbers_only = preg_replace("/[^\d]/", "", $str);
// $number = (int) $str;
Sorry for joining the bandwagon late, rather than using Regex, I would suggest you use PHP's built in functions, which may be faster than Regex.
filter_var
flags for the filters
e.g. to get just numbers from the given string
<?php
$a = '!a-b.c3#j+dk9.0$3e8`~]\]2';
$number = str_replace(['+', '-'], '', filter_var($a, FILTER_SANITIZE_NUMBER_INT));
// Output is 390382
?>
To adhere to more strict standards for your question, I have updated my answer to give a better result.
I have added str_replace, as FILTER_SANITIZE_NUMBER_FLOAT or INT flag will not strip + and - chars from the string, because they are part of PHP's exception rule.
Though it has made the filter bit long, but it's now has less chance of failing or giving you unexpected results, and this will be faster than REGEX.
Edit:
1: Realized that with FILTER_SANITIZE_NUMBER_FLOAT, PHP won't strip these characters optionally .,eE, hence to get just pure numbers kindly use FILTER_SANITIZE_NUMBER_INT
2: If you have a PHP version less than 5.4, then kindly use array('+', '-') instead of the short array syntax ['+', '-'].
You can use a regular expression to remove any character that is not a digit:
preg_replace('/\D/', '', $str)
Here the pattern \D describes any character that is not a digit (complement to \d).
Use PHP FILTER functions if you are using PHP 5.2.X, 5.3.x,5.4 . Its highly recommended
$mixed_input = "e3r4t5";
$only_numbers = filter_var($mixed_input, FILTER_SANITIZE_NUMBER_INT);
Please Go through with this link to know more
Replace everything that isn't a number and use that value.
$str = "foo1bar2baz3";
$num = intval(preg_replace("/[^0-9]/", "", $str));
You could use the following function:
function extract_numbers($string) {
preg_match_all('/([\d]+)/', $string, $match);
return $match;
}
I have number like 9843324+ and now I want to get rid of + at the end and only have 9843324 and so how should I do this in php ?
Right now I am doing $customer_id = explode('+',$o_household->getInternalId); also $o_household->getInternalId returns me 9843324+ but I want 9843324, how can I achieve this ?
Thanks.
if you just want to pop the + off the end, use substr.
$customer_id = substr($o_household->getInternalId, 0, -1);
or rtrim
$customer_id = rtrim($o_household->getInternalId, "+");
You can use a regeular expression to remove anything that isn't a number
$newstr = preg_replace("/[^0-9]+/","",$str);
$customer_id = rtrim ( $o_household->getInternalId, '+' );
rtrim function reference
rtrim removes the characters in the second argument (+ only in this case) from the end of a string.
In case there is no + at the end of the string, this won't mess up your value like substr.
This solution is obviously more readable and faster than, let's say preg_replace too.
you could use intval($o_household->getInternalId)
Is there a reason you need to use explode? i would just use substr as Andy suggest or str_replace. Either would work for the example you provided.
You can use intval to get the integer value of a variable, if possible:
echo intval($str);
Be aware that intval will return 0 on failure.