Style array_slice output - php

I have the following PHP script that extracts the last 5 rows from file X.
<?php
$f=file("x.txt");
$last=array_slice($f, -5);
echo implode("<br>",$last);
?>
The output is the following:
John
Christmas
George
Luck
Sun
Question: How can I make the output to be clickable links? Example of output I want:
John
Christmas
George
Luck
Sun
I tried something like:
echo implode("<br> <a href='http://google.com/q?' . $last . ''>");
But it didn't work at all... any solutions? Thanks!

Instead of implode(), you can just loop thru $last then echo desired html line
foreach ($last as $value) {
echo "<a href='http://www.google.com?q=$value'>$value</a><br>";
}

Try this :-
<?php
$names = array('John','Christmas','George','Luck','Sun');
foreach($names as $name) {
echo "<br>$name";
}
?>
Happy Coding :-)

I think no need to use implode function.
I have modified your code as below. Hope this will help.
<?php
$f=file("x.txt");
$last=array_slice($f, -5);
if(!empty($last)){
foreach ($last as $value) {
?>
<a href='http://www.google.com?q=<?php echo $value;?>'><?php echo $value;?></a><br>
<?php
}
}
?>

<?php
$f = array("Volvo", "BMW", "Toyota"); //$f=file("x.txt");
$last=array_slice($f, -5);
echo implode("<br>",$last);
foreach ($last as $value) {
echo "<a href='http://www.google.com?q=$value'>$value</a> , ";
}
?>

You can use array_map and implode if you don't want to loop.
$last = array('John','Christmas','George','Luck','Sun');
function addlink($last)
{
return('' . $last .'');
}
$last = array_map("addlink",$last);
echo implode("\n", $last);
https://3v4l.org/l3nME

Related

PHP - why is generating a text area per line when attempting to echo in a text area?

Why can not I make an echo inside a textarea after doing a foreach? the echo happens yes, but everything comes in fragments, one line in each text area.
Why does not everything come out in the same text area?
$array = end($matches);
$array = array_unique($array, SORT_REGULAR);
foreach ($array as $strX) {
$strX = 'myprefix'.$strX.'<br>';
//echo $strX;
echo '<textarea>'.$strX.'</textarea>';
}
Concatenate the value and then echo into the textarea.
$value = null;
foreach ($array as $strX) {
$value .= 'myprefix'.$strX.PHP_EOL;
}
echo '<textarea>'.$value.'</textarea>';
https://3v4l.org/KZn2M
As I understood you so far. You just need to put the beginning tag of textarea before the for-loop. So your code would become:
$array = end($matches);
$array = array_unique($array, SORT_REGULAR);
echo '<textarea>';
foreach ($array as $strX) {
$strX = 'myprefix'.$strX.'<br>';
//echo $strX;
}
echo '</textarea>';
Try this, put textarea outsside loop
$array = end($matches);
$array = array_unique($array, SORT_REGULAR);
echo '<textarea>';
foreach ($array as $strX) {
$strX = 'myprefix'.$strX.'<br>';
//echo $strX;
}
echo '</textarea>';

json decode sorting from variables value

