This is the code in lib.php:
<?php
class table {
function __construct($string, $time) {
$out = '<table cellpadding="5">';
$out .= $this->getRow('th', 'Nom., Shuffled String, Lenght');
for ($x = 1; $x <= $time; $x++) {
$shuffledStr = str_shuffle($string); //Maybe this causes the problem
$shuffledStr_len = strlen($shuffledStr);
$out .= $this->getRow('td', $x . ', ' . $shuffledStr . ', ' . $shuffledStr_len);
}
$out .= '</table>';
echo $out;
}
public function getRow($tagName, $contents_list) {
//Variables:
$out = '';
$contents_array = explode(', ', $contents_list);
$contents_number = count($contents_array);
$start_tag = '<' . $tagName . '>';
$end_tag = '</' . $tagName . '>';
// Build
$out .= '<tr>';
for ($i = 0; $i < $contents_number; $i++) {
$out .= $start_tag . $contents_array[$i] . $end_tag;
}
$out .= '</tr>';
return $out;
}
}
?>
And here is index.php:
<?php
require_once 'lib.php';
$string = ''; //My string
$shuffleTimes = 100;
$table = new table($string, $shuffleTimes);
?>
This program gets the a string and the number that you wanna shuffle,
then create a table and returns the number, shuffled string and the length of the shuffle string in each row.
If I set the variable $string to 'Stack Overflow' for instance it'll work correctly (It shuffles this word 100 times randomly, returns all lengths 14 and the length of the ALL shuffled strings are really 14.)
But...
If I add some special character to the variable $string (for example Stack Overflow+_)(*&^%$#{}[]<>#!~./=-) it won't work correctly. That means it returns the lengths 37 But they doesn't have 37 characters!(For example it printed nothing and printed it's lenght 38.
I think it's a bit strange. :(
Why is that?! Which character causes that and how to fix it?
There are multiple issues with your code.
1. getRow() issue :
The problem is in getRow(), where you concat parameters into one string with ,, and then explode by ,. If your string has , inside it, then you will have problems, example: Stack ,test.
2. multibyte issue :
This code doesn't work for multibyte characters. To accomplish that you will need to change function str_shuffle() with function bellow mb_str_shuffle(), and strlen() with mb_strlen().
function mb_str_shuffle($str) {
$tmp = preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
shuffle($tmp);
return join("", $tmp);
}
Or some other unicode function that you find in comments on http://php.net/manual/en/function.str-shuffle.php
3. length is ok, but string is missing issue :
This is because you have HTML special chars in your string, like < and >. If string Stack Overflow+_)(*&^%$#{}[]<>#!~./=- is shuffled, and you get something like a#^&/c-_O. instead of a#^&/c-_O.< ~*>)$wevS+{(%}klr[]f=to!#. You should escape special chars with htmlspecialchars() when you output the string.
Sounds like a problem with Encoding.
Have you tried using a function, that handles encoding right?
try this: (copied from PHP Manual)
function unicode_shuffle($string, $chars, $format = 'UTF-8')
{
for($i=0; $i<$chars; $i++)
$rands[$i] = rand(0, mb_strlen($string, $format));
$s = NULL;
foreach($rands as $r)
$s.= mb_substr($string, $r, 1, $format);
return $s;
}
Related
I'm trying to parse a string and delete the adjacent letters that are same. I want to return the count of number of deletions and output the resulted string after the deletions are made. Say I have
$str = "aaabbbcc";
As you can see, you need to do 5 deletions to make the adjacent letters not same. The $output string is "abc" and the number of deletions is five.
function str_deletions ($str)
{
$prev = $str[0];
$length = strlen($str);
$deletions = 0;
$output = "";
for ($i=1 ; $i < $length; $i++)
{
if ($str[$i]== $prev)
{
$deletions++;
$prev = $str[$i]; // not sure here ?
}
}
echo $output; // ?
return $deletions;
}
$str = "aabbcc";
echo str_deletions ($str);
EDIT
This is an interview question, I'm not supposed to use any built-in functions like regex or array_count_values
Thanks for your help !
Here is another regex solution. I use a regex to only match a word character that is repeated, and then remove each consecutive repeating character one by one, which allows me to use &$count argument with preg_replace:
count
If specified, this variable will be filled with the number of replacements done.
The regex is
(\w)(?=\1)
See demo. Note you can replace \w with . to match any character but a newline. OR if you need to match only letters, I suggest using '/(\p{L})(?=\1)/u'
See IDEONE demo:
$str = "aaabbbcc";
$cnt = -1;
$result = preg_replace('/(\w)(?=\1)/', "", $str, -1, $cnt);
echo "Result: " . $result . PHP_EOL . "Deletions: " . $cnt;
Output:
Result: abc
Deletions: 5
Regex solution
This is a much simpler way of doing what you're after using preg_replace():
<?php
function str_deletions($str){
$replaced = preg_replace("/(.)\\1+/", "", $str);
$length = strlen($str) - strlen($replaced);
return array("new_word" => $replaced, "chars_replaced" => $length);
}
$str = "aabbcc";
$string_deletions = str_deletions($str);
echo "New String: " . $string_deletions['new_word'] . "\n";
echo "Chars deleted: " . $string_deletions['chars_replaced'] . "\n";
?>
No inbuilt functions
For the purposes of completion (and since you updated your question with more information to say that we can't use regexes because it's an interview question), here's what I'd do:
Using count_chars():
function str_deletions($str){
$string_data['new_word'] = count_chars($str,3);
$string_data['chars_replaced'] = strlen($str) - strlen($string_data['new_word']);
return $string_data;
}
$str = "aabbcc";
echo str_deletions($str);
Note: in this example count_chars(); will return unique chars in a string, not quite remove duplicates (i.e. "aabbccaa" would still yield "abc" as an output) but your question wasn't clear what the interviewer wanted - whether it was truly a remove duplicate question or a unique char question.
Using array_unique():
Slightly slower and a bit more heavy handed:
function str_deletions($str){
$string_array = array_unique(str_split($str));
foreach($string_array as $string_cur){
$string_data['new_word'] .= $string_cur;
}
$string_data['chars_replaced'] = strlen($str) - strlen($string_data['new_word']);
return $string_data;
}
$str = "aabbcc";
echo str_deletions($str);
Note: It's worth pointing out that if I realised it was an interview question, I wouldn't have provided an answer as doing it for you kind of defeats the purpose. Still, with the amount of answers here now and the fact that I've seen something similar to this in an interview, my hope is someone will learn from these.
The basic algorithm (indeed $prev = $str[$i]; isn't at the good place but you wasn't far):
function str_deletion($str) {
$del = 0;
if (1 < $length = strlen($str)) { // $str has more than 1 char
$prev = $str[0];
$output = $prev;
for ($i=1; $i<$length; $i++) {
if ($prev == $str[$i]) {
$del++;
} else {
$prev = $str[$i]; // when different, change the previous character
$output .= $prev; // and append it to the output
}
}
} else {
$output = $str;
}
echo $output;
return $del;
}
I have changed your function
this is not returning both the output string and number of deletions
function str_deletions ($str)
{
$prev = NULL;
$deletions = 0;
$output = "";
$i=0;
while ($i < strlen($str))
{
if (substr($str,$i,1) == $prev)
{
$deletions++;
//$prev = substr($str,$i,1);/*remove this line, no need here as the same stmnt is there after ifelse*/
}else{
$output.=substr($str,$i,1);
}
$prev = substr($str,$i,1);
$i++;
}
$arr = array(
'output'=>$output,
'deletions'=>$deletions
);
return $arr;
}
$str = "aaabbcc";
print_r(str_deletions ($str));
output for above function call is
Array ( [output] => abc [deletions] => 4 )
Solved with no external function except count;
$str="aaavvvffccca";
$count = strlen($str);
for($i=0;$i<$count;$i++){
$array[]=$str[$i];
}
$del =0;
for($i=0;$i<$count;$i++){
$next=isset($array[$i+1])?$array[$i+1]:null;
if($array[$i]==$next)
{
$del++;
}
else
{
$newarray[]=$array[$i];
}
}
echo "Filter Text:". implode('',$newarray);
echo"Total Deleted:".$del;
The straight forward solution to find out the number of deletions can be
If there are N consecutive same characters delete N-1 out of those N characters.
function str_likes($str)
{
$length = strlen($str);
$del = 0;
for ($i=0 ; $i < $length ; $i++)
{
if ($str[$i] == $str[$i+1])
{
$del++;
}
}
return $del;
}
$str = "aabbccaaa";
echo str_likes($str); //4
I have to determine the position of a special character in the string for example:
E77eF/74/VA on 6 and 9 position (counting from 1)
we have '/' so I have to change them to position number -> E77eF6749VA
On MSSQL I could use PATINDEX but I need to use php for this.
It should work for everything except 0-9a-zA-Z
I found strpos() and strrpos() on php.net but I doens't work well for me.
Anyway tries to do something like that?
<?php
$content = 'E77eF/74/VA';
//With this pattern you found everything except 0-9a-zA-Z
$pattern = "/[_a-z0-9-]/i";
$new_content = '';
for($i = 0; $i < strlen($content); $i++) {
//if you found the 'special character' then replace with the position
if(!preg_match($pattern, $content[$i])) {
$new_content .= $i + 1;
} else {
//if there is no 'special character' then use the character
$new_content .= $content[$i];
}
}
print_r($new_content);
?>
Output:
E77eF6749VA
Might not be the most efficient way, but works.
$string = 'E77eF/74/VA';
$array = str_split($string);
foreach($array as $key => $letter){
if($letter == '/'){
$new_string.= $key+1;
}
else{
$new_string.= $letter;
}
}
echo $new_string; // prints E77eF6749VA
I'm using an RTF converter and I need 240 as &#U050&#U052&#U048 but Im not to sure how to do this!?!
I have tried using the following function:
function string_to_ascii($string) {
$ascii = NULL;
for ($i = 0; $i < strlen($string); $i++) {
$ascii += "&#U"+str_pad(ord($string[$i]),3,"0",STR_PAD_LEFT);
}
return($ascii);
}
But it still just outputs just the number (e.g. 2 = 50) and ord just makes it go mad.
I've tried echo "-&#U"+ord("2")+"-"; and I get 50416 !?!?
I have a feeling it might have something to do with encoding
I think you're over thinking this. Convert the string to an array with str_split, map ord to all of it, then if you want to format each one, use sprintf (or str_pad if you'd like), like this:
function string_to_ascii($string) {
$array = array_map( 'ord', str_split( $string));
// Optional formatting:
foreach( $array as $k => &$v) {
$v = sprintf( "%03d", $v);
}
return "&#U" . implode( "&#U", $array);
}
Now, when you pass string_to_ascii( '240'), you get back string(18) "&#U050&#U052&#U048".
Just found this:
function to_ascii($string) {
$ascii_string = '';
foreach (str_split($string) as $char) {
$ascii_string .= '&#' . ord($char) . ';';
}
return $ascii_string;
}
here
how can i convert it into float and then increment it and then convert back to string.
if($set==$Data_Id)
{
$rel='1.1.1.2';
}
after increment it should be like 1.1.1.3.
Please any help.
so crazy, it may work
$rel='1.1.1.2';
echo substr($rel, 0, -1). (substr($rel,-1)+1); //1.1.1.3
the big question is what do you want to happen if the string ends in 9 ??
Here's a slightly different approach.
<?php
function increment_revision($version) {
return preg_replace_callback('~[0-9]+$~', function($match) {
return ++$match[0];
}, $version);
}
echo increment_revision('1.2.3.4'); //1.2.3.5
Anthony.
"1.1.1.2" is not a valid number. So you'll have to do something like this:
$rel = '1.1.1.2';
$relPlusOne = increment($rel);
function increment($number) {
$parts = explode('.', $number);
$parts[count($parts) - 1]++;
return implode('.', $parts);
}
If this is exactly the case you need to solve, you could do it with intval(), strval(), str_replace(), substr() and strlen().
$rel = '1.1.1.2'; // '1.1.1.2'
// replace dots with empty strings
$rel = str_replace('.', '', $rel); // '1112'
// get the integer value
$num = intval($rel); // 1112
// add 1
$num += 1; // 1113
// convert it back to a string
$str = strval($num); // '1113'
// initialize the return value
$ret = '';
// for each letter in $str
for ($i=0; $i<strlen($str); $i++) {
echo "Current ret: $ret<br>";
$ret .= $str[$i] . '.'; // append the current letter, then append a dot
}
$ret = substr($ret, 0, -1); // remove the last dot
echo "Incremented value: " . $ret;
This method will change 1.1.1.9 to 1.1.2.0, however. If that's what you want, then this will be fine.
I have the following issue;
Here is my string I'm trying to remove javascript:l(" from the string below:
javascript:l("Bayou-La-Batre");
My code is;
$q_string = str_replace('javascript:l(" ','',$row['1']);
But it's failing.
This is quicker than a regex replace and quicker.
Any ideas?
You left a space
$q_string = str_replace('javascript:l("','',$row['1']);
Change
$q_string = str_replace('javascript:l(" ','',$row['1']);
to
$q_string = str_replace('javascript:l("','',$row['1']);
Whenever I'm having this kind of problem, I try formatting my search/replace call in a monospaced editor to see if anything pops out
$q_string = str_replace('javascript:l(" ', '',
'javascript:l("Bayou-La-Batre")';
Formated like that, it becomes obvious that the 15th character of the search string does not match the 15th characters of the string that's being searched ([ ] vs. [B]).
Try removing that whitespace and you should be happy.
You can use
$q_string = substr($row['1'], 14);
to get the same results with some speed gain. I have tested speeds of three function so you can see how they compare with speed.
$string = 'javascript:l("Bayou-La-Batre")';
$str_replace = function($string) {
return str_replace('javascript:l("', '', $string);
};
print $str_replace($string) . "\n";
$substr = function($string) {
return substr($string, 14);
};
print $substr($string) . "\n";
$preg_replace = function($string) {
return preg_replace('/^[a-z:\(]+"/', '', $string);
};
print $preg_replace($string) . "\n";
function measure($function, $string) {
$start = microtime(true);
for($i = 0; $i < 1000000; $i++) {
$function($string);
}
return microtime(true) - $start;
}
print 'Substr: ' . measure($substr, $string) . "\n";
print 'Str replace:' . measure($str_replace, $string) . "\n";
print 'Preg replace:' . measure($preg_replace, $string);
The output of this on my machine
Bayou-La-Batre")
Bayou-La-Batre")
Bayou-La-Batre")
Substr: 3.7492098808289
Str replace:4.5258920192719
Preg replace:5.8815109729767