Search code examples
phpwordpressthemessubstrstrpos

How Can I Get The First Value Before Comma Separated List?


Though this question seems similar to ones already asked, but this is a different case.

I have a php hook <?php echo zublum_display_breadLocation($pid);?> in my wordpress singular.php that displays 3 different locations, like country, state, city

I also have another code I copied online which outputs the first value before comma (,)

<?php 
$var = "1,23,45,123,145,200"; 
echo $first_word = substr($var, 0, strpos($var, ','));
?>

The problem is when I replace the php hook in my theme with the code I got online, it does not output anything.

This is what I did

<?php 
$var = "<?php echo zublum_display_breadLocation($pid);?>"; 
echo $first_word = substr($var, o, strpos($var, ','));
?>

Please help, it will be much appreciated.


Solution

  • An explode would seem the simplest way

    echo explode(',', $var)[0];