Output text string in array - php

How do I print the following string in an array in PHP, only selecting the texts in lowercase:
{"fieldValue":[{"portfoliocategory":"Printing","portfoliocategoryid":"printing"},{"portfoliocategory":"Digitization","portfoliocategoryid":"digitization"},{"portfoliocategory":"Android App","portfoliocategoryid":"androidapp"},{"portfoliocategory":"Photography","portfoliocategoryid":"photography"},{"portfoliocategory":"Artwork","portfoliocategoryid":"artwork"}],"fieldSettings":{"autoincrement":1}}
Output should be:
<ul>
<li>printing</li>
<li>digitization</li>
<li>androidapp</li>
<li>photography</li>
<li>artwork</li>
</ul>
Thanks.

As I understand your question.
$input = '{"fieldValue":[{"portfoliocategory":"Printing","portfoliocategoryid":"printing"},{"portfoliocategory":"Digitization","portfoliocategoryid":"digitization"},{"portfoliocategory":"Android App","portfoliocategoryid":"androidapp"},{"portfoliocategory":"Photography","portfoliocategoryid":"photography"},{"portfoliocategory":"Artwork","portfoliocategoryid":"artwork"}],"fieldSettings":{"autoincrement":1}}';
$output = '<ul>'; // Store the output
$values = json_decode($input); // This will convert to an object
foreach ($values->fieldValue as $val) {
$output .= '<li>'.$val->portfoliocategoryid.'</li>';
}
$output .= '</ul>';
$output will give you the expected results.

Related

how to separate the second comma in a php variable from mysql in html

Sorry if my question is being duplicate because I've tried to find similar questionI've got a data column in MySQL which look something like this :
https://i.stack.imgur.com/3VRI9.png
I've created a form which display the address in html using php.
$pdf_content .= '<div style="padding-left:5%">';
$pdf_content .= $company_name.'<br>';
$pdf_content .= '<div id="errorMessage">'.$address.'<br></div>';
//$pdf_content .= 'Add2,<br>';
//$pdf_content .= 'Add3,<br>';
$pdf_content .= $postcode.'<br>';
$pdf_content .= $state.'<br>';
$pdf_content .= $country.'<br>';
$pdf_content .= $mobile_phone.'<br>';
$pdf_content .= '<b>RE: Quotation for 3<sup>rd</sup> party claim vehicle </b>';
$pdf_content .= '</div><br>';
What I wanted to do now is the address which look something like No.43,Jalan Bandar Bahagia,Taman Pinji Mewah 1 , I wanted to separate the second comma which will look something like this
https://i.stack.imgur.com/0OlG3.png
I've tried but still not working
$pdf_content .= $address.'<br>';
$myList_explode = explode(",",$address);
for($i=0;$i<sizeof($myList_explode);$i++){
echo $myList_explode[$i];
if(($i+1)%2==0){
echo "</br>";
}else{
echo ",";
}
}
Is possible to do it in php while in MySQL it won't be separated?Thanks in advance.
Consume the leading substring before the second occurring comma, then forget it with \K. Then match the second occurring comma and replace it.
Regex prevents having to write a multi-line solution with a loop.
Code: (Demo)
echo preg_replace('~^[^,]*,[^,]*\K,~', '<br>', $address);
Output:
No.43,Jalan Bandar Bahagia<br>Taman Pinji Mewah 1
My pattern bears some similarity to the pattern in this preg_match() call: https://stackoverflow.com/a/65355441/2943403
You can try like this...:
$string = 'No.43,Jalan Bandar Bahagia,Taman Pinji Mewah 1';
echo formatString($string);
// No.43,Jalan Bandar Bahagia
// Taman Pinji Mewah 1
function formatString($inputString) {
$stringToArray = explode(",",$inputString);
$finalString = "";
$count = count($stringToArray) - 1;
foreach($stringToArray as $k => $arr) {
$addon = ",";
if($k == 1) {
$addon = "<br>";
}
if($k == $count) {
$addon = "";
}
$finalString .= $arr.$addon;
}
return $finalString;
}
Basically you create an array from string with explode, loop over it and create a string out of the elements. And since you want to add after second comma, function adds it if $key == 1 (second key).

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

Get certain strings only (PHP)

