split text by characters (with file_get_contents and file_put_contents) - php

I have the following code which works exactly as it should:
<?php
$text = "text"; $text_ok = urlencode($text);
if(!#file_get_contents("http://site.com/t=".$text_ok))
{
echo "Error.";
}
else
{
$data = file_get_contents("http://site.com/t=".$text_ok);
$file = "texts/".md5($text).".txt";
if(!file_exists($file)) {
file_put_contents($file, $data);
}
?>
Lorem <?php echo $file; ?>"> ipsum
<?php
}
?>
The problem is that http://site.com/t=$text_ok only works if $text is less than 25 characters. I'm wondering if it is possible when $text exceeds 25 characters to split into multiple parts and create files like texts/md5($text)/1.txt, texts/md5($text)/2.txt etc. I hope you understand. Any help is appreciated. Thanks!

Try this:
<?php
$text = "text";
$split = str_split($text, 25);
$count = 1;
foreach ($split as $s) {
$text_ok = urlencode($s);
if (!#file_get_contents("http://site.com/t=" . $text_ok)) {
echo "Error.";
} else {
$data = file_get_contents("http://site.com/t=" . $text_ok);
$file = "texts/" . md5($text) . "/" . $count . ".txt";
if (!file_exists($file)) {
file_put_contents($file, $data);
}
?>
Lorem <?php echo $file; ?>"> ipsum
<?php
}
$count++;
}
?>

Related

Find a phrase in a string

My code below is working
<?php
//load synonyms of words
$json_file = fopen("dict.json", "r") or die("Unable to open file!");
$js = fread($json_file, filesize("dict.json"));
$json = json_decode($js, true);
$text = "Hello my friend, today i am feeling good.";
$ar_text = explode(" ", $text);
foreach ($ar_text as $val)
{
$rand = rand(1, 3);
$randx = rand(1, 3);
if ($rand == $randx)
{
if (array_key_exists($val, $json))
{
$null = "{" . $val . "|";
$inc = $json[$val]['sinonim'];
$i = 1;
foreach ($inc as $siap)
{
$null .= $siap . "|";
if ($i == 4) break;
$i++;
}
$null .= "}";
$text = str_replace(" $val ", " $null ", $text);
//echo $null."<br>";
}
else
{
//echo "not found ".$val."<br>";
}
}
//echo $val;
}
$text = str_replace("|}", "}", $text);
echo $text;
//echo $json['mengaras']['tag'];
?>
i use explode to get word by word and then replace with word synonyms, how to get a phrase like "really good" and find it on dict.json.
Example: Hello, i am really good right now.
Output: Hello, i am {so fine|super fine|superb} now.
Use str_replace() - Replace all occurrences of the search string with the replacement string
$text = "Hello, i am really good right now.";
$find = "really good";
$replace = "so fine|super fine|superb";
$newtext= str_replace($find, $replace, $text);
OUTPUT:
Hello, i am so fine|super fine|superb right now.
You can do it this way too:
$text = 'Hello, i am really good right now.';
$match = 'really good';
$match_len = strlen($match);
$replace = array("so fine","super fine", "superb");
$pos = strpos($text, $match);
if($pos !== false){
echo "Word Found!";
$replace = implode("|", $replace);
$embed = '{' . $replace . '}';
echo $text_before . ' ' . $embed . ' ' . $text_after;
} else{
echo "Word Not Found!";
}
Output
Hello, i am {so fine|super fine|superb} right now.

Strict standard error in PHP

I've looked at similar posts here but couldn't crack a solution. I'm getting the Strict Standards: Only variables should be passed by reference error here on the strtolower($file['name']))), line. Tried hours now looking for a solution but couldn't figure out how to correct. Can you highlight where I've gone wrong?
Note: Code Edited as per #Kolink's suggestion below:
<?php
require_once 'upload_config.php';
$mydirectory = myUploadDir();
$uploaded_file_counter = 0;
$UploadLimit = $_POST['counter'];
for ($i = 0; $i <= $UploadLimit; $i++) {
$file_tag = 'filename' . $i;
$filename = $_FILES[$file_tag]['name'];
if ($filename != null) {
$rand = time();
$str = "$rand$filename";
// set folder name in here.
$filedir = myUploadDir();
//change the string format.
$string = $filedir . $str;
$patterns[0] = "/ /";
$patterns[1] = "/ /";
$patterns[1] = "/ /";
$replacements[1] = "_";
$dirname = strtolower(preg_replace($patterns, $replacements, $string));
//end of changing string format
//checking the permitted file types
if ($check_file_extentions) {
$allowedExtensions = allowedfiles();
foreach ($_FILES as $file) {
if ($file['tmp_name'] > '') {
/*if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) */
if (!in_array(array_reverse(explode(".", strtolower($file['name'])))[0], $allowedExtensions)) {
$fileUploadPermission = 0;
} else {
$fileUploadPermission = 1;
}
}
}
} else {
$fileUploadPermission = 1;
}
//end of checking the permitted file types
if ($fileUploadPermission) {
if (move_uploaded_file($_FILES[$file_tag]['tmp_name'], $dirname)) {
echo "<p>"; ?>
<div><?php echo "<img style='height:100px;width:200px' src='$dirname'>"; ?></div> <?php
echo "</p>";
$uploaded_file_counter += 1;
}
}
}
}
if ($uploaded_file_counter == 0) {
echo "<br /> <b style='font-weight:bold;color:red'>Opss! Please select an image file<b>";
} else {
echo "<br /> <b>You request " . $i . " image files to upload and " . $uploaded_file_counter . " files uploaded sucessfully</b>";
}
?>
end takes its argument by reference, since it modifies the original array by moving its internal pointer.
Since you appear to be using PHP 5.4, you can do this instead to get the last one:
if( !in_array(array_reverse(explode(".",strtolower($file['name'])))[0],$allowedExtensions))
That said, it's probably better to do it in several steps for readability:
$parts = explode(".",strtolower($file['name']));
$extension = $parts[count($parts)-1];
if( !in_array($extension,$allowedExtensions)) {

Display same word in text file using PHP

I have text file as below:
text.txt
lebuzzdesbonsplans.com;hotmail.com;26608;828;3.11
lebuzzdesbonsplans.com;hotmail.fr;24798;876;3.53
friendcorp.fr;yahoo.fr;11343;0;0
friendcorp.fr;free.fr;9856;12;.12
friendcorp.fr;wanadoo.fr;9283;1;.01
messengear.fr;free.fr;9090;11;.12
messengear.fr;laposte.net;8107;2;.02
....................................
....................................
PHP code:
<?php
$PMTA_FILE = file_get_contents("text.txt");
$lineFromText = explode("\n", $PMTA_FILE);
$title = "";
$domain = "";
foreach($lineFromText as $line){
$data = explode(";",$line);
$domain = $data[0];
if (array_key_exists($domain, $domains_seen)){
continue;
}
$domains_seen[$domain] = true;
echo $domain;
echo "<br>";
echo $data[2];
echo "<br>";
}
?>
But the result of this code:
lebuzzdesbonsplans.com
26608
friendcorp.fr
11343
messengear.fr
9090
I do not want the result as above, I need the result as below:
lebuzzdesbonsplans.com
26608
24798
friendcorp.fr
11343
9856
9283
messengear.fr
9090
8107
Anyone know help me to get the solution please,Thanks.
You can use
$lines = array_reduce(file("text.txt", FILE_IGNORE_NEW_LINES), function ($a, $b) {
$b = str_getcsv($b, ";");
$a[$b[0]][] = $b[2];
return $a;
});
foreach($lines as $k => $values)
{
echo $k,PHP_EOL;
echo implode(PHP_EOL, $values);
echo PHP_EOL;
}
Output
lebuzzdesbonsplans.com
26608
24798
friendcorp.fr
11343
9856
9283
messengear.fr
9090
8107
natsort($lineFromText);
foreach($lineFromText as $line){
$data = explode(";",$line);
$domain = $data[0];
if (!array_key_exists($domain, $domains_seen)){
echo $domain;
echo "<br>";
}
echo $data[2];
echo "<br>";
$domains_seen[$domain] = true;
}
Update: sort the lines first - that way all the domains will be collected together,

PHP script adding a space in between every character

I have a little problem with a script I wrote.
It basically combines permutations of text in every possible order.
For some reason it is adding a space in between every character - can anyone figure out why?
Thanks in advance.
<?php
if(isset($_POST['submit']))
{
//pull in the perms
$perms = "{#A#B#C|#A#B#D|#A#B#E|#A#B#F|#A#C#D|#A#C#E|#A#C#F|#A#D#E|#A#D#F|#A#E#F|#B#C#D|#B#C#E|#B#C#F|#B#D#E|#B#D#F|#B#E#F|#C#D#E|#C#D#F|#C#E#F|#D#E#F}";
//open the box's
$box1= fopen("box1.txt","r");
$box2= fopen("box2.txt","r");
$box3= fopen("box3.txt","r");
$box4= fopen("box4.txt","r");
$box5= fopen("box5.txt","r");
$box6= fopen("box6.txt","r");
//this is the output
$tulis = fopen("output.txt","w+");
//read the box's
$box1 = fread($box1, filesize("box1.txt"));
$box2 = fread($box2, filesize("box2.txt"));
$box3 = fread($box3, filesize("box3.txt"));
$box4 = fread($box4, filesize("box4.txt"));
$box5 = fread($box5, filesize("box5.txt"));
$box6 = fread($box6, filesize("box6.txt"));
$perms = str_replace("#A","$box1",$perms);
$perms = str_replace("#B","$box2",$perms);
$perms = str_replace("#C","$box3",$perms);
$perms = str_replace("#D","$box4",$perms);
$perms = str_replace("#E","$box5",$perms);
$perms = str_replace("#F","$box6",$perms);
echo $text;
fwrite($tulis,$perms);
//close them properly
$box1= fopen("box1.txt","r");
$box2= fopen("box2.txt","r");
$box3= fopen("box3.txt","r");
$box4= fopen("box4.txt","r");
$box5= fopen("box5.txt","r");
$box6= fopen("box6.txt","r");
fclose($box1);
fclose($box2);
fclose($box3);
fclose($box4);
fclose($box5);
fclose($box6);
fclose($tulis);
}
//this means that if the submit button hasn't been pressed, print the form!
else
{
print '
<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<input type="submit" name="submit"></input>
</form>
';
}
?>
Unless I am mistaking and I missed what you are trying to do, this script is doing pretty much like what you wrote, except that there are no str_replace and it creates permuations automatically for the number of files you provide (at least 3 for it to work properly) :
$files = array(
"box1.txt",
"box2.txt",
"box3.txt",
"box4.txt",
"box5.txt",
"box6.txt"
);
$contents = array();
foreach ($files as $index => $filename) {
$contents[$index] = trim(file_get_contents($filename), " \n\r");
}
$perms = array();
$len = count($contents);
for ($i=0; $i<$len-2; $i++) {
for ($j=$i+1; $j<$len-1; $j++) {
for ($k=$j+1; $k<$len; $k++) {
$perms[] = $contents[$i] . $contents[$j] . $contents[$k];
}
}
}
$text = '{' . implode('|', $perms) . '}';
file_put_contents('output.txt', $text);
Note that this version does not use fopen and trim the text read from the files to remove any whitespaces (CR and CL characters too) from the content.
** Edit **
This is the actual test program that I have made :
header('Content-type: text/plain; charset=utf-8');
$contents = array(
'A', 'B', 'C', 'D', 'E', 'F'
);
// fixed embedded loops method
$perms = array();
$len = count($contents);
for ($i=0; $i<$len-2; $i++) {
for ($j=$i+1; $j<$len-1; $j++) {
for ($k=$j+1; $k<$len; $k++) {
$perms[] = $contents[$i] . $contents[$j] . $contents[$k];
}
}
}
$text = '{' . implode('|', $perms) . '}';
echo $text . "\n";
// Recursive method
function permutationRecursive( & $perms, $maxDepth, $contents = array(), $values = array(), $startIndex = 0) {
for ($i=$startIndex, $count=count($contents)-($maxDepth-1); $i<$count; $i++) {
array_push($values, $contents[$i]);
if ($maxDepth > 1) {
permutationRecursive( $perms, $maxDepth - 1, $contents, $values, $i + 1);
} else {
$perms[] = implode($values);
}
array_pop($values);
}
}
$perms = array();
permutationRecursive($perms, 3, $contents);
$text = '{' . implode('|', $perms) . '}';
echo $text . "\n";;
The output of this script is
{ABC|ABD|ABE|ABF|ACD|ACE|ACF|ADE|ADF|AEF|BCD|BCE|BCF|BDE|BDF|BEF|CDE|CDF|CEF|DEF}
{ABC|ABD|ABE|ABF|ACD|ACE|ACF|ADE|ADF|AEF|BCD|BCE|BCF|BDE|BDF|BEF|CDE|CDF|CEF|DEF}

how to extract texts from PDFs using xpdf?

I have many PDFs in a folder. I want to extract the text from these PDFs using xpdf. For example :
example1.pdf extract to example1.txt
example2.pdf extract to example2.txt
etc..
here is my code :
<?php
$path = 'C:/AppServ/www/pdfs/';
$dir = opendir($path);
$f = readdir($dir);
while ($f = readdir($dir)) {
if (eregi("\.pdf",$f)){
$content = shell_exec('C:/AppServ/www/pdfs/pdftotext '.$f.' ');
$read = strtok ($f,".");
$testfile = "$read.txt";
$file = fopen($testfile,"r");
if (filesize($testfile)==0){}
else{
$text = fread($file,filesize($testfile));
fclose($file);
echo "</br>"; echo "</br>";
}
}
}
I get blank result. What's wrong with my code?
try using this :
$dir = opendir($path);
$filename = array();
while ($filename = readdir($dir)) {
if (eregi("\.pdf",$filename)){
$content = shell_exec('C:/AppServ/www/pdfs/pdftotext '.$filename.' ');
$read = strtok ($filename,".");
$testfile = "$read.txt";
$file = fopen($testfile,"r");
if (filesize($testfile)==0){}
else{
$text = fread($file,filesize($testfile));
fclose($file);
echo "</br>"; echo "</br>";
}
}
You do not have to create a temporary txt file
$command = '/AppServ/www/pdfs/pdftotext ' . $filename . ' -';
$a = exec($command, $text, $retval);
echo $text;
if it does not work check the error logs of the server.
The lines
echo "</br>";
echo "</br>";
should be
echo "</br>";
echo $text."</br>";
Hope this helps

Categories