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.
Related
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
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>';
Why not output the first element array?
i use next code
$product_idn='123112$2313213';
$count_products=substr_count($product_idn,'$')+1;
$idn_products=explode('$',$product_idn);
$name_products='';
$s=0;
while($s<=$count_products){
$prod=$idn_products[$s];
$res10=mysql_query("..... WHERE `product_idn`='$prod'");
$i10=mysql_fetch_assoc($res10);
$name_products.=$i10['name'].', ';
$s++;
}
echo $name_products;
//give 2313213,,
Why not output the first element array ?
What about
$product_idn='123112$2313213';
$idn_products=explode('$',$product_idn);
$name_products='';
foreach($idn_products as $val){
$res10=mysql_query("..... WHERE `product_idn`='$val'");
$i10=mysql_fetch_assoc($res10);
$name_products.=$i10['name'].', ';
}
echo $name_products;
There are a lot of unusual techniques being used in the original code. My best guess at what I'd do, without really knowing the purpose of this code is:
$product_idn = '123112$2313213';
$idn_products = explode('$', $product_idn);
$name_products = '';
foreach($product_idn as $value) {
$res10 = mysql_query("SELECT name FROM ... WHERE `product_idn`='$value'");
if ($res10) {
$i10 = mysql_fetch_assoc($res10);
$name_products .= $i10['name'].', ';
}
}
$name_products = rtrim(', ', $name_products);
echo $name_products;
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;
Trying to stop the words has been entered from repeating when the array displays its contents when echoed.
Here is the code.
echo "$array[$x] has been entered";
echo implode(", ", $array) . " has been entered";
uh, echo $array[$x];?
??
how about
echo $array[$x]." has been entered";
As far as i could get your question; try below code:
foreach($array as $key => $value)
{
// if this is the first item
if ($key === 1)
{
echo "$array[$key] has been entered";
}
else
{
echo $array[$key];
}
}
After your updated comment:
You can use something like
foreach ($array as $a) {
echo $a;
}
echo "has been entered";
Use single-quotes rather than double-quotes:
echo implode(', ', $array) . ' has been entered';