PHP variable as hyperlink - php

I'm trying to set the php variables linked to one of my page in the folder, index.html. However, I am getting an error of Name&lang=en"> Any help?
if (isset($result_array)) {
foreach ($result_array as $result) {
$name = preg_replace("/" . $search . "/i", "<b class='high'>" . $search . "</b>", $result['Name']);
$url = '<a href="index.html">' . urlencode($result['Name']) . '&lang=en';
$out = str_replace('nameS', $name, $html);
$out = str_replace('urlS', $url, $out);
$_SESSION[$search] = $result['Name'];
echo($out);
}
}

Related

Adding functions into an Array in php

As it stands I am having real trouble figuring out just how to get the functions delete, move/copy, rename into an array via click of an image or text, and then making the function correspond to the correct row in the table and the correct file on the row in the array.
This is a very hard question to word to be honest but the array currently populates a table with files in a folder, file name, size and date modified, I'm trying to add in small images on each row for delete file, rename file ect. so that these images are linked with functions so when pressed it will delete the corresponding file or rename it if that makes sense. anyway the array code is below, and I understand if its difficult to answer just figured I would ask.
Also $cellOptions is the cell im trying to populate it currently just gives me back the logged in user
http://pastebin.com/dkeUAk50
function listFiles($dir)
{
$output = ''; $outRows = ''; $files = array();
if (is_dir($dir)) {
if ($dirHandle = opendir($dir)) {
$files = array_diff(scandir($dir), array('.', '..', '.htaccess'));
$totalSize = (int) 0;
foreach($files as $file) {
$fileTime = #date("d-M-Y", filectime($dir . '/' . $file)) . ' ' . #date("h:i", filemtime($dir . '/' . $file));
$totalSize += filesize($dir . '/' . $file);
$fileSize = #byte_convert(filesize($dir . '/' . $file));
$cellLink = '<td class="list_files_table_file_link">' . $file . '</td>';
$cellTime = '<td>' . $fileTime . '</td>';
$cellOptions = '<td>'. $_SESSION['Username'] .'<td>';
$cellSize = '<td>' . $fileSize . '</td>';
$outRows .= '<tr>' . "\n " . $cellLink . "\n " . $cellTime . "\n " . $cellSize . "\n" . $cellOptions . '</tr>' . "\n";
}
closedir($dirHandle);
}
}
$output = '<table class="list_files_table" width="100%" align="center" cellpadding="3" cellspacing="1" border="0">' . "\n";
$output .= '<thead><tr><td><b>Name</b></td><td><b>Date Modified</b></td><td><b>Size</b></td></tr></thead>' . "\n";
$output .= '<tfoot><tr><td colspan="2">' . count($files) . ' files.</td><td>' . #byte_convert($totalSize) . '</td></tr></tfoot>' . "\n";
$output .= '<tbody>' . "\n";
$output .= $outRows;
$output .= '</body>' . "\n";
$output .= '</table>';
return $output;
}
function byte_convert($bytes)
{
$symbol = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$exp = (int) 0;
$converted_value = (int) 0;
if ($bytes > 0) {
$exp = floor(log($bytes)/log(1024));
$converted_value = ($bytes/pow(1024,floor($exp)));
}
return sprintf('%.2f ' . $symbol[$exp], $converted_value);
}
session_start();
echo listFiles($_SESSION['UserFolder']);
Add an element that contains the following in your loop:
A form that submits to the same page with a method of POST
An hidden input element that contains the filename
A submit button to submit the form.
This is what it would look like:
$cellSize = '<td>' . $fileSize . '</td>';
$deleteCell = '<td><form action="/" method="POST"><input type="hidden" value="'.$file.'" ame="fileToDelete"/><input type="submit" value="Delete" name="deleteButton"/></form></td>';
Create a function to delete:
function deleteFile($dir, $fileToDelete){
if (is_dir($dir)) {
if ($dirHandle = opendir($dir)) {
$files = array_diff(scandir($dir), array('.', '..', '.htaccess'));
if($files){
foreach($files as $file){
if($file === $fileToDelete) {
unlink($fileToDelete);
$output = 'Successfully deleted file: '.$fileToDelete;
}
}
}
}
}
return $output;
}
Check if a form was submitted, and if so, delete the file in question:
if(isset($_POST)){
echo deleteFile($_SESSION['UserFolder'], $_POST['fileToDelete']);
}

PHP Function running locally but not within a loop

