I have some problem with base64 encode and decode, I would like to decode my string with php, I have found a solution but it's not work well, My expectation I would like decode only string which have been encode and other string which is not encoded should keep normal.
Here is a string :
$string = "I need your help 4Z6B4Z+S4Z6b4Z624Z+G4Z6E4oCL4Z6O4Z624Z6f4Z+L4oCL";
Here is what I have tried :
echo base64_decode($string);
Result:
SSBuZWVkIHlvdXIgaGVscA = ខ្លាំងណាស់
The result above is not what I need, below is what I need:
I need your help ខ្លាំងណាស់
So, could anyone tell me how to do it with php.
This should work for you:
<?php
$str = "I need your help 4Z6B4Z+S4Z6b4Z624Z+G4Z6E4oCL4Z6O4Z624Z6f4Z+L4oCL";
for($count = 0; $count < strlen($str); $count++) {
$temp = substr($str, $count, strlen($str));
if ( base64_encode(base64_decode($temp, true)) === $temp) {
echo substr($str, 0, $count) . base64_decode($str);
break;
}
}
?>
Related
I'm working on a project where I use php to grab a random greek word from a xampp sql server . I then use str_shuffle() to randomize the word order (ex. bye => ybe).However using str_shuffle() on greek letters returns the word with many ???? in place of most greek letters . If I remove str_shuffle() from my code the greek word is displayed correctly with no ??? .
I have written code that ensures I have the correct encoding but str_shuffle() is the problem .
<h1 id = "hidden-word">The word is :
<?php
$link = mysqli_connect('localhost' , 'root' , '' ,'dodecanese');
if(!$link){
echo 'Error connecting to DB';
exit;
}
mysqli_query($link,"SET NAMES 'utf8'");
$query = "SELECT island_name FROM dodecanese ORDER BY RAND() LIMIT 1";
$result = mysqli_query($link, $query);
if(!$result){
echo 'There is an issue with the DB';
exit;
}
$row = mysqli_fetch_assoc($result);
//str shuffle creates ?
echo '<span id = "random-island">'.str_shuffle($row['island_name']). '</span>';
?>
</h1>
I also have encoding <meta charset="utf-8"/> on html . I have seen many posts about this and especially the UTF-8 all the way through but it did not help . I would appreciate your help with this . Thank you in advance .
I've looked in the the PHP manual for str_shuffle and found out in the comments that indeed there are problems with some unicode chars.
But there is also a solution there - which I've tested for you, and it works:
<?php
function str_shuffle_unicode($str) {
$tmp = preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
shuffle($tmp);
return join("", $tmp);
}
$a = "γκξπψ";
$b = str_shuffle($a);
$c = str_shuffle_unicode($a);
echo $a; // γκξπψ
echo "<br/>str_shuffle: ".$b; // ξ��κ�ψ�
echo "<br/>str_shuffle_unicode: ".$c; // κξγπψ
?>
Unfortunately str_shuffle() does not work with multibyte characters and there is no (or at least I do not know such) built-in function to do that. As a workaround you can write one your own. For example the below code will split the string into array of single letters, shuffle the array and then join its elements back to string (I used Cyrillic letters for the example):
$str = "абвгдежзий";
$temp = mb_str_split($str,1);
shuffle($temp);
$str = join("", $temp);
echo $str;
The above function mb_str_split is a PHP 7.4+ only. If you are using earlier version, you can use preg_split:
$str = "абвгдежзий";
$temp = preg_split("//u", $str, 0);
shuffle($temp);
$str = join("", $temp);
echo $str;
the more uncomfortable preg_match_all:
$str = "абвгдежзий";
preg_match_all('/./u', $str, $temp);
shuffle($temp[0]);
$str = join("", $temp[0]);
echo $str;
or even looping and adding to the array char-by-char (that way you save a regex call):
$str = "абвгдежзий";
$len = mb_strlen($str, 'UTF-8');
$temp = [];
for ($i = 0; $i < $len; $i++) {
$temp[] = mb_substr($str, $i, 1, 'UTF-8');
}
shuffle($temp);
$str = join("", $temp);
echo $str;
Say I have string such as below:
"b<a=2<sup>2</sup>"
Actually its a formula. I need to display this formula on webpage but after b string is hiding because its considered as broken anchor tag. I tried with htmlspecialchars method but it returns complete string as plain text. I am trying with some regex but I can get only text between some tags.
UPDATE:
This seems to work with this formula:
"(c<a) = (b<a) = 2<sup>2</sup>"
And even with this formula:
"b<a=2<sup>2</sup>"
HERE'S THE MAGIC:
<?php
$_string = "b<a=2<sup>2</sup>";
$string = "(c<a) = (b<a) = 2<sup>2</sup>";
$open_sup = strpos($string,"<sup>");
$close_sup = strpos($string,"</sup>");
$chars_array = str_split($string);
foreach($chars_array as $index => $char)
{
if($index != $open_sup && $index != $close_sup)
{
if($char == "<")
{
echo "<";
}
else{
echo $char;
}
}
else{
echo $char;
}
}
OLD SOLUTION (DOESN'T WORK)
Maybe this can help:
I've tried to backslash chars, but it doesn't work as expected.
Then i've tried this one:
<?php
$string = "b<a=2<sup>2</sup>";
echo $string;
?>
Using < html entity it seems to work if i understood your problem...
Let me know
Probably you can give spaces such as :
b < a = 2<sup>2</sup>
It does not disappear the tag and looks much more understanding....
You could try this regex approach, which should skip elements.
$regex = '/<(.*?)\h*.*>.+<\/\1>(*SKIP)(*FAIL)|(<|>)/';
$string = 'b<a=2<sup>2</sup>';
$string = preg_replace_callback($regex, function($match) {
return htmlentities($match[2]);
}, $string);
echo $string;
Output:
b<a=2<sup>2</sup>
PHP Demo: https://eval.in/507605
Regex101: https://regex101.com/r/kD0iM0/1
I'm trying to store the numbers from the strings on:
http://driftsdata.statnett.no//snpsrestapi/PowerData/PowerOverview/se?callback=Production.UpdateData
To my database, as these varies every hour.
However, preg_replace and str_replace doesnt work for me, as it only prints out "22" instead of "22 340" as it sais on the website.
here is my code:
for($i=0; $i<6; $i++) {
$info = get_data($countries[$i], $text);
for($j=0; $j<8; $j++) {
$info[$j] = preg_replace('/\s+/', '', $info[$j]);
$info[$j] = (int)$info[$j];
any help?
Since you are dealing with json format, the cleanest and safest way is to use json_decode():
<pre>
<?php
$jsontext = preg_replace('~^[^(]*+\(|\)$~', '', $text);
$json = json_decode($jsontext);
foreach($json->production as $prod) {
printf("<br/><strong>%s</strong><br/>%s\t%s\t%s\t%s\t%s",
$prod->type, $prod->se, $prod->dk, $prod->no, $prod->fi, $prod->ee);
$result[$prod->type] = preg_replace('~[ -]~', '',
array($prod->se, $prod->dk, $prod->no, $prod->fi, $prod->ee));
}
echo '<br/>' . print_r($result, true);
?>
</pre>
I am trying out a Logic in strings but facing difficulties in string manipulation functions. Which function will be good for this below approach:
My String is "Hello" I want to add "------------------" after the first string that is "Hello--------------" and the length of the string should be 20 after the string manipulation.
I want to add "------------------" to the string to make it 20 length.
In other words: Hello+Underscores
If the string length is too much we can trim the string.
Below is the code which I tried.
<?php
$challenge = 'hello';
$length = strlen($challenge);
$i= $length +1;
$challenge=substr($challenge,0,$i);
echo $challenge.'<br>';
?>
I tried string concatenation but I am sure I cant use it in this logic, I think the string adding should be done with preg_replace.
Can some one give a good advice on it!
str-pad is the easiest way to achieve your task and code sample as follows.
<?php
$input = "Alien";
echo str_pad($input, 10); // produces "Alien "
echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
echo str_pad($input, 6 , "___"); // produces "Alien_"
?>
Just use str_pad.
$input = 'hello';
$output = str_pad($input, 20, '_');
echo $output;
demo: http://ideone.com/0EPoV2
Here you go
<?php
$string = "anything";
echo substr($string."------------------------------------------",0,20);
?>
Just use the first 20 chars of your string and ------------------------
Edit based on new requirement not given in original question for some reason.
<?php
$string = "anything";
$newstring = substr($string."------------------------------------------",0,20);
echo $newstring."whatever you want to add at end";
?>
Try this
<?php
$input = "HELLO";
echo str_pad($input, 10, "----", STR_PAD_RIGHT);
?>
here $input is string and 10 is length of char added STR_PAD_RIGHT is position
View this link PHP.net
$str = 'Hello';
$str .= "_";
while(strlen($str) <= 20){
$str .= "-";
}
echo $str;
try this code
$challenge = 'hello';
$length = strlen($challenge);
if($length < 20){
$limit = 20-$length;
for($i=0;$i<$limit;$i++){
$challenge .= '_';
}
}
echo $challenge;
I need the encoded URL for processing in one of the API, but it requires the full encoded URL. For example, the URL from:
http://test.site-raj.co/999999?lpp=1&px2=IjN
has to become an encoded URL, like:
http%3a%2f%test%site%2draj%2eco%2f999999%3flpp%3d1%26px2%3dIjN
I need every symbol to be encoded, even the dot(.) and hyphen(-) like above.
Try this. Inside a function maybe if you are using it more than once...
$str = 'http://test.site.co/999999?lpp=1&p---x2=IjN';
$str = urlencode($str);
$str = str_replace('.', '%2E', $str);
$str = str_replace('-', '%2D', $str);
echo $str;
This will encode all characters that are not plain letters or numbers. You can still decode this with the standard urldecode or rawurldecode:
function urlencodeall($x) {
$out = '';
for ($i = 0; isset($x[$i]); $i++) {
$c = $x[$i];
if (!ctype_alnum($c)) $c = '%' . sprintf('%02X', ord($c));
$out .= $c;
}
return $out;
}
Why don't you use rawurlencode
for example rawurlencode("http://test.site-raj.co/999999?lpp=1&px2=IjN")