PHP Replace special characters with corresponding ascii code - character

I'm looking for a function that converts all special characters in their corresponding ascii code!
I'll exlpain!
I have this code:
<trust:RequestSecurityTokenResponseCollection xmlns:trust="http://docs.oasis-open.org/ws-sx/ws-trust/200512">
And I would like to convert it in
%3Ctrust%3ARequestSecurityTokenResponseCollection+xmlns%3Atrust%3D%22http%3A%2F%2Fdocs.oasis-open.org%2Fws-sx%2Fws-trust%2F200512%22%3E
If I use the function urlencode it convert the string in
%26lt%3Btrust%3ARequestSecurityTokenResponseCollection+xmlns%3Atrust%3D%26quot%3Bhttp%3A%2F%2Fdocs.oasis-open.org%2Fws-sx%2Fws-trust%2F200512%26quot%3B%3E
How can I solve the problem?

The problem was only for 2 characters:
<
"
So I solved with this 3 lines:
$result = urlencode($result);
$result = str_replace ('%26lt%3B','%3C', $result);
$result = str_replace ('%26quot%3B','%22', $result);

Related

In PowerShell, how do I copy the last alphabet characters from a string which also has numbers in it to create a variable?

For example if the string is blahblah02baboon - I need to get the "baboon" seperated from the rest and the variable would countain only the characters "baboon". Every string i need to do this with has alphabet characters first then 2 numbers then more alphabet characters, so it should be the same process everytime.
Any advice would be greatly appreciated.
My advice is to learn about regular expressions.
'blahblah02baboon' -replace '\D*\d*(\w*)', '$1'
Or use regex
$MyString = "01baaab01blah02baboon"
# Match any character which is not a digit
$Result = [regex]::matches($MyString, "\D+")
# Take the last result
$LastResult = $Result[$Result.Count-1].Value
# Output
Write-Output "My last result = $LastResult"

What is the best way to replace unicode characters in a php string?

I have some strange characters showing up in a production database. The string I want to replace is \u00fc\u00be\u008c\u00a3\u00a4\u00bc.
This fails.
$column = str_replace('\u00fc\u00be\u008c\u00a3\u00a4\u00bc', "'", $column);
and this works.
$column = str_replace('ü¾Œ£¤¼',"'",$column) ;
What is the best way to replace unicode characters in a PHP string without copying in the decoded text?
After following the lead from https://stackoverflow.com/users/395384/epb I used json_decode to translate the unicode which works.
$unicode = json_decode("\u00fc\u00be\u008c\u00a3\u00a4\u00bc") ;
$column = str_replace($unicode, "'", urldecode($row[$columnIndex]));

Remove first and last three character of a word with powershell

I have a list of users in a text file who's names are in the following format: xn-tsai-01.
How do I script to remove the xn- KEEP THIS -01 so the output is like: tsai
I know how to do this in bash but not too familiar with powershell.
Thanks in advance!
Why not use Substring method. If you will always trim the first three characters, you can do the following assuming the variable is a string type.
$string = xn-tsai-01
$string.Substring(3)
Here is a quick way to do it using regex:
'xn-tsai-01' -replace '.*?-(.*)-.*','$1'
Example with a list:
(Get-Content list.txt) -Replace '.*?-(.*)-.*','$1'
You can use the .NET string method IndexOf("-") to find the first, and LastIndexOf("-") to find the last occurrence of "-" within the string.
Use these indexes with Substring() to remove the unnecessary parts:
function Clean-Username {
param($Name)
$FirstDash = $Name.IndexOf("-") + 1
$LastDash = $Name.LastIndexOf("-")
return $Name.Substring( $f, $l - $f )
}
PS C:\> Clean-UserName -Name "xn-tsai-01"
tsai
Boe's example is probably going to be the most efficient.
Another way is to use the split() method if they're in a uniform format.
Get-Content .\list.txt | % { ($_.Split('-'))[1] }
% is an alias for ForEach

Preg_replace in php - multiple replace

I have $string, which contains:
this example
and I have these 3 expressions:
$pattern = array('/aa*/','/ii*/');
$replacement = array('<i>$0</i>','<b>$0</b>');
preg_replace($pattern, $replacement, $string);
where, preg_replace returns:
th<b>i</b>s ex<<b>i</b>>a</<b>i</b>>mple
and I need output like this:
th<b>i</b>s ex<i>a</i>mple
which means, that I want to replace only characters in original string. Is it possible?
This does the trick in my testing
$pattern = array('/([a-z|^|\s])(aa*)/', '/([a-z|^|\s])(ii*)/');
$replacement = array('$1<i>$2</i>','$1<b>$2</b>');

encode special character in html entities in perl

I have a string where special characters like ! or " or & or # or #, ... can appear. How can I convert in the string
str = " Hello "XYZ" this 'is' a test & so *n #."
automatically every special characters with their html entities, so that I get this:
str = " Hello &quot ;XYZ&quot ; this &#39 ;is&#39 ; a test &amp ; so on #"
I tried
$str=HTML::Entities::encode_entities($str);
but it does a partial work the # is not transformed in &#64 ;
SOLUTION:
1) with your help (Quentin and vol7ron) I came up with this solution(1)
$HTML::Entities::char2entity{'#'} = '#';
$HTML::Entities::char2entity{'!'} = '!';
$HTML::Entities::char2entity{'#'} = '#';
$HTML::Entities::char2entity{'%'} = '%';
$HTML::Entities::char2entity{'.'} = '.';
$HTML::Entities::char2entity{'*'} = '*';
$str=HTML::Entities::encode_entities($str, q{#"%'.&#*$^!});
2) and I found a shorter(better) solution(2) found it here:
$str=HTML::Entities::encode_entities($str, '\W');
the '\W' does the job
#von7ron with solution(1) you will need to specify the characters you want to translate as Quentin mentioned earlier even if they are on the translation table.
# isn't transformed because it isn't considered to be a "special character". It can be represented in ASCII and has no significant meaning in HTML.
You can expand the range of characters that are converted with the second argument to the function you are using, as described in the documentation.
You can manually add a character to the translation table (char2entity hash).
$HTML::Entities::char2entity{'#'} = '#';
my $str = q{ Hello "XYZ" this 'is' a test & so on #};
my $encoded = HTML::Entities::encode_entities( $str, q{<>&"'#} );
The above adds #, which will be translated to #.
You then need to specify the characters you want to translate, if you don't it uses <>&", so I added both # and '. Notice, I didn't have to add the ' to the translation table, because it's already there by default.
You don't need to add ASCII characters (0-255) to the char2entity hash, since the module will do it automatically.
Note: Setting the char2entity for #, was done as an example. The module automatically sets numerical entities for ASCII characters (0-255) that weren't found. You'd have to use it for unicode characters, though.
Cheap, dirty, and ugly, but works:
my %translations;
$translations{'"'} = '&quot ;';
$translations{'\''} = '&#39 ;';
etc...
sub transform()
{
my $str = shift;
foreach my $character (keys(%translations))
{
$str =~ s/$character/$translations{$character}/g;
}
return $str;
}