I have this function and statements running fine in my page:
$name = "JÜRGEN RÜDIGER HERMANN";
function nameReplace($name)
{
$name = str_replace("Ñ", "N", $name);
$name = str_replace("Ü", "U", $name);
$name = str_replace("Ç", "C", $name);
$name = str_replace("Á", "A", $name);
$name = str_replace("É", "E", $name);
return $name;
}
$name = nameReplace($name);
echo "This is the name!" . $name . "<br>";
$orename = "ÑÜÇÁÉ";
$orename = nameReplace($orename);
echo "Forename test: " . $orename . "<br>";
$astname = "ÁÉÑÜÇ";
$astname = nameReplace($astname);
echo "Lastname test: " . $astname . "<br>";
Everything about works fine, the characters are changed to acceptable characters for upload into a progress DB. But later in the page I'm looping through data and taking strings from a parsed HTML file:
$familyname = nameReplace($familyname);
The above familyname nameReplace() fails. If I echo out just family name it echoes out the string. If I echo out nameReplace($familyname) it returns blank or false.
Any ideas? Do functions have a specific scope?
Thanks,
Stephen.
Below is the code for the loop as requested:
for($i=1;$i<41;$i++)
{
$textfile = 'interpol_create' . $i. '_' . $date . '.txt';
$myfile = fopen($textfile, "r") or die("Unable to open file!");
if($myfile)
{
#read through the file line by line
while (($buffer = fgets($myfile, 4096)) !== false)
{
$linenumber++;
if( (strstr($buffer, '<span')) && (strstr($buffer, 'titre')))
{
$buffer = str_replace('<br />', '|', $buffer);
#echo $buffer . '<br>';
$result = explode('|', $buffer);
#family name variable
$familyname = $result[0];
$familyname = str_replace('<span class="titre">', '', $familyname);
$familyname = trim($familyname);
$familyname = nameReplace($familyname);
#first name variable
$forename = $result[1];
$forename = nameReplace($forename);
if(strstr($forename, "RGEN"))
{
echo "Forename is ".nameReplace((string)$forename)."<br>";
$newVar = "JÜRGEN RÜDIGER";
$newVar = nameReplace($newVar) . "<br>";
echo "New variable = " . $newVar . "<br>";
}
$forename = str_replace("</span>", "", $forename);
$forename = trim($forename);
#echo ($namecounter + 1) . ' | ';
#echo 'Family name : ' . $familyname . ' | Forename is : ' . $forename . '<br>';
$update_full_file .= $forename . " " . $familyname;
$firstnames[$namecounter] = $forename;
$lastnames[$namecounter] = $familyname;
}#end of if strstr $buffer
if( (strstr($buffer, 'Age today :')) && (strstr($buffer, '<br')))
{
$buffer = str_replace('<br />', '', $buffer);
$buffer = str_replace('Age today :', '', $buffer);
$agetoday = $buffer;
$agetoday = str_replace("<br/>", "", $agetoday);
$agetoday = trim($agetoday);
#echo 'Age Today : ' . $agetoday . '<br>';
$ages[$namecounter] = $agetoday;
$update_full_file .= ",," . $agetoday;
$nextlinenumber = $linenumber + 2;
$nationalitylinenumbers[$namecounter] = $nextlinenumber;
#echo $nationcounter . " " . $nextlinenumber . '<br>';
}
if( (strstr($buffer, 'Nationality :')) )
{
#$buffer = str_replace('<br />', '', $buffer);
#$buffer = str_replace('Age today :', '', $buffer);
#echo 'Nationality : ' . $buffer . '<br>';
#echo 'Line number : ' . $linenumber . '<br>';
}
if( in_array($linenumber, $nationalitylinenumbers) )
{
#echo $nationcounter . $buffer . '<br>';
#$nationcounter++;
}
###does the current linenumber appear in the array $nationalitylinenumbers?###
if( in_array($linenumber, $nationalitylinenumbers) )
{
$nation = $buffer;
$nation = str_replace("<br/>", "", $nation);
$nation = str_replace("</div>", "", $nation);
$nation = str_replace(" ", "", $nation);
#$nation = str_replace(",", "", $nation);
$nation = str_replace('<divclass="sep">', 'No Country', $nation);
$nation = trim($nation);
if(strstr($nation, ','))
{
#echo 'Create a duplicate entry';
$nations = explode(',', $nation);
$nation = $nations[0];
$nation = trim($nation);
$duplicateNation = $nations[1];
$duplicates[$duplicate_counter] .= $forename . " " .$familyname . ",," . $agetoday . ",,,,,". trim($duplicateNation) .",,,,,\n";
$duplicate_counter++;
}
#$nation = countryMappings($nation);
###check to see if entry has 2 countries###
#echo $nationcounter . $nation . '<br>';
$nationcounter++;
$update_full_file .= ",,,,," .$nation. ",,,,,\n";
$nationalitys[$namecounter] = $nation;
#echo $buffer.'<br>';
$namecounter++;
}
}#end of while buffer = fgets
}#end of if myfile
}
I managed to fix this!
The issue was I was translating the wrong data. I parsed in the 'Ü' character and was using:
$name = str_replace("Ü", "U", $name);
I exported the data to a text file and found that the character is actually exporting as Ãœ instead of Ü. I amended my function to be as below and it now works:
$name = str_replace("Ü", "U", $name);
Thanks again to all who responded, all the tips were very helpful.

