Perl to php similar functions for escape and encode - php

I have this part of perl code and I want to find a similar code in PHP.
(I don't know perl, only php)
my $encoded_string = encode( 'UTF8', $not_latin_string );
my $escaped_string = Unicode::Escape::escape($encoded_string);
The result will then be bcrypt and store as password.
I tried different methods but are not working correctly. So if someone knows perl and php and can help me I will be thankful.
Thanks.

Escape unicode character:
<?php
function unicode_escape($str) {
$str_utf16_be = mb_convert_encoding($str, "UTF-16BE");
$chunks = str_split(bin2hex($str_utf16_be), 4);
return implode("", preg_filter("/^/", "\\u", $chunks));
}
$not_latin_string = '𠮷野屋の牛丼食べたい';
echo unicode_escape($not_latin_string);
output:
\ud842\udfb7\u91ce\u5c4b\u306e\u725b\u4e3c\u98df\u3079\u305f\u3044
Do you really need this function? Have you tried json_encode?

Related

Preg_replace not works if the string is too long?

I have the following long string:
$long_text = "aaaaaaaaaaaaaaaaaaaaaaaa[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaaaaaaaaaaaaaaaaaa[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaaaaaaaaaaaaaaaaaa[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaaaaaaaaaaaaaaaaaa[BS][BS][BS][BS][BS][BS][BS][BS]";
And I would like use [BS] as backspace. So I have the following code:
$long_text = preg_replace('/.(?R)*\[BS\]/', '', $long_text);
But It doesn't work because the string is too long.
Can someone help me and tell me why can't PHP handle this long text? Is there any way to handle it? (it works with shorter texts)
Not an answer to your question, but maybe to your problem ....
<?php
$s = "aaaaaaaaXbbbbbbbb[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaaXbbbbbbbb[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaaXbbbbbbbb[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaaXbbbbbbbb[BS][BS][BS][BS][BS][BS][BS][BS]aaaaaaaa";
while ( false!==($pos=strpos($s, '[BS]')) ) {
$s = substr_replace($s, '', $pos-1, 5);
}
echo $s;
prints
aaaaaaaaXaaaaaaaaXaaaaaaaaXaaaaaaaaXaaaaaaaa
(as intended)

Creating a UTF-8 string from hexadecimal code

In C++, it's possible create a UTF-8 string using this kind of notation: "\uD840\uDC50".
However this doesn't work in PHP. Is there a similar notation?
If not, is there any built-in way to create a UTF-8 string knowing its Unicode code point?
I've ended up implementing it like this:
$utf8 = html_entity_decode("一", ENT_COMPAT, 'UTF-8');
function hexToString($str){return chr(hexdec(substr($str, 2)));}
$result = preg_replace_callback("/(\\\\x..)/isU", function($m) { return hexToString($m[0] ); }, $str);

PHP Preg_Replace REGEX BB-Code

So I have created this function in PHP to output text in the required form. It is a simple BB-Code system. I have cut out the other BB-Codes from it to keep it shorter (Around 15 cut out)
My issue is the final one [title=blue]Test[/title] (Test data) does not work. It outputs exactly the same. I have tried 4-5 different versions of the REGEX code and nothing has changed it.
Does anyone know where I am going wrong or how to fix it?
function bbcode_format($str){
$str = htmlentities($str);
$format_search = array(
'#\[b\](.*?)\[/b\]#is',
'#\[title=(.*?)\](.*?)\[/title\]#i'
);
$format_replace = array(
'<strong>$1</strong>',
'<div class="box_header" id="$1"><center>$2</center></div>'
);
$str = preg_replace($format_search, $format_replace, $str);
$str = nl2br($str);
return $str;
}
Change the delimiter # to /. And change "/[/b\]" to "\[\/b\]". You need to escape the "/" since you need it as literal character.
Maybe the "array()" should use brackets: "array[]".
Note: I borrowed the answer from here: Convert BBcode to HTML using JavaScript/jQuery
Edit: I forgot that "/" isn't a metacharacter so I edited the answer accordingly.
Update: I wasn't able to make it work with function, but this one works. See the comments. (I used the fiddle on the accepted answer for testing from the question I linked above. You may do so also.) Please note that this is JavaScript. You had PHP code in your question. (I can't help you with PHP code at least for awhile.)
$str = 'this is a [b]bolded[/b], [title=xyz xyz]Title of something[/title]';
//doesn't work (PHP function)
//$str = htmlentities($str);
//notes: lose the single quotes
//lose the text "array" and use brackets
//don't know what "ig" means but doesn't work without them
$format_search = [
/\[b\](.*?)\[\/b\]/ig,
/\[title=(.*?)\](.*?)\[\/title\]/ig
];
$format_replace = [
'<strong>$1</strong>',
'<div class="box_header" id="$1"><center>$2</center></div>'
];
// Perform the actual conversion
for (var i =0;i<$format_search.length;i++) {
$str = $str.replace($format_search[i], $format_replace[i]);
}
//place the formatted string somewhere
document.getElementById('output_area').innerHTML=$str;
​
Update2: Now with PHP... (Sorry, you have to format the $replacements to your liking. I just added some tags and text to demostrate the changes.) If there's still trouble with the "title", see what kind of text you are trying to format. I made the title "=" optional with ? so it should work properly work texts like: "[title=id with one or more words]Title with id[/title]" and "[title]Title without id[/title]. Not sure thought if the id attribute is allowed to have spaces, I guess not: http://reference.sitepoint.com/html/core-attributes/id.
$str = '[title=title id]Title text[/title] No style, [b]Bold[/b], [i]emphasis[/i], no style.';
//try without this if there's trouble
$str = htmlentities($str);
//"#" works as delimiter in PHP (not sure abut JS) so no need to escape the "/" with a "\"
$patterns = array();
$patterns = array(
'#\[b\](.*?)\[/b\]#',
'#\[i\](.*?)\[/i\]#', //delete this row if you don't neet emphasis style
'#\[title=?(.*?)\](.*?)\[/title\]#'
);
$replacements = array();
$replacements = array(
'<strong>$1</strong>',
'<em>$1</em>', // delete this row if you don't need emphasis style
'<h1 id="$1">$2</h1>'
);
//perform the conversion
$str = preg_replace($patterns, $replacements, $str);
echo $str;

