Welcome to WebmasterWorld Guest from 50.16.120.143

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

str replace remove last separating character

     
4:49 pm on Apr 21, 2013 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:July 4, 2007
posts: 193
votes: 0


Okay, bear with as I'm not very good at php! I have this in a Wordpress template which is basically getting the categories that are attached to a post and displaying them.

<?php
$categories = wp_get_post_categories( $post->ID, array('fields' => 'ids'));
if($categories) {
$cat_ids = implode(',' , $categories);
$cats = wp_list_categories('title_li=Listed in:&style=none&echo=0&include='.$cat_ids);
echo str_replace('<br />', ' > ', $cats);
}
?>

This displays the following:

Food > Fruit > Apple >

But I need to remove the last separating character. In this case the ">" so it reads

Food > Fruit > Apple

I've seen a lot of different things such as rtrim etc but I just don't know how to get them to work with the current code I have.

Any help would be really appreciated.
5:26 pm on Apr 21, 2013 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4845
votes: 4


Using rtrim you could do

$categories = rtrim($categories,"> ");

[php.net...]

The function's 2nd argument will take any character(s) and trim them off.
5:52 pm on Apr 21, 2013 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:July 4, 2007
posts: 193
votes: 0


Thanks for this. Where does this for info the code above?
5:57 pm on Apr 21, 2013 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4845
votes: 4


replace
echo str_replace('<br />', ' > ', $cats);
with
echo rtrim(str_replace('<br />', ' > ', $cats),"> ");

The issue could be sorted a few lines higher up in your code, to avoid the "empty" array value that's causing the extra separator... but changing the above line should do the job all the same.
6:37 pm on Apr 21, 2013 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:July 4, 2007
posts: 193
votes: 0


Thanks again for your help with this. I tried that code but unfortunately the last ">" remains.
7:02 pm on Apr 21, 2013 (gmt 0)

Administrator from US 

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 25, 2005
posts:14662
votes: 95


That's because of your ending space in the string '> ' so you need both the space and the '>' in the rtrim stripping character list.
7:12 pm on Apr 21, 2013 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:July 4, 2007
posts: 193
votes: 0


I've tried the following ones but still nothing…

echo rtrim(str_replace('<br />', ' > ', $cats),'> ');
echo rtrim(str_replace('<br />', ' > ', $cats),' > ');
echo rtrim(str_replace('<br />', ' > ', $cats),' >');
echo rtrim(str_replace('<br />', ' > ', $cats),"> ");
echo rtrim(str_replace('<br />', ' > ', $cats)," > ");
echo rtrim(str_replace('<br />', ' > ', $cats)," >");
8:57 pm on Apr 21, 2013 (gmt 0)

Administrator from US 

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 25, 2005
posts:14662
votes: 95


Try breaking it down in segments so you can see the intermediary results as I've often found the failure was some odd result of nesting functions which is often unexpected but does happen now and then.

Also, I've found bugs in the specialized functions before, such as rtrim/ltrim() vs trim() itself, so using just trim() as opposed to ltrim() or rtrim() might also work but could have unintended consequences at times.
9:13 pm on Apr 21, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13436
votes: 389


But this is only part of the code. You're not spitting the > back into raw html, are you? It may not be as instantly disastrous as a lone < but I'd really prefer to see it converted into into &gt; before you let it loose on the world.

So structurally you've got two pieces.

First piece: replace all occurrences of "<br />" with something. Any one character will do, so long as it doesn't occur naturally in the text. Trim away the final occurrence of that character.

Second piece: replace all remaining "something" with "_&gt;_" (using lowlines as a standin for literal spaces because Forums won't display spaces inside markup).
9:23 pm on Apr 21, 2013 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:July 4, 2007
posts: 193
votes: 0


Wow, really appreciate everyone's help with this but this is all a little beyond my limited coding knowledge!
9:54 pm on Apr 21, 2013 (gmt 0)

Administrator from US 

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 25, 2005
posts:14662
votes: 95


I tried this simple test:
$cats=" Food > Fruit > Apple > ";
echo rtrim( $cats, '> ' );

And got the expected output:
"Food > Fruit > Apple"

So I tried this:
echo rtrim( " Food > Fruit > Apple > ", '> ' );

Same output

So I tried this:
$cats=" Food <br /> Fruit <br /> Apple <br /> ";
echo rtrim(str_replace('<br />', ' > ', $cats),'> ');

Same output, all worked.

No clue what's going on with your code but it works here.

Now I suspect you may have browser cache issues - clear out your cache, reload browser, REFRESH page, or test your code in a different browser because you may be seeing old results which happens all the time to me when playing with local host code testing.

[edited by: incrediBILL at 9:59 pm (utc) on Apr 21, 2013]

9:56 pm on Apr 21, 2013 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4845
votes: 4


There may be a newline or carriage return in there too.

echo rtrim(str_replace('<br />', ' > ', $cats),"> \r\n");

Should work if that's the case.
9:56 pm on Apr 21, 2013 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:July 4, 2007
posts: 193
votes: 0


Thanks for this. I'll give it another go in the morning when my head's a bit clearer.
9:59 pm on Apr 21, 2013 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:July 4, 2007
posts: 193
votes: 0


@brotherhood of LAN - that's it! It works perfectly.

Again, really really appreciate everyone's help with this.
11:16 am on Apr 24, 2013 (gmt 0)

Junior Member

5+ Year Member Top Contributors Of The Month

joined:July 4, 2007
posts: 193
votes: 0


Actually this doesn't work perfectly as it seemingly strips something out of content lower in my code and then messes up the formatting of the page.

Any other ideas?
2:00 pm on Apr 24, 2013 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4845
votes: 4


Try evaluating your code like this

echo '<pre>';
$cat_ids = implode(',' , $categories);
print_r($cat_ids);
$cats = wp_list_categories('title_li=Listed in:&style=none&echo=0&include='.$cat_ids);
print_r($cats);
echo str_replace('<br />', ' > ', $cats);
exit(0);

If there is an empty array value in $cat_ids, use array_pop() to get rid of it.

If there is (still) an empty array value in $cats, use array_pop() to get rid of it
6:49 pm on Apr 24, 2013 (gmt 0)

Administrator from US 

WebmasterWorld Administrator incredibill is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 25, 2005
posts:14662
votes: 95


TIP: you may find print_r() a little easier to read in the browser if you wrap it in something like <pre> tags.

Example:

echo '<pre>';
print_r(whatever);
echo '</pre>';

It's going to temporarily mess up the web page layout but we don't care at this point as we're debugging and reading that output as easily as possible is my top priority.


I made myself a function that always outputs in in <pre> format that I call pre_print_r() to avoid writing that stuff in the code over and over and to avoid forgetting to take it out as well.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members