var_export prettifier / visualizer

I'm using var_export to dump output to logs when errors occur. However since the result is in pure text, I don't get a chance to push it through some sort of library like krumo so I can interactively explores the output.
What methods do people have to deal with making var_export text more readable?
Here is my function, it works well for multidimensional arrays:
function VE($varname, $varval, $short_syntax=true, $tag = ' ', $comma='', $end_line="\r\n") {
$res = '';
if($short_syntax){
$begin_array = '[';
$end_array = ']';
} else {
$begin_array = 'array(';
$end_array = ')';
}
$arr = explode('/',$varname);
$dim =count($arr)-1;
$lastKey = end($arr);
if (! is_array($varval)){
if( is_string($varval)) $varval = "'$varval'";
$res .= str_repeat($tag,$dim) . $lastKey . ' => ' . $varval . $comma . $end_line;
}else{
$res .= str_repeat($tag,$dim) . $lastKey . ' => ' . $begin_array . $end_line;
$count_varval = 0;
$dim_varval = count($varval);
foreach ($varval as $key => $val){
$count_varval++;
if($count_varval<$dim_varval) $commma=','; else $commma='';
if( is_string($key)) $key = "'$key'";
$res .= VE ($varname . "/" . $key , $val, $short_syntax, $tag, $commma);
}
$res .= str_repeat($tag,$dim) . $end_array . $comma . $end_line;
}
return $res;
}
$bigarray = array(); // your array
$bb = VE ('$bigarray', $bigarray);
echo "<pre>$bb</pre>";
I hope it helps ;)

How to iterate this in a foreach construct

This one only covers the first record in the array -- $form[items][0][description]. How could I iterate this to be able to echo succeeding ones i.e
$form[items][1][description];
$form[items][2][description];
$form[items][3][description];
and so on and so forth?
$array = $form[items][0][description];
function get_line($array, $line) {
preg_match('/' . preg_quote($line) . ': ([^\n]+)/', $array['#value'], $match);
return $match[1];
}
$anchortext = get_line($array, 'Anchor Text');
$url = get_line($array, 'URL');
echo '' . $anchortext . '';
?>
This should do the trick
foreach ($form['items'] as $item) {
echo $item['description'] . "<br>";
}
I could help you more if I saw the body of your get_line function, but here's the gist of it
foreach ($form['items'] as $item) {
$anchor_text = get_line($item['description'], 'Anchor Text');
$url = get_line($item['description'], 'URL');
echo "{$anchor_text}";
}
You can use a for loop to iterate over this array.
for($i=0; $i< count($form['items']); $i++)
{
$anchortext = get_line($form['items'][$i]['description'], 'Anchor Text');
$url = get_line($form['items'][$i]['description'], 'URL');
echo '' . $anchortext . '';
}

Strange PHP issue

My function looks like that
protected function make_js_link($list, $folder, $parentdir = "js") {
$links = array();
$list = explode(',', $list);
foreach ($list as $name) {
$dir = $parentdir . "/";
if (is_string($folder))
echo $folder . "/";
$links[] = '<script src="' . $dir . trim($name) . '.js"></script>' . "\n";
}
echo implode(" ", $links);
}
So when js file located in $parentdir I'm calling like that
$this->make_js_link('ckeditor', 0, 'incl/editor');
If file located in parentdir/another_dir, then calling like that
$this->make_js_link('jquery', 'adapters', 'incl/editor');
The problem is, PHP escapes this part in both cases: even if I have folder variable with exact string value:
if (is_string($folder))
echo $folder . "/";
Where I did wrong?
You did echo instead of
$dir = $parentdir . "/";
if (is_string($folder))
$dir.= $folder . "/";

Categories