Jump to content

str_replace help


webready

Recommended Posts

Hello All,

 

I'm using php to auto-insert variables into a canonical url within the <head>.

 

Here is my code:

<link rel="canonical" href="http://www.mysite.com/page.php?IT=<?php print $item; ?>&IT2=<?php print $desc; ?>" />

 

I used this to replace "+" with " ". <?php $desc  = str_replace ("+"," ",$desc); ?>

 

The problem is i use the variables in different sections of the page were I don't want the "+" to appear.

 

I only want the "+" to appear in the canonical url space.

 

I'm sure there is an easy way, but not for me :)

 

So to sum up: I want srt_replace code to be incorporated into the <?php print $desc; ?> code ONLY.

 

Thanks in advance for any help!

Link to comment
Share on other sites

hehe np, i just make it more clear (i hope  ::))

 

<link rel="canonical" href="http://www.mysite.com/page.php?IT=<?php print $item; ?>&IT2=<?php echo str_replace ("+"," ",$desc); ?>" />

 

So just like i showed you I echo str_replace instead of assigning $desc again.

to explain that in more detail:

if you do this

$desc = str_replace  ("+"," ",$desc);  // here you are assiging $desc , and thus altering it's value

but if you do this:

echo str_replace  ("+"," ",$desc);  // you are just outputing the value of $desc, 
                                                 //but altering the output via str_replace instead 
                                               //of altering $desc, that way it keeps it's value to be used below like you wanted

Link to comment
Share on other sites

Ok, I knew there would be something else :)

 

In some case I actually use "%2D" in my URL's were I want the. The "%2D" stays in the URL address bar. But, in the canonical URL, it shows a "+". Anyway to make sure the canonical version prints the "%2D" and not the decoded "+". I know, I'm crazy!

 

Thanks again :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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