Jump to content

[SOLVED] str_replace in wordpress, am I doing something wrong?


Kane250

Recommended Posts

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...

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?

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>


 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.