I have the following code which grabs a the categories from my wordpress blog and replaces all the line breaks with a pipe.
<?php $variable = wp_list_categories('style=none&echo=0'); ?>
<?php $variable = str_replace('<br />', ' | ', $variable); ?>
<?php echo $variable; ?>
The code works – however I need the last occurrence to be ignored.
Any way around this?
Thanks!
Another way would be to get the last pipe
|
character withstrrpos
, once found, just remove it using the string index, then use asubstr_replace
and replace it with a<br/>
again:Sample Output
Sidenote: This would be the dirty solution to it, but if you have more complex operations to be done, it might be better to just use HTML Parsers with this one,
DOMDocument
in particular in conjunction with->replaceChild
with regression.Sample Output
Try with
rtrim()
. It will remove the last|
from the string.Update
When using
str_replace
with category variable it return categories|
pipe seprated with space before category nameso you can remove last two characters one with
space
and second is|
pipe.if you use Pipe with spaces before and after the category name then use
-4
instead-2