How to split a string every certain character? - php

Not a php guru here.
I have a string:
works/but/needs/splitting
I'd need the output in a ul list
<ul>
<li>works</ul>
<li>but</li>
<li>needs</li>
<li>splitting</li>
<ul>
I have been looking into explode("/", $text); and tried
$originalstring = "works/but/needs/splitting";
$delimiter = "/";
if(strpos($originalstring,$delimiter) > 0){
But I just don't know much of php and I can't work it out

Sorry I might not understand your questions, But split by / is this
$originalstring = "works/but/needs/splitting";
$pieces = explode("/", $originalstring);
echo '<ul>';
foreach ($pieces as $pi){
echo '<li>'.$pi.'</li>';
}
echo '</ul>';

$originalstring = "works/but/needs/splitting";
$e=explode('/',$originalstring); //creates the array ($e) of each element
echo '<ul>';
foreach ($e as $each){ //loop the array ($e)
echo '<li>'.$each.'</li>';
}
echo '</ul>';

Related

multiple characters replacements in string php

Let's say that I have the following string:
$string = 'xxyyzz';
And then I have a substitution array like this:
$subs = ['xy'];
Meaning that every x should be replaced by y in my string and every y should be replaced by x. Let's say that my substitution array can only contain pairs of characters to be replaced in my $string.
How would I go about doing this?
I tried using str_replace the following way but that doesn't work:
foreach ($subs as $sub) {
$sub_arr = str_split($sub);
$reversed_sub_arr = array_reverse($sub_arr);
$output = str_replace($sub_arr, $reversed_sub_arr, str_split($string));
}
$output = implode('', $output);
But the output gives me xxxxzz
The output should be yyxxzz
Thanks for any help
This working for your case
$string = 'xxyyzz';
$subs = ['xy'];
foreach ($subs as $sub) {
$sub_arr = str_split($sub);
$output = strtr($string, array($sub_arr[0]=>$sub_arr[1], $sub_arr[1]=>$sub_arr[0]));
}
echo $output; //yyxxzz
Extending #Orgil answer if two items in $subs array like $subs = ['xy', 'dz']
$string = $output = 'xxyyzz';
$subs = ['xy', 'dz'];
foreach ($subs as $sub) {
$sub_arr = str_split($sub);
$output = strtr($output, array($sub_arr[0]=>$sub_arr[1], $sub_arr[1]=>$sub_arr[0]));
}
echo $output;
Demo

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>';

Explode to array and print each element as list item

I have a set of numbers in a table field in database, the numbers are separated by comma ','.
I am trying to do the following:
Step 1. : SELECT set of numbers from database and explode it to array :
$array = explode(',', $set_of_numbers);
Step 2. : Print each element of the array as list item by using foreach loop :
foreach ($array as $list_item => $set_of_numbers){
echo "<li>";
print_r(array_list_items($set_of_numbers));
echo "</li>";}
Please anybody tell me what is wrong. Thank you.
$numbers = '1,2,3';
$array = explode(',', $numbers);
foreach ($array as $item) {
echo "<li>$item</li>";
}
Assuming your original $set_of_numbers is simply a CSV string, something like 1,2,3,4,..., then your foreach is "mostly" ok. But your variable naming is quite bonkers, and your print-r() call uncesary:
$array = explode(',', $set_of_numbers);
foreach($array as $key => $value) {
echo "<li>$key: $value</li>";
}
Assuming that 1,2,3,4... string, you'd get
<li>0: 1</li>
<li>1: 2</li>
<li>2: 3</li>
etc...
$numbers = "1,2,3";
$array = explode(",", $numbers);
/* count length of array */
$arrlength = count($array);
/* using for while */
$x = 0;
while ($x < $arrlength) {
echo "<li>$array[$x]</li>" . PHP_EOL;
$x++;
}
echo PHP_EOL;
/* using for classic */
for ($x = 0; $x < $arrlength; $x++) {
echo "<li>$array[$x]</li>" . PHP_EOL;
}
echo PHP_EOL;
/* using for each assoc */
foreach ($array as $value) {
echo "<li>$value</li>" . PHP_EOL;
}
echo PHP_EOL;
/* using for each assoc key */
foreach ($array as $key => $value) {
echo "<li>$key => $value</li>" . PHP_EOL;
}
body, html, iframe {
width: 100% ;
height: 100% ;
overflow: hidden ;
}
<iframe src="https://ideone.com/ZqT4Yi" ></iframe>
You actually don't need to explode on the commas. You can just replace each comma with an ending tag followed by an opening tag and then wrap your whole string in an opening and closing tag.
$set_of_numbers = '1,2,3';
echo '<li>' . str_replace(',', '</li><li>', $set_of_numbers) . '</li>';
// outputs: <li>1</li><li>2</li><li>3</li>
No looping is necessary.
Here is answer for your question to get ride of your problem
$Num = '1,2,3,4,5,';
$Array = explode(',',$Num);
foreach ($Array as $Items)
{
echo "<li>&Items</li>"; // This line put put put in the list.
}
This can easily be achieved by the following code snippet:
<?php
$my_numbers = '1,12,3.2,853.3,4545,221';
echo '<ul>';
foreach(explode(',', $my_numbers) AS $my_number){
echo '<li>'.$my_number.'</li>';
}
echo '</ul>';
The above code will output the following HTML:
<ul><li>1</li><li>12</li><li>3.2</li><li>853.3</li><li>4545</li><li>221</li></ul>
Credits: http://dwellupper.io/post/49/understanding-php-explode-function-with-examples

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;

PHP - Split an array into chunks.

I have a variable on my page called, $recipe['ingredients'];
inside the var you have as follows,
100ml milk, 350ml double cream, 150ml water
and so on. Now I'm trying to split it up so it looks as follows
<ul>
<li>100ml milk</li>
<li>350ml double cream</li>
<li>150ml water</li>
</ul>
So far I have the following code,
$ingredientsParts = explode(',', $row_rs_recipes['ingredients']);
$ingredients = array($ingredientsParts);
while (! $ingredients) {
echo" <li>$ingredients</li>";
}
But for some reason it doesn't work and I do not have the experience with explode to fix it.
$ingredientsParts = explode(',', $row_rs_recipes['ingredients']);
$li = '<ul>';
foreach($ingredientsParts as $key=>$value){
$li.=" <li>$value</li>";
}
$li.= '</ul>';
echo $li;
this should be enough:
$ingredientsParts = explode(', ', $row_rs_recipes['ingredients']);
foreach ($ingredientsParts as $ingredient)
{
echo "<li>$ingredient</li>";
}
or you can explode it by ',' and use echo '<li>' . trim($ingredient) . '</li>'; to remove whitespace from beginning/end of that string
When you explode() a string it is automatically converted into an array. You do not need to convert it to an array type as you did on the second line.
You want to use a foreach() loop to iterate through an array, not a while loop.
$ingredientsAry = explode(',', $row_rs_recipes['ingredients']);
foreach($ingredientsAry as $ingredient){
echo "<li>$ingredient</li>";
}
In fact you can just do a foreach() loop on the explode() value
foreach(explode(',', $row_rs_recipes['ingredients']) as $ingredient){
echo "<li>$ingredient</li>";
}
The explode method already return an array, so you don't have to transform your variable $ingredientsParts into an array.
Just do:
$ingredientsParts = explode(', ', $row_rs_recipes['ingredients']);
foreach ($ingredientsParts as $ingredient)
{
echo "<li>$ingredient</li>";
}
if (!empty($recipe['ingredients'])) {
echo '<ul><li>' . implode('</li><li>', explode(', ', $row_rs_recipes['ingredients'])) . '</li></ul>';
}
You can do this:
$ingredients = explode(',', $row_rs_recipes['ingredients']);
$list = '<ul>';
foreach ($ingredients as $ingredient)
{
$list .= '<li>' . $ingredient . '</li>';
}
$list .= '</ul>';
echo $list;

Categories