Strip illegal characters - php

I have this function that strips illegal characters. I found it here http://php.net/manual/en/function.strtr.php, function fixoutput($str)
So, this is my code.
<?php
$stuff = 'Foo ◻◻◻◻◻◻◻◻◻◻◻◻';
function fix_output($str){
$newstr = '';
$good[] = 9; #tab
$good[] = 10; #nl
$good[] = 13; #cr
for($a=32;$a<127;$a++){
$good[] = $a;
}
$len = strlen($str);
$strs = array();
for($b=0;$b < $len+1; $b++){
if(in_array(ord($str[$b]), $good)){
$newstr .= $str[$b];
}//fi
}//rof
return $newstr;
}
echo fix_output($stuff);
echo '<br>'.$stuff;
And I have this output.
Notice: Uninitialized string offset: 40 in /<directory>/foo/foo.php on line 17
Foo
Foo ◻◻◻◻◻◻◻◻◻◻◻◻
I want a fix for this notice.
I'm having this notice because is $str a string in this context, not an array. I am are trying to treat it like an array, which doesn't work. I'm having trouble in creating a fix for this, could you guys lend me some ideas? Thanks a bunch!
Please mention the things that are unclear.

Related

Warning: Illegal offset type in

I have some problem with this code. Warning: Illegal offset type in Line 22
$this->word[$kata][]=array($i,$j);
and the full code is below
private $jmldoc = 0; private $word = array();
public function getIndex($D) {
$this->jmldoc = count($D);
for($i=0; $i<$this->jmldoc; $i++) {
$pp = new prePro($D[$i]);
$kata = $pp->tokenize();
$n = count($kata);
for($j=0; $j<$n; $j++) {
$this->word[$kata]=array($i,$j);
}
}
}
Can you help me to fix it?
You are passing an array, not a string/integer index to your $this->word.
//I suppose from the context of your code that $kata is an array also
//so if that's true, it can't be used as an index
$this->word[$kata][]=array($i,$j);
Keep in mind that $this->word is an array. So probably there is something wrong with your program logic. To fix this, use an integer or string to access the elements of an array.

Error using substr()

I am getting error while using substr:
Warning: substr() expects parameter 3 to be long
I am new to php and could not locate the problem. I would appreciate any help.
Here is the code:
function prepare_string($passed_string,$length)
{
$matches = array("`","!","#","®","©","~","#","$","%","^","&","*","-","=","+","|","\\","[","{","]","}","(",")",";",":","\"","'",",","<",">",".","?","/","\'","\\","'","’");
$passed_string =substr($passed_string,0,$length);
for($i=0;$i<count($matches);$i++)
{
$passed_string = str_replace($matches[$i],"_",$passed_string);
}
$passed_string = str_replace(" ","_",$passed_string);
return $passed_string;
}
var_dump($length) and see what's in there.
remove foreach loop and instead put just this line $passed_string = str_replace($matches,"_",$passed_string);
add " " in $matches array too and then you can get rid of this line $passed_string = str_replace(" ","_",$passed_string);
Sounds like substr() is not receiving a well-formed value for some reason... One option would be to add a line inside the function that casts $length as an integer. Example:
function prepare_string($passed_string,$length)
{
// Add this line
$length = (int) $length;
$matches = array("`","!","#","®","©","~","#","$","%","^","&","*","-","=","+","|","\\","[","{","]","}","(",")",";",":","\"","'",",","<",">",".","?","/","\'","\\","'","’");
$passed_string =substr($passed_string,0,$length);
for($i=0;$i<count($matches);$i++)
{
$passed_string = str_replace($matches[$i],"_",$passed_string);
}
$passed_string = str_replace(" ","_",$passed_string);
return $passed_string;
}

match two strings and compare each letter in php

