Kane250 Posted June 2, 2009 Share Posted June 2, 2009 Hey there, So, I am modifying wordpress lately, which is all PHP, and I'm trying to change tags in URL's so they do not contain spaces or apostrophes. This was screwing up my URL's. So here is what I did: <?php $title = get_the_title(); $title_link = str_replace(" ", "_", $title);?> <h2><a href="?tag=<?php echo str_replace("'", "", $title_link); ?>"><?php echo $title; ?></a></h2> get_the_title() if you're not familiar with Wordpress just returns the title of a post, so in my case it's just a word of a couple words. The above worked for anything that had a space, and added an underscore as requested, but it will not remove the apostrophes and I don't understand why. Anyone see a good reason why this would not work correctly? Thanks for any help in advance. I've been stuck on this one for days... Link to comment https://forums.phpfreaks.com/topic/160637-solved-str_replace-in-wordpress-am-i-doing-something-wrong/ Share on other sites More sharing options...
Jibberish Posted June 2, 2009 Share Posted June 2, 2009 I have just tried it with this test code <?php $title = "link's" ; $title_link = str_replace(" ", "_", $title);?> <h2><a href="?tag=<?php echo str_replace("'", "", $title_link); ?>"><?php echo $title; ?></a></h2> and it removes the apostrophes out of the href part of the <a> link and leaves it in in the text that is echoed out between the <a> link. so in html terms it looks like <a href="?tag=links"> link's </a> Is this what you want it to do? Link to comment https://forums.phpfreaks.com/topic/160637-solved-str_replace-in-wordpress-am-i-doing-something-wrong/#findComment-847739 Share on other sites More sharing options...
Kane250 Posted June 2, 2009 Author Share Posted June 2, 2009 Yeah that is how it is supposed to function, remove the apostrophe from the actual link but leave it in the visible title. I have no idea why it wont work in Wordpress. The apostrophe always shows up in the url.. Link to comment https://forums.phpfreaks.com/topic/160637-solved-str_replace-in-wordpress-am-i-doing-something-wrong/#findComment-847749 Share on other sites More sharing options...
Jibberish Posted June 2, 2009 Share Posted June 2, 2009 The only thing i can think of is that its returning a html escaped string, so its returning the apostrophe as a html code, hence str_replace not working try <?php $title = get_the_title() ; $title_link = html_entity_decode(str_replace(" ", "_", $title));?> <h2><a href="?tag=<?php echo str_replace("'", "", $title_link); ?>"><?php echo $title; ?></a></h2> Link to comment https://forums.phpfreaks.com/topic/160637-solved-str_replace-in-wordpress-am-i-doing-something-wrong/#findComment-847754 Share on other sites More sharing options...
Kane250 Posted June 2, 2009 Author Share Posted June 2, 2009 That did not work either, but I did just try urlencode() and it converts the apostrophe to this: %26%238217%3B and somehow, it fixes my problem! Is that the html equivalent of an apostrophe or something? Link to comment https://forums.phpfreaks.com/topic/160637-solved-str_replace-in-wordpress-am-i-doing-something-wrong/#findComment-847765 Share on other sites More sharing options...
Jibberish Posted June 2, 2009 Share Posted June 2, 2009 Yeah I can never remeber which things what when it comes to url encoding and html ones Good job on getting it sorted though. Link to comment https://forums.phpfreaks.com/topic/160637-solved-str_replace-in-wordpress-am-i-doing-something-wrong/#findComment-847768 Share on other sites More sharing options...
Kane250 Posted June 2, 2009 Author Share Posted June 2, 2009 Me either haha, Thanks for your assistance Link to comment https://forums.phpfreaks.com/topic/160637-solved-str_replace-in-wordpress-am-i-doing-something-wrong/#findComment-847771 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.