spacepoet Posted February 28, 2012 Share Posted February 28, 2012 Hello: I wanted to see if someone knows how to do a "str_replace" before it's in the link .. This is the code I currently have and it does work, but I want the "str_replace" to take place before it gets added to the link: <?php $query = "SELECT product_id,myTitle FROM myProducts ORDER BY myTitle ASC"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $product_id = stripslashes($row['product_id']); $myTitle = stripslashes($row['myTitle']); ?> <li><a href="http://www.mywebsite.com/Promotional.Products-Promotional.Items/Promotional-Products/<?php echo $product_id ?>-<?php echo str_replace(" ", "-", $myTitle) ?>-<?php echo $full_state ?>-Promotional-Products.html" title="<?php echo $myTitle?> <?php echo "$full_state"; ?>"><?php echo $myTitle?></a></li> <?php } ?> Is there anyway I can make it happen here" $myTitle = stripslashes($row['myTitle']); Everything I have tried has not worked .. Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/257899-write-str_replace-before-its-in-the-link/ Share on other sites More sharing options...
kicken Posted February 28, 2012 Share Posted February 28, 2012 Just move the call to where you want it and save the result to a new variable, then put the variable in the link where the original call was. $myTitle = stripslashes($row['myTitle']); $myTitleDashed = str_replace(" ", "-", $myTitle); <li><a href="http://www.mywebsite.com/Promotional.Products-Promotional.Items/Promotional-Products/<?php echo $product_id ?>-<?php echo $myTitleDashed; ?>-<?php echo $full_state ?>-Promotional-Products.html" title="<?php echo $myTitle?> <?php echo "$full_state"; ?>"><?php echo $myTitle?></a></li> Quote Link to comment https://forums.phpfreaks.com/topic/257899-write-str_replace-before-its-in-the-link/#findComment-1321890 Share on other sites More sharing options...
spacepoet Posted February 28, 2012 Author Share Posted February 28, 2012 Hi: Thank you -worked perfectly! However, it did not solve my mod_rewrite issue (I thought it would). Any idea why this: #RewriteRule ^Promotional.Products-Promotional.Items/Promotional-Products/([0-9]+)-([a-zA-Z]+)-([a-zA-Z]+)-Promotional-Products.html$ Promotional.Products-Promotional.Items/Promotional-Products.php?product_id=$1&myTitleDashed=$2&full_state=$3 is not working? It keeps going to my 404 page. It works fine when I am doing: RewriteRule ^Promotional.Products-Promotional.Items/Promotional-Products/([0-9]+)-([a-zA-Z]+)-Promotional-Products.html$ Promotional.Products-Promotional.Items/Promotional-Products.php?product_id=$1&full_state=$2 <?php $query = "SELECT product_id,myTitle FROM myProducts ORDER BY myTitle ASC"; $result = mysql_query($query); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $product_id = stripslashes($row['product_id']); $myTitle = stripslashes($row['myTitle']); ?> <li><a href="http://www.mywebsite.com/Promotional.Products-Promotional.Items/Promotional-Products/<?php echo $product_id ?>-<?php echo $full_state ?>-Promotional-Products.html" title="<?php echo $myTitle?> <?php echo "$full_state"; ?>"><?php echo $myTitle?></a></li> <?php } ?> But when I add the product to it, it keeps failing. It has something to do with the spaces in the product name, because when there is a product with a single word, it goes to the page fine. I thought if I replaced the spaces with hyphens, it would match and I would not have any errors. Any ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/257899-write-str_replace-before-its-in-the-link/#findComment-1321921 Share on other sites More sharing options...
spacepoet Posted February 28, 2012 Author Share Posted February 28, 2012 It just dawned on me ... i think the middle regex needs to have a replace spaces with hyphens ... i think that will work ... do you know how to write that in?? Quote Link to comment https://forums.phpfreaks.com/topic/257899-write-str_replace-before-its-in-the-link/#findComment-1321943 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.