eMonk Posted May 7, 2011 Share Posted May 7, 2011 How can I add str_replace(" ", "-") to the following line: echo "<p>" . $_SERVER['SERVER_NAME'] . "/" . strtolower(stripslashes($row['name'])) .".php</p>"; I have a bunch of provinces being displayed and want to replace spaces with - (for example British Columbia turns into www.domain.com/british-columbia.php so I can link to this file). Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/ Share on other sites More sharing options...
spiderwell Posted May 7, 2011 Share Posted May 7, 2011 wrap it around the string to lower would work, or around stripslashes or around $row['']. There is probably a very small difference in execute time, but the differences would be marginal as to be un noticeable. Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1211866 Share on other sites More sharing options...
eMonk Posted May 7, 2011 Author Share Posted May 7, 2011 I tried but it doesn't work? echo "<p>" . $_SERVER['SERVER_NAME'] . "/" . strtolower(stripslashes(str_replace($row['name'])" ", "-")) .".php</p>"; Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1211914 Share on other sites More sharing options...
wildteen88 Posted May 7, 2011 Share Posted May 7, 2011 Because you have missed of the comma in str_replace echo "<p>" . $_SERVER['SERVER_NAME'] . "/" . strtolower(stripslashes(str_replace($row['name'])," ", "-")) .".php</p>"; Notice the highlighted comma. Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1211926 Share on other sites More sharing options...
jcbones Posted May 7, 2011 Share Posted May 7, 2011 I believe that should be: echo "<p>" . $_SERVER['SERVER_NAME'] . "/" . strtolower(stripslashes(str_replace(" ","-",$row['name']))) .".php</p>"; Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1211942 Share on other sites More sharing options...
eMonk Posted May 7, 2011 Author Share Posted May 7, 2011 I think that works jcbones but when I tried to put it into a href I get the following error: echo "<a href=\" . $_SERVER['SERVER_NAME'] . "/" . strtolower(stripslashes(str_replace(" ", "-", $row['name']))) .".php">stripslashes($row['name'])</a>"; Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1211982 Share on other sites More sharing options...
PaulRyan Posted May 7, 2011 Share Posted May 7, 2011 Try this... echo "<a href='\".$_SERVER['SERVER_NAME']."/".strtolower(stripslashes(str_replace(" ", "-", $row['name']))) .".php'> ".stripslashes($row['name'])." </a>"; Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1211985 Share on other sites More sharing options...
eMonk Posted May 7, 2011 Author Share Posted May 7, 2011 I'm getting the same error PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1211986 Share on other sites More sharing options...
PaulRyan Posted May 7, 2011 Share Posted May 7, 2011 echo "<a href='".$_SERVER['SERVER_NAME']."/".strtolower(stripslashes(str_replace(" ", "-", $row['name']))) .".php'> ".stripslashes($row['name'])." </a>"; Well the error is not coming from that line I just gave you, as I've been over 10 times and all is correct. Most likely another line is causing the error. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1211991 Share on other sites More sharing options...
eMonk Posted May 7, 2011 Author Share Posted May 7, 2011 Are you sure? The error is coming from that line number according to the error message. Also when I pasted the code it turns up as all red, usually there's blue and orange colors when it's going to work. Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1211993 Share on other sites More sharing options...
spiderwell Posted May 7, 2011 Share Posted May 7, 2011 why not remove all the nested functions, and do the str replace on its own first once that is working start putting the lowercase and stripslashes back in one at a time Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1211995 Share on other sites More sharing options...
eMonk Posted May 7, 2011 Author Share Posted May 7, 2011 Good tip... I can't even get str_replace() to work on its own. echo "<a href='\".$_SERVER['SERVER_NAME']."/".str_replace(" ", "-",$row['name']).".php'>".stripslashes($row['name'])."</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1212000 Share on other sites More sharing options...
salathe Posted May 7, 2011 Share Posted May 7, 2011 \" should be " in that last line of code. Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1212004 Share on other sites More sharing options...
eMonk Posted May 7, 2011 Author Share Posted May 7, 2011 Ah, I can't believe I missed that. Thanks salathe! Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1212006 Share on other sites More sharing options...
eMonk Posted May 7, 2011 Author Share Posted May 7, 2011 Any idea why my domain is showing up twice in the output? http://www.domain.com/v1/www.domain.com/british-columbia.php Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1212008 Share on other sites More sharing options...
eMonk Posted May 7, 2011 Author Share Posted May 7, 2011 BTW the output should be: http://www.domain.com/v1/british-columbia.php Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1212013 Share on other sites More sharing options...
wildteen88 Posted May 7, 2011 Share Posted May 7, 2011 What is the contents of $row['name']? Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1212014 Share on other sites More sharing options...
eMonk Posted May 7, 2011 Author Share Posted May 7, 2011 What is the contents of $row['name']? The name of the province, for example, British Columbia. Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1212016 Share on other sites More sharing options...
eMonk Posted May 7, 2011 Author Share Posted May 7, 2011 I just removed $_SERVER['SERVER_NAME'] and it appears to be working now. Thanks for the help everyone! Quote Link to comment https://forums.phpfreaks.com/topic/235757-str_replace/#findComment-1212034 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.