I googled this question I can't to find the exact solution...
I have 2 variables...
$s1 = "ABC"; //or "BC"
$s2 = "BC"; //or "Bangalore"
I have to compare $s1 and $s2 and give the output as letters which is not present in $s2
eg : "A" // or"C"
Like that
I have to compare $s2 and $s1 and give the output as letters which is not present in $s1
eg : null // or"angalore"
What I tried..
I spit the strings to array...
Using nested for loop to find the non matched letters...
I wrote code more than 35 lines..
But no result :(
Please help me ......
echo str_ireplace(str_split($s2), "", $s1); // output: A
You can use array_diff() here:
function str_compare($str1, $str2)
{
$str1chars = str_split($str1);
$str2chars = str_split($str2);
$diff = array_diff($str1chars, $str2chars)
return implode($diff);
}
By calling the function as follows:
$diffchars = str_compare('ABC', 'BC');
You will receive a string containing the characters that do not appear in both strings. In this example, it'll be A, because that character appears in $str1, but not in $str2.
You can use str_split and array_diff like :
<?php
$s1 = 'abcedf';
$s2 = 'xzcedf5460gf';
print_r(array_diff(str_split($s1), str_split($s2)));
Use array_diff():
function str_diff($str1, $str2) {
$arr1 = str_split($str1);
$arr2 = str_split($str2);
$diff = array_diff($arr1, $arr2);
return implode($diff);
}
Usage:
echo str_diff('BC', 'Bangalore'); // => C
echo str_diff('ABC', 'BC'); // => A
Ok to do this
$str1s = "abc";
$str2s = "BCd";
function findNot($str1, $str2, $asArray = false){
$returnValue = array_diff(array_unique(str_split(strtolower($str1))), array_unique(str_split(strtolower($str2))));
if($asArray == false){
return implode($returnValue);
}else{
return $returnValue;
}
}
echo findNot($str1s, $str2s); //gives a string
echo findNot($str1s, $str2s, true); //gives array of characters
This allows you to return as either array or string.

Native PHP function for hex string representation?

Is there a native PHP function for converting a string to its HEX representation that can be then eval()'d as a string, such as:
"ABC" => "\x41\x42\x43"
I know it can be done in several steps, I'm just wondering if I'm unnecessarily complicating something that could be done with a single, native function?
There is no native function, but you can use hex2bin to eval PHP code which is in hex, e.g.
eval(hex2bin("6563686f20706928293b")); # Output: 3.14159265359
eval(hex2bin(bin2hex("echo pi();"))); # Behind the scenes.
Or you can call the function which name is in hex string:
$ echo '<?$_=hex2bin(7069);die($_());' | php # 7069 = pi
3.14159265359
There is no built in PHP function to achieve the same results, but with some PHP-Fu you can do it in one line:
$str = 'ABC';
$str = '\x'.implode('\x', str_split(bin2hex($str), 2));
echo $str; // \x41\x42\x43
There is also a piece of code in the PHP docs which gives exactly the same results:
$str = 'ABC';
$field=bin2hex($str);
$field=chunk_split($field,2,"\\x");
$field= "\\x" . substr($field,0,-2);
echo $field; // \x41\x42\x43
$n = 1234;
echo printf("%x", $n); //should return the hexadecimal format of the number
Why don't you try something like this?
function strtohex($string) {
if (!empty($string)) {
$output = null;
$count = strlen($string, "UTF-8");
for ($i = 0; $i < $count; $i++) {
$output .= dechex(ord($string[$i]));
}
return $output;
}
}
For one-liners, the only alternative I can think about is the bin2hex() function, which is native in PHP and it offers the same.

Weird PHP Behavior

<?php
$dog[] = "12";
$dog[] = "3";
for ($i = 0; $i < 2; $i++) {
$dig = $dog[i];
echo $dig;
}
?>
$dig is always null. Why?
i is not a variable, use $i
If you had error_reporting(E_ALL) on, as you should when in development, you would have caught it immediately (undefined constant).
$dig = $dog[i];
should be:
$dig = $dog[$i];
Easy. You want $dog[$i]. The PHP engine looks for a constant name i, can't find one, so resorts to looking for string. No a key with value 'i' either, so returns NULL.
your missing the $ in this line
$dig = $dog[i];
should be
$dig = $dog[$i];
you could also simplify this code by writing it this way
<?php
$dogs[] = "12";
$dogs[] = "3";
foreach($dogs as $dog) {
echo $dog;
}
?>
You want
$dig = $dog[$i];
You missed the $

Categories