[ {"play":"hdr.1","name":"1","year":"1994","class":"act bio deo"},
{"play":"hdr.2","name":"2","year":"1972","class":"deo bio sil"},
{"play":"hdr.3","name":"3","year":"1974","class":"sil moc tel"},
{"play":"hdr.5","name":"4","year":"1994","class":"rep sim fal"},
{"play":"hdr.6","name":"5","year":"1967","class":"viz tel moc"},
{"play":"hdr.7","name":"6","year":"2003","class":"fal deo dec"},
{"play":"hdr.8","name":"7","year":"1999","class":"tel act bio"},
{"play":"hdr.9","name":"8","year":"1993","class":"mio moc viz"},
{"play":"hdr.10","name":"9","year":"1957","class":"fal dec mio"} ]
I have this json.json data and i use this php code to make from all links that i use somewhere:
<?php
$fill = file_get_contents("json.json");
$tstJson = json_decode($fill);
foreach($tstJson as $val)
echo "<a class='".$val->class."' href='?".$val->play."' >".$val->name."</a>";
?>
But how do i make this to echo only the classes that contains "act" ?
Like a sorting mode.
You can use strpos() to check if 'act' is in class before echoing:
foreach($tstJson as $val)
if (strpos($val->class, 'act') !== false)
echo "<a class='".$val->class."' href='?".$val->play."' >".$val->name."</a>";
See demo
You just need an if with a preg_match:
<?php
$fill = file_get_contents("json.json");
$tstJson = json_decode($fill);
foreach ($tstJson as $val) {
if (preg_match('/\bact\b/', $val->class)) {
echo "<a class='".$val->class."' href='?".$val->play."' >".$val->name."</a>";
}
}
?>
The \bact\b checks that the entire word is act (so, for example, fact won't match).
Output (with line breaks for readability):
<a class='act bio deo' href='?hdr.1' >1</a>
<a class='tel act bio' href='?hdr.8' >7</a>
try this
<?php
$fill = file_get_contents("json.json");
$tstJson = json_decode($fill);
foreach($tstJson as $val)
{
$act_class = explode(" ", $val->class);
if(in_array("act", $act_class))
{
echo "<a class='".$val->class."' href='?".$val->play."' >".$val->name."</a>";
}
}
?>

How to replace a string with a foreach loop

I want to replace a string with a loop in php
This is the string:-
$name=array("tom","vicky","raj");
str="{$loop_start} Good morning {$value} {$loop_end}";
i want it as:
foreach($name as $value){
echo Good morning $value;
}
Want to replace {$loop_start} with foreach($name as $value){
and {$loop_end} with }
I am trying but not getting the solution of it
Please help me if somebody know.
Thanks
PHP explode() will convert String into an array.
$name ='"tom","vicky","raj"';
$ino_array = explode(',', $name);
foreach($ino_array as $value) {
echo 'Good morning '.$value.' <br />';
};
Use the below code $name='"tom","vicky","raj"' is a string.
<?php
$str='"tom","vicky","raj"';
$str = substr($str,1);
$str = substr($str,0,-1);
$name = explode('","',$str);
foreach($name as $value){
echo "Good morning $value<br/>";
}
?>
This will might help you.

Generate a set of HTML list items from a comma separated list? PHP

I have a field in my database with the text value:
"these, are, some, keywords" (minus the inverted commas)
Now, I wonder if I can generate an unordered list from this so ultimately my HTML reads:
<ul>
<li>these</li>
<li>are</li>
<li>some</li>
<li>keywords</li>
</ul>
Is this possible with PHP and if so is anyone able to help me out with this?
Many thanks for any pointers.
You can accomplish this with something like the following:
<?php
$yourList = "these, are, some, keywords";
$words = explode(',', $yourList);
if(!empty($words)){
echo '<ul>';
foreach($words as $word){
echo '<li>'.htmlspecialchars($word).'</li>';
}
echo '</ul>';
}
?>
As mentioned by elcodedocle, you may want to use str_getcsv() instead of explode if more appropriate.
Have a look at str_getcsv() and explode()
Example:
<?php
$mystring = "these, are,some , keywords";
$myvalues = str_getcsv($mystring);
$myoutput = "<ul>";
foreach ($myvalues as $value){
$myoutput .= "<li>".trim($value)."</li>\n";
}
$myoutput .= "</ul>";
echo $myoutput;
?>
You need to explode you string for ', '
print <ul>
for each element in the array you received you print '<li>' . $value . '</li>'
print </ul>
You can try:
$arr = explode(",","these, are, some, keywords");
$res = "<ul>";
foreach ($arr as $val){
$res .= "<li>" . $val . "</li>";
}
$res .= "</ul>";
echo $res;

How do I include a range of lines of a file in php?

I am aware that this:
<?php
$file2 = file('website/files/myfolder/file.html');
echo $file2[8];
echo $file2[9];
echo $file2[10];
echo $file2[11];
?>
would give me the contents on lines 8,9,10,11 in the file.html file, however I would like to do this for about 100 lines ranging from lines 23 to 116. How would I accomplish this without using the echo $file2[NUMBER]; one hundrend times?
Another way to explain what I am trying to do would be possibly if I could do this in php:
echo $file2[23-116];
obviously this will not work but that is the concept.
Thanks for your help!
Use a loop like so
$data = file('website/files/myfolder/file.html');
for ($i = 23; $i <= 116; $i++) {
echo $data[$i];
}
http://php.net/manual/en/control-structures.for.php
Or you could splice the data:
$data = file('website/files/myfolder/file.html');
echo implode(PHP_EOL, array_splice($data, 23, 116 - 23));
http://php.net/manual/en/function.array-splice.php
http://php.net/manual/en/function.implode.php
How about something like this?
$file = file('website/files/myfolder/file.html');
foreach( range( 23, 116) as $i) {
echo $file[$i];
}

Categories