Text:
TestString
HT-Child1 CLASS-Class1
AnotherString
HT-Child2 CLASS-Class2
HT-Child3 CLASS-Class3
HT-Child4 CLASS-Class4
CLASSOFWEEK-Class
What I have so far: (Inside $display->getExtraHTML() is the text). Could someone guide me towards what I need to do to adapt my code to get the results I want.
<?php
$additionalHTML = explode("\n", $display->getExtraHTML());
$html = "";
$html .= "<ul>";
foreach($additionalHTML as $key => $item){
$html .= "<li>$item</li>";
}
$html .= "</ul>";
echo $html;
?>
I know I can use something like this to get string between, but how do I use it to get all the values i need?
$string = strstr($display->getExtraHTML(), "HT-"); // gets all text from HT
$string = strstr($string, "CLASS-", true); // gets all text before CLASS
Can I use both explode and strstr to get to where I want?
Expect HTML markup:
Expected Result: (Get the values from HT- and CLASS-)
<ul>
<li>Child1 Class1</li>
<li>Child2 Class2</li>
<li>Child3 Class3</li>
<li>Child4 Class4</li>
</ul>
Complete solution with preg_match_all function:
$txt = '
TestString
HT-Child1 CLASS-Class1
AnotherString
HT-Child2 CLASS-Class2
HT-Child3 CLASS-Class3
HT-Child4 CLASS-Class4
CLASSOFWEEK-Class';
preg_match_all('/^HT-(\S+)\s+CLASS-(\S+)/m', $txt, $m);
$html = "<ul>";
if (isset($m[1]) && isset($m[2])){
foreach(array_map(null, $m[1], $m[2]) as $pair){
$html .= "<li>". implode(' ', $pair) ."</li>";
}
}
$html .= "</ul>";
echo $html;
The output (push Run code snippet):
<ul><li>Child1 Class1</li><li>Child2 Class2</li><li>Child3 Class3</li><li>Child4 Class4</li></ul>
Here's the solution
<?php
$str = "TestString
HT-Child1 CLASS-Class1
AnotherString
HT-Child2 CLASS-Class2
HT-Child3 CLASS-Class3
HT-Child4 CLASS-Class4
CLASSOFWEEK-Class";
$additionalHTML = explode("\n", $str);
$html = "";
$html .= "<ul>";
foreach($additionalHTML as $key => $item){
if(substr($item,0,3) == "HT-") {
$i = explode(" ",$item);
$a = substr($i[0],3);
$b = substr($i[1],6);
$html .= "<li>$a"." ". "$b</li>";
}
}
$html .= "</ul>";
echo $html;
Result
Child1 Class1
Child2 Class2
Child3 Class3
Child4 Class4
You may use regex, to find the matching lines and extract required data:
if(preg_match("/HT-([A-Za-z0-9]+) CLASS-([A-Za-z0-9]+)/", $item, $output))
{
$html .= "<li>".implode(" ",array_slice($output,1))."</li>";
}

How to add values to string in foreach?

I have an array with lots of information. I want to extract some of the information:
*to a string
*To a new array
I tried doing this for a string:
$output;
foreach ($response->data as $post){
$output = $output . $post->link;
}
echo $output;
but the $output inside the foreach is undefined.
Is this a good solution? In that case how do I declare a variable with the right scope?
Your error appears because you're not initialising the $output variable at the beginning.
Try $output = ""; instead.
But on the other hand, a more elegant solution would be to use PHP's built-in implode() method.
Manual:
http://php.net/manual/en/function.implode.php
You could do something like this:
$output = implode(" ", $response->data);
echo $output;
UPDATE
If you want to 'implode' associative arrays, linepogl's answer to this question provides a nice example: Imploding an associative array in PHP
UPDATE 2
Here's an updated code that actually deals with the ->link part of your question:
$response_array = array();
foreach ($response->data as $post) {
$response_array[] = $post->link;
}
$output = implode(" ", $response_array);
echo $output;
You must define the variable before using it in foreach. Like:
$output="";
instead of
$output;
try this code
$output = '';
foreach ($response->data as $post){
$output = $output . $post->link;
}
echo $output;
.
Try
$output = '';
foreach ($response->data as $post){
$output .= $post->link;
}
echo $output;
Try like
$output = '';
foreach ($response->data as $post){
$output .= $post->link;
}
echo $output;
First you need to initialize the $output value as null thenonly in foreach loop concat the result to the $output.
$output = '';
foreach ($response->data as $post){
$output .= $post->link;
}
echo $output;
or you can use join
$output = join('',$response->data);
$output = array();
foreach ($response->data as $post){
$output[] = $post->link;
}
foreach( $output as $key => $value ){
echo $value;
}
To your code you might do:
$output = NULL;
foreach ($response->data as $post){
$output .= $post->link;
}
echo $output;

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;

Categories