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
https://forums.phpfreaks.com/topic/214196-str_replace-help/
Share on other sites

and what if you echo it out like

<?php  echo str_replace ("+"," ",$desc);?>

That way you don't have to change the variable but only change the output of the variable like:

 

<?php
$desc = 'lalalalaal + laalalala + lalalalala +';
echo str_replace ("+"," ",$desc);
?>

Link to comment
https://forums.phpfreaks.com/topic/214196-str_replace-help/#findComment-1114518
Share on other sites

Hi Fortnox007,

 

Thanks for the reply. Unfortunately I'm even more confused.

 

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

 

like <?php echo $desc; "NEW STR CODE GOES HERE"?>

 

Thanks again!

Link to comment
https://forums.phpfreaks.com/topic/214196-str_replace-help/#findComment-1114525
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
https://forums.phpfreaks.com/topic/214196-str_replace-help/#findComment-1114527
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
https://forums.phpfreaks.com/topic/214196-str_replace-help/#findComment-1114546
Share on other sites

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.