How can I convert non-asciis to numbers in php? - php

I just started to code in php, and I got a task to do.
I have to read non-ascii characters like the " character from a file, convert them to a string array, the convert into integers,and add numbers to them.
For example, if I have the '"' character, I should get 147, then if I add -31 to it, it should return the character 't'.
Here is a code i tried;
$file = fopen("word.txt", "r") or die("File missing");
$wholefile=fread($file, filesize("word.txt"));
fclose($file);
$words =explode(chr(0x0A), $wholefile);
$file = fopen("password.txt", "r") or die("File missing");
$wholefile=fread($file, filesize("password.txt"));
fclose($file);
$words =explode(chr(0x0A), $wholefile);
//An exmaple
$str = $words[0];
for($i = 0; $i< strlen($str); $i++)
{
$str[$i] = chr(ord($str[$i]) - 31);
}
echo "$str";
Can you help me to get ascii characters isntead of ��-s?
The content of the file (exmaple)
pS“`nf2†ddn^MZrrŠ

Related

Get text on next line of value found PHP

I have the following text file called people.txt with the contents:
mikey.mcgurk
Boss Man
michelle.mcgurk
Boss Man 2
I'd like to adjust my PHP script to grab the data on the line that follows each username, so if I was searching for mikey.mcgurk, my script would output Boss Man.
PHP:
<?php //
$file = 'people.txt';
$searchfor = "mikey.mcgurk";
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
// write all of this to a text file
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
you can do like this
$contents = file_get_contents($file);
$contents = explode(PHP_EOL, $contents);
if(array_search($searchfor, $contents) !== false){
echo $contents[array_search($searchfor, $contents)+1];
}
You can compare like this:
$contents = file_get_contents($file);
$lines = explode("\n",$contents);
for($i = 0; $i < count($lines); $i++) {
if( $lines[$i] ==$searchfor ) {
echo "Username ".$lines[$i+1];
}
}
Instead of using regular expression you can do this by getting all lines in an array
$lines = explode(PHP_EOL, $contents);
then get keys of results
$keys = array_keys($lines, $pattern);
and increment keys by 1 to get the next line
foreach ($keys as $key) {
echo $lines[++$key];
}

PHP replace character in file without open file

I have a file, that contain:
file content NULNULNULNUL something else.
When I try get file content
$str = file_get_contents($fileName), in $str appear just file content.
How I can replace the NUL in file, without open file?
1.) To get the replaced content as a string. First get the file content just like you did.
$str = file_get_contents($fileName)
Then replace all occurances of NUL with a blank space like:
$new_content = str_replace('NUL'," ",$str);
OR
2.) To replace the content inside the file (to replace content and write it back to the file):
file_put_contents($fileName,str_replace('NUL',' ',file_get_contents($fileName)));
here we are using file_put_contents — which writes a string to a file. We pass the filename and the replaced-string as a parameter
I convert each character to assci code. This is work now:
$newStr='';
$str = file_get_contents($fileName);
$length = strlen($str);
for ($i=0; $i < $length; $i++)
{
$current = ord($str{$i});
if ($current != 0x0)
{
$newStr .= chr($current);
}
else
{
$newStr .= "";
}
}
$num = file_put_contents($fileName,$newStr);

How to convert all the characters in a file to ascii numbers in php

I want to covert all the characters in a file to ASCII code in php? I know of ord function but whether there is any function that will do for the entire file?
iconv may do the work
http://php.net/manual/de/function.iconv.php
it convertes chars of a specified charset in a string to another one. look at the //TRANSLIT and //IGNORE specials for chars that cannot be converted 1:1.
to get the file in a string you can use file_get_contents and save it after iconv etc. is applied with file_put_contents.
$inputFile = fopen("input.txt", "rb");
$outputFile = fopen("output.txt", "w+");
while (!feof($inputFile)) {
$inputBlock = fread($inputFile, 8192);
$outputBlock = '';
$inputLength = strlen($inputBlock);
for ($i = 0; $i < $inputLength; ++$i) {
$outputBlock .= str_pad(dechex(ord($inputBlock{$i})),2,'0',STR_PAD_LEFT);
}
fwrite($outputFile,$outputBlock);
}
fclose($inputFile);
fclose($outputFile);

php for loop to remove common words

I have a text file that maintains a list of words.
What I am trying to do is pass a string(sentence) to this function and remove the word from the string if it exists in the text file.
<?php
error_reporting(0);
$str1= "the engine has two ways to run: batch or conversational. In batch, expert system has all the necessary data to process from the beginning";
common_words($str1);
function common_words($string) {
$file = fopen("common.txt", "r") or exit("Unable to open file!");
$common = array();
while(!feof($file)) {
array_push($common,fgets($file));
}
fclose($file);
$words = explode(" ",$string);
print_r($words);
for($i=0; $i <= count($words); $i+=1) {
for($j=0; $j <= count($common); $j+=1) {
if($words[$i] == $common[$j]){
unset($words[$i]);
}
}
}
}
?>
It doesn't seem to work however. The common words from the string are not being removed. instead I am getting the same string with the one I started.
I think I am doing the loop wrong. What is the correct approach and what am I doing wrong?
on the line
if($words[$i] == $common[$j]){
change it to
if(in_array($words[$i],$common)){
and remove the second for loop.
Try using str_replace():
foreach($common as $cword){
str_replace($cwrod, '', $string); //replace word with empty string
}
Or in full:
<?php
error_reporting(0);
$str1= "the engine has two ways to run: batch or conversational. In batch, expert system has all the necessary data to process from the beginning";
common_words($str1);
function common_words(&$string) { //changes the actual string passed with &
$file = fopen("common.txt", "r") or exit("Unable to open file!");
$common = array();
while(!feof($file)) {
array_push($common,fgets($file));
}
fclose($file);
foreach($common as $cword){
str_replace($cword, '', $string); //replace word with empty string
}
}
?>

PHP get array of words from txt file

I have a text file with spam words. I want to have an array filled with those words.
I've tried doing:
$fp = #fopen("../files/spam.txt",'rb');
$words = fgetcsv($fp,100,"\n");
but it doesn't work (words only has the first letter of the txt file in it first cell).
do you how to do this?
EDIT:
the .txt file looks like this:
yahoo
google
msn
blah
blah
EDIT:
I DONT KNOW WHAT IS A CSV FILE! THIS IS A TEXT FILE! I JUST GIVE AN EXAMPLE.
please could some1 help me it looks really easy, i just dont understand.
That is not a CSV file. CSV stands for comma separated values. You have no commas!
$spam_words = file('../files/spam.txt', FILE_IGNORE_NEW_LINES);
All you need to do is:
$words = file('./files/spam.txt',FILE_IGNORE_NEW_LINES);
$artic = array(); //create a array
$directory = "/var/www/application/store/"; //define path
$files1 = scandir($directory); //scan the directory
$c = count($files1); //count the files the directory
print $c; //print it
for($i = 2; $i < $c; $i++) {
print "<br />" . $files1[$i];
$f = $directory . $files1[$i];
print $f . "<br />";
$h = fopen($f, 'r') or die("cannot open a file!!". $f);
$line1 = fgets($h);
list($id, $idval) = explode("\t\t", $line1);
print "$id";
}
How about splitting the string you read from the file into an array?
split() function definition.
$string = "Niagara Becks Corn";
$array = split(" ", $string);
# ["Niagara", "Becks", "Corn"]
Use the 'file' function to read a PHP file from the disk into an array. See the manual here: http://php.net/manual/en/function.file.php
Try this
$file = file_get_contents('spam.txt');
$file_words = explode(" ", $file);
$file_count = count($file_words);
for ($i=0; $i < $file_count; $i++){
echo $file_words[$i] . "<br />";
}
Your spam.txt file should look like this:
yahoo msn google

Categories