I'm looking at trying to get a file's contents and preview the first 50 words, excluding titles, and I've hit a snag.
$getPage = $_SERVER['QUERY_STRING'];
$page = "news/" . $getPage . ".php";
$directory = 'news/';
$scanned_directory = array_diff(scandir($directory, 1), array('..', '.'));
if (file_exists($page)) {
include $page;
} else {
foreach ($scanned_directory as $value) {
$file = file_get_contents('news/' . $value);
$less_words = implode(' ', array_slice(explode(' ', $file), 0, 50));
$result = preg_replace('/\<.*?\>|\s*/', '', $less_words);
echo '<p>$result ...<br> Read more</p>';
/* TO ADD: URL variable */
}
}
The issue I am having with this is when echoing $less_words the text outputted is correct, however the <h1></h1> tags in the 2 example files I have show, and are formatted. But when echoing $result the text outputted on the page is $result Read more.
The php function http://php.net/manual/en/function.strip-tags.php would work...
$file = file_get_contents('news/' . $value);
$file = strip_tags($file);
$less_words = implode(' ', array_slice(explode(' ', $file), 0, 50));
echo '<p>'.$less_words.'...<br> Read more</p>';
Change
echo '<p>$result ...<br> Read more</p>';
/* TO ADD: URL variable */
to
echo '<p>'.$result.' ...<br> Read more</p>';
/* TO ADD: URL variable */
Related
Here is my code which is a partially based on a few different codes that you can find easily in various places if googled. I'm trying to count the internal and external links, all links and ( TO DO .nofollow ) links on a any webpage. This is what I have till now. Most of the results are correct, some generic calls gives me a weird results though, and I still need to do .nofollow and perhaps _blank as well. If you care to comment or add/change anything with bit of logic explanation then please do so, it will be very appreciated.
<?php
// transform to absolute path function...
function path_to_absolute($rel, $base)
{
/* return if already absolute URL */
if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;
/* queries and anchors */
if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
/* parse base URL and convert to local variables:
$scheme, $host, $path */
extract(parse_url($base));
/* remove non-directory element from path */
$path = preg_replace('#/[^/]*$#', '', $path);
/* destroy path if relative url points to root */
if ($rel[0] == '/') $path = '';
/* dirty absolute URL */
$abs = "$host$path/$rel";
/* replace '//' or '/./' or '/foo/../' with '/' */
$re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
/* absolute URL is ready! */
return $scheme.'://'.$abs;
}
// count zero begins
$intnumLinks = 0;
$extnumLinks = 0;
$nfnumLinks = 0;
$allnumLinks = 0;
// get url file
$url = $_REQUEST['url'];
// get contents of url file
$html = file_get_contents($url);
// http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php
// loading DOM document
$doc=new DOMDocument();
#$doc->loadHTML($html);
$xml=simplexml_import_dom($doc); // just to make xpath more simple
$strings=$xml->xpath('//a');
foreach ($strings as $string) {
$aa = path_to_absolute( $string[href], $url, true );
$a = parse_url($aa, PHP_URL_HOST);
$a = str_replace("www.", "", $a);
$b = parse_url($url, PHP_URL_HOST);
if($a == $b){
echo 'call-host: ' . $b . '<br>';
echo 'type: int </br>';
echo 'title: ' . $string[0] . '<br>';
echo 'url: ' . $string['href'] . '<br>';
echo 'host: ' . $a . '<br><br>';
$intnumLinks++;
}else{
echo 'call-host: ' . $b . '<br>';
echo 'type: ext </br>';
echo 'title: ' . $string[0] . '<br>';
echo 'url: ' . $string['href'] . '<br>';
echo 'host: ' . $a . '<br><br>';
$extnumLinks++;
}
$allnumLinks++;
}
// count results
echo "<br>";
echo "Count int: $intnumLinks <br>";
echo "Count ext: $extnumLinks <br>";
echo "Count nf: $nfnumLinks <br>";
echo "Count all: $allnumLinks <br>";
?>
Consider this post as closed. At first I wanted to delete this post but then again someone might use this code for his work.
I've managed to cut the string a sentence into a word. but the new results can be viewed in the browser when the program runs. but these results can not change the condition of the strings in the original text file. I want the contents of the original text file identical to compile the results in the browser. Well how ya how to store the results of the pieces of the word to the text file? in this case stored in notepad with a .txt extension.
To cut the text I use the following php code:
$width = strlen($openfile)/28000;
$wrapped = wordwrap($openfile, $width,'<br>');
//echo $wrapped;
$stringedit=str_replace(" ", "<br>", $openfile);
echo $stringedit;
result from browser is like this
You can use:
file_put_contents ( $fileName, $stringedit); //here filename indicates the name/path of source file.
the solution for it problem is like this ,it is 100% work:
<?php
$array_filename = glob('simpantoken/*.txt');
foreach ($array_filename as $fileteks)
{
$stringteks = file_get_contents($fileteks);
$konversi = strtolower($stringteks);
$jenistandabaca = array(',', '!', '?', '.', ':',';', '-');
$hapustandabaca = str_replace($jenistandabaca,'',$konversi);
$hapustandabaca = trim(preg_replace('/[^0-9a-z]+/i','', $konversi));
$hapustandabaca = preg_replace('/[^a-z\d]+/i', '', $konversi);
$hapustandabaca = preg_replace('/[^\w]+/','',$konversi);
$hapustandabaca = preg_replace('/\W+/','',$konversi);
$replacespasi = str_replace(" ", PHP_EOL, $konversi);
$konversistring = explode("/", $konversi);
$array = preg_split('/[\pZ\pC]+/u', $konversi);
$ubahkarakter = str_replace(" ", '<br/>', $konversi);
if(strpos($konversi,' ') > 0)
{
echo "ada spasi";
}
else
{
echo "tidak ada spasi";
}
$handle = fopen($fileteks, 'w');
fwrite($handle, $replacespasi);
fclose($handle);
}
?>
I have the following code :
function removeFilename($url)
{
$file_info = pathinfo($url);
return isset($file_info['extension'])
? str_replace($file_info['filename'] . "." . $file_info['extension'], "", $url)
: $url;
}
$url1 = "http://website.com/folder/filename.php";
$url2 = "http://website.com/folder/";
$url3 = "http://website.com/";
echo removeFilename($url1); //outputs http://website.com/folder/
echo removeFilename($url2);//outputs http://website.com/folder/
echo removeFilename($url3);//outputs http:///
Now my problem is that when there is only only a domain without folders or filenames my function removes website.com too.
My idea is there is any way on php to tell my function to do the work only after third slash or any other solutions you think useful.
UPDATED : ( working and tested )
<?php
function removeFilename($url)
{
$parse_file = parse_url($url);
$file_info = pathinfo($parse_file['path']);
return isset($file_info['extension'])
? str_replace($file_info['filename'] . "." . $file_info['extension'], "", $url)
: $url;
}
$url1 = "http://website.com/folder/filename.com";
$url2 = "http://website.org/folder/";
$url3 = "http://website.com/";
echo removeFilename($url1); echo '<br/>';
echo removeFilename($url2); echo '<br/>';
echo removeFilename($url3);
?>
Output:
http://website.com/folder/
http://website.org/folder/
http://website.com/
Sounds like you are wanting to replace a substring and not the whole thing. This function might help you:
http://php.net/manual/en/function.substr-replace.php
Since filename is at last slash you can use substr and str_replace to remove file name from path.
$PATH = "http://website.com/folder/filename.php";
$file = substr( strrchr( $PATH, "/" ), 1) ;
echo $dir = str_replace( $file, '', $PATH ) ;
OUTPUT
http://website.com/folder/
pathinfo cant recognize only domain and file name. But if without filename url is ended by slash
$a = array(
"http://website.com/folder/filename.php",
"http://website.com/folder/",
"http://website.com",
);
foreach ($a as $item) {
$item = explode('/', $item);
if (count($item) > 3)
$item[count($item)-1] ='';;
echo implode('/', $item) . "\n";
}
result
http://website.com/folder/
http://website.com/folder/
http://website.com
Close to the answer of splash58
function getPath($url) {
$item = explode('/', $url);
if (count($item) > 3) {
if (strpos($item[count($item) - 1], ".") === false) {
return $url;
}
$item[count($item)-1] ='';
return implode('/', $item);
}
return $url;
}
is there any php functions, to sanitize link+path?
i.e.
http://example.com/fold1/fold2/fold3/../../././MyFile.HTML
to
http://example.com/fold1/MyFile.HTML
so, i want remove dots,but maintain the suitable(relative) correct path.
I've found so far, is :
echo ConvertDotedPathToNormalUrl('http://example.com/directory/.././pageee.html');
code:
function ConvertDotedPathToNormalUrl($url){
$firstType = '/(.*)\/((?:(?!\.\.).)+)\/\.\.\//si';
preg_match($firstType,$url,$result);
if (!empty($result[2])){
$url = str_replace('/'.$result[2].'/..','',$url);
if ( strstr($url,'../')){$url= ConvertDotedPathToNormalUrl($url);}
}
$url = str_replace('/./','/',$url); $url = str_replace('://','|||',$url);$url = str_replace('//','/',$url);$url = str_replace('|||','://',$url);
return $url;
}
p.s. but not, it converts
You can
1) get the $path using parse_url(..).
2) get the $webroot = $_SERVER['DOCUMENT_ROOT'];
3) get the $zrealpath = realpath($webroot . $path);
<?php
define ('CRLF', "<br />\n");
$url = 'http://example.com/fold1/fold2/fold3/../../././MyFile.HTML';
$parsed = parse_url($url);
echo '---- vardump($parsed):', CRLF; // for education
zvardump($parsed);
$webroot = $_SERVER['DOCUMENT_ROOT'];
echo 'webroot = ', $webroot, CRLF;
$path = $parsed['path'];
echo 'path = ', $path, CRLF;
$zrealpath = realpath($webroot . $path);
echo 'realpath = ', $zrealpath, CRLF;
function zvardump($var1) {
ob_start();
echo "<pre style=\"margin:0;\">\n";
var_dump($var1);
echo "</pre>\n";
$zoutput = ob_get_contents();
ob_end_clean();
echo str_replace("=>\n ", " => ", $zoutput);
}
?>
i am uploading the image into the server , need to place the _ in place of gap in the image. Like if the name of image is Stack Flow.jpg, i need to send it as Stack_Flow.jpg in the directory as well in the email. HOw could be possible with the following code. i have tried but no success.. I am sending the 4 files in one form, code as ---
$filea = $_FILES['FILE1']['name'];
$fileb = $_FILES['FILE2']['name'];
$filec = $_FILES['FILE3']['name'];
$filed = $_FILES['FILE4']['name'];
$order_image_a='order_'.$orderId.'_'.$filea;
if(!empty($filea)) move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");
$order_image_b='order_'.$orderId.'_'.$fileb;
if(!empty($fileb)) move_uploaded_file($_FILES['FILE2']['tmp_name'], "../files/$order_image_b");
$order_image_c='order_'.$orderId.'_'.$filec;
if(!empty($filec)) move_uploaded_file($_FILES['FILE3']['tmp_name'], "../files/$order_image_c");
$order_image_d='order_'.$orderId.'_'.$filed;
if(!empty($filed)) move_uploaded_file($_FILES['FILE4']['tmp_name'], "../files/$order_image_d");
i am using below function, how could i apply it for all four files--
<script>
function convertSpecialChars($str) {
$str = str_replace( " ", "_", $str );
return $str;
}
</script>
here is a quick example in php:
<?php
$name = "Stack Flow.jpg";
echo preg_replace('/[\s\-]+/', '_', $name );
?>
returns Stack_Flow.jpg
http://codepad.org/MQoEZ2wv
This is not a script but PHP..
<?
function convertSpecialChars($str) {
$str = str_replace( " ", "_", $str );
return $str;
?>
//do the same for all other images..
$filea = str_replace(' ', '_', $filea;
$order_image_a='order_'.$orderId.'_'.$filea;
if(!empty($filea)) move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");
Using:
<?php
function convertSpecialChars($str) {
$str = str_replace( " ", "_", $str );
return $str;
}
?>
And then your code:
$filea = $_FILES['FILE1']['name'];
$fileb = $_FILES['FILE2']['name'];
$filec = $_FILES['FILE3']['name'];
$filed = $_FILES['FILE4']['name'];
$order_image_a='order_'.$orderId.'_'.convertSpecialChars($filea);
if(!empty($filea))
move_uploaded_file($_FILES['FILE1']['tmp_name'], "../files/$order_image_a");
$order_image_b='order_'.$orderId.'_'.convertSpecialChars($fileb);
if(!empty($fileb))
move_uploaded_file($_FILES['FILE2']['tmp_name'], "../files/$order_image_b");
$order_image_c='order_'.$orderId.'_'.convertSpecialChars($filec);
if(!empty($filec))
move_uploaded_file($_FILES['FILE3']['tmp_name'], "../files/$order_image_c");
$order_image_d='order_'.$orderId.'_'.convertSpecialChars($filed);
if(!empty($filed))
move_uploaded_file($_FILES['FILE4']['tmp_name'], "../files/$order_image_d");
Or if possible, you could do it in a loop (less duplicate code):
for ($i = 1; $i <= 4; $i++)
{
$file = $_FILES['FILE' . $i]['name'];
$order_image = 'order_' . $orderId . '_' . convertSpecialChars($file);
if(!empty($file))
move_uploaded_file($_FILES['FILE' . $i]['tmp_name'], "../files/$order_image");
}
In your code change $order_image_a='order_'.$orderId.'_'.$filea; and other similar lines to
$order_image_a='order_'.$orderId.'_'.convertSpecialChars($filea);
But will better if you will know how work your code.