Having trouble with a preg_match_all and a degree symbol

I am having trouble with a preg_match_all on a string that contains a degree symbol. The sample of code is below.
//Sample data
$x = "<array_0>
<id>text-21650</id>
<text>Lat/Long 38° 57' 34 N, 106° 21' 38 W</text>
</array_0>";
$reels = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';
preg_match_all($reels, $x, $elements);
foreach ($elements[1] as $ie => $xx) {
$name = $elements[1][$ie];
$cdend = strpos($elements[3][$ie], "<");
if ($cdend > 0) {
$xmlary[$name] = substr($elements[3][$ie], 0, $cdend - 1);
}
if (preg_match($reels, $elements[3][$ie]))
$xmlary[$name] = processEl($elements[3][$ie]);
else if ($elements[3][$ie] !== null) {
$xmlary[$name] = $elements[3][$ie];
}
}
For some reason it doesn't work properly with the degree symbols in there. If I take it out it works. I would really like to find a way that they can stay in there without changing them. I am also wondering if there may be other extended character that could cause a problem too.
Any help would be greatly appreciated.
Thanks
Have a look at this previous answer on StackOverflow.
Basically you will have to switch to Unicode matching.
Use mb_ereg_match instead to support UTF-8 chars. Docs:
http://php.net/manual/en/book.mbstring.php
Initialize mb* like this:
mb_regex_encoding('UTF-8'); mb_internal_encoding('UTF-8');
I had the same problem, and this other post from stackoverflow helped me. Basically, to look for a degree symbol, you'd use \x{00B0}, ie.
preg_match_all("/\x{00B0}/", $x, $elements);

Text Obfuscation using base64_encode()

I'm playing around with encrypt/decrypt coding in php. Interesting stuff!
However, I'm coming across some issues involving what text gets encrypted into.
Here's 2 functions that encrypt and decrypt a string. It uses an Encryption Key, which I set as something obscure.
I actually got this from a php book. I modified it slightly, but not to change it's main goal.
I created a small example below that anyone can test.
But, I notice that some characters show up as the "encrypted" string. Characters like "=" and "+".
Sometimes I pass this encrypted string via the url. Which may not quite make it to my receiving scripts. I'm guessing the browser does something to the string if certain characters are seen. I'm really only guessing.
is there another function I can use to ensure the browser doesn't touch the string? or does anyone know enough php bas64_encode() to disallow certain characters from being used? I'm really not going to expect the latter as a possibility. But, I'm sure there's a work-around.
enjoy the code, whomever needs it!
define('ENCRYPTION_KEY', "sjjx6a");
function encrypt($string) {
$result = '';
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr(ENCRYPTION_KEY, ($i % strlen(ENCRYPTION_KEY))-1, 1);
$char = chr(ord($char)+ord($keychar));
$result.=$char;
}
return base64_encode($result)."/".rand();
}
function decrypt($string){
$exploded = explode("/",$string);
$string = $exploded[0];
$result = '';
$string = base64_decode($string);
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr(ENCRYPTION_KEY, ($i % strlen(ENCRYPTION_KEY))-1, 1);
$char = chr(ord($char)-ord($keychar));
$result.=$char;
}
return $result;
}
echo $encrypted = encrypt("reaplussign.jpg");
echo "<br>";
echo decrypt($encrypted);
You could use PHP's urlencode and urldecode functions to make your encryption results safe for use in URLs, e.g
echo $encrypted = urlencode(encrypt("reaplussign.jpg"));
echo "<br>";
echo decrypt(urldecode($encrypted));
You should look at urlencode() to escape the string correctly for use in the query.
If you are worried about +,= etc. similar characters, you should have a look at http://php.net/manual/en/function.urlencode.php and it's friends from "See also" section. Encode it in encrypt() and decode at the beginning of decrypt().
If this doesn't work for you, maybe some simple substitution?
$text = str_replace('+','%20',$text);

Categories