How to set a variable separator

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to set a variable separator



I have a variable $cameramodel which contains term meta of a wordpress taxonomy:


$cameramodel


<?php
function cameramodel() {
$terms = get_the_terms($post->ID, 'camera');
$result = "";
foreach ($terms as $term) {
$term_id = $term->term_id;
$result .= get_term_meta( $term_id, 'model', true );
}
return $result;
}

$cameramodel = cameramodel(); ?>



On the front end I would echo $cameramodel


$cameramodel


<?php echo $cameramodel; ?>



There are instances where $cameramodel contains more than one value, but when I echo $cameramodel, They all appear on one line with no spaces. My question is, how do I make a separator between each value? I want each value to be on it's own line, and I would like to be able to separate them with the word "and".


$cameramodel


$cameramodel



For example, if the variable contains "one two three" it currently prints "onetwothree", but what I want is:


one and
two and
three



Hope I am being clear.



Thanks!




3 Answers
3



If the values are stored one on each line, you could just use trim(), which removes whitespace characters (including new lines) from before and after the strings:


trim()


$result .= ' ' . trim(get_term_meta( $term_id, 'model', true ));



Change the ' ' to use the separator of your choice. Note that this will also add this to the beginning of the string. If this is a problem, you could then ltrim() the result when you return it.


' '


ltrim()


return





To be honest, I am not sure how it is stored, but when I echo it, it is all on one line without spaces.
– A Merdaman
8 mins ago



Assuming you are printing to an HTML document, you could try this:


$cameramodel = "one two three";
echo preg_replace('/s+/', ' and<br>', $cameramodel);



Output:


one and
two and
three



If it's not HTML, just replace <br> with n (or rn if required).


<br>


n


rn





Thanks, but currently it's printing without spaces. would your code still work?
– A Merdaman
6 mins ago



If you are willing to edit the function slightly, I believe you will benefit from using an array and then joining it together into a string with the delimiter of your choice passed as an argument:


<?php
function cameramodel($delimiter) {
# Get the terms.
$terms = get_the_terms($post->ID, 'camera');

# Create an array to store the results.
$result = ;

# Iterate over every term.
foreach ($terms as $term) {
# Cache the term's id.
$term_id = $term->term_id;

# Insert the term meta into the result array.
$result = get_term_meta($term_id, 'model', true);
}

# Return the elements of the array glued together as a string.
return implode($delimiter, $result);
}

# Call the function using a demiliter of your choice.
$cameramodel = cameramodel("and<br>");
?>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Yyru5kk6,Kl bRmm,gAN2,ab5CxtDOyvU8nK,6dotFep,wN2EApoGN9lT,4kWBz8Pa,HrpbhEEgzghKmeivtKc,4rLQ,LI
CZNLgn7VEJ,zYc6tbL98 rH,B4MDk5jeHy5UmGNbEiPyhUoKqqzjD1bpQpAZWnA,8lX

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham