herghost Posted January 28, 2008 Share Posted January 28, 2008 Well, using the little php I know I have come up with this: <?php function changelink($change) { $find = "htmlspecialchars($row["source"])"; $replace = "<a href="http://www.amazon.co.uk/dp/($row["source"])?tag=speciainvita-21&camp=1406&creative=6394&linkCode=as1&creativeASIN=($row["source"])" />"; $change = preg_replace($find, $replace, $change); return $change; } <?php echo $change; ?> Which obviously doesn't work! What I am trying to do is insert "source" into various places in the bottom line. I am guessing there is another way of doing this apart from the find and replace method as I am guessing trying to find a string will not work. I just wanna know how to do this! I welcome anyone who can show me how to do this and explain how it works as I am really trying to learn php and not just copy and paste! This is the line originally: <?php echo htmlspecialchars($row["source"]); ?> and "source" is gathered using: <input name="source" type="text" value="<?php echo htmlspecialchars(stripslashes($source)); ?>" maxlength="255" size="50"/> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/ Share on other sites More sharing options...
PersonPerson Posted January 28, 2008 Share Posted January 28, 2008 Escape all double quotes except the initial and final ones. Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-450836 Share on other sites More sharing options...
teng84 Posted January 28, 2008 Share Posted January 28, 2008 this should work $find = htmlspecialchars($row["source"]); $replace = '<a href="http://www.amazon.co.uk/dp/('.$row["source"].')?tag=speciainvita-21&camp=1406&creative=6394&linkCode=as1&creativeASIN=('.$row["source"].')" />'; Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-450840 Share on other sites More sharing options...
cooldude832 Posted January 28, 2008 Share Posted January 28, 2008 that will never work ever regardless of if you fix quotes go to php.net and look at variable scope you are trying to use global variables (or what looks to be global) in a function and that isn't going to work with your syntax. $row['source'] is undefined in the function thus it can't do anything about it. Your even trying to use the local variable $change outside the function in the global area that won't work also. Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-450841 Share on other sites More sharing options...
teng84 Posted January 28, 2008 Share Posted January 28, 2008 that will never work ever regardless of if you fix quotes go to php.net and look at variable scope you are trying to use global variables (or what looks to be global) in a function and that isn't going to work with your syntax. $row['source'] is undefined in the function thus it can't do anything about it. Your even trying to use the local variable $change outside the function in the global area that won't work also. oh sorry i don't see that i though its not inside the function and i dont see this function changelink($change) Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-450842 Share on other sites More sharing options...
herghost Posted January 28, 2008 Author Share Posted January 28, 2008 Thanks for your help so far, so how do i get this to work? can I do it without creating a function, my knowledge of php is very limited, im learning as I go! Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-450877 Share on other sites More sharing options...
herghost Posted January 28, 2008 Author Share Posted January 28, 2008 Is there anyway to get this to work? Thanks dave Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-451621 Share on other sites More sharing options...
herghost Posted January 30, 2008 Author Share Posted January 30, 2008 ok I have been looking at this for a few days now and have got no closer, any ideas people? Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-453820 Share on other sites More sharing options...
cooldude832 Posted January 30, 2008 Share Posted January 30, 2008 explain clearly what u trying to do Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-453822 Share on other sites More sharing options...
atlanta Posted January 30, 2008 Share Posted January 30, 2008 its because your using preg_replace and not using the correct syntax for it . Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-453824 Share on other sites More sharing options...
herghost Posted January 30, 2008 Author Share Posted January 30, 2008 Ok, The user inserts a product number, for example : 1000 and it is stored using this: <input name="source" type="text" value="<?php echo htmlspecialchars(stripslashes($source)); ?>" maxlength="255" size="50"/> I then want to display "source" inside a line: i.e http://www.amazon.co.uk/dp/"source"?tag=speciainvita-21&camp=1406&creative=6394&linkCode=as1&creativeASIN="source") using the echo Is this possible? Thanks dude sorry post updates : I know I am using the wrong syntax, i just have no idea what to use or what it does, it is outside my limited knowledge of php Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-453830 Share on other sites More sharing options...
atlanta Posted January 30, 2008 Share Posted January 30, 2008 try <?php function changelink($change) { $find = "source"; $replace = htmlspecialchars($change); $change = '<a href="http://www.amazon.co.uk/dp/(source)?tag=speciainvita-21&camp=1406&creative=6394&linkCode=as1&creativeASIN=(source)" > test</a>'; $change2 = str_replace($find, $replace, $change); echo $change2; } changelink($row["source"]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-453837 Share on other sites More sharing options...
herghost Posted January 30, 2008 Author Share Posted January 30, 2008 Thanks for this, I think I understand it! I am I correct in thinking that I can load the function by saving it as say functions.php and calling it by typing: <?php echo $row["source"]; ?> making sure I include functions.php thanks Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-453941 Share on other sites More sharing options...
herghost Posted January 30, 2008 Author Share Posted January 30, 2008 One last question, I have it working, almost! The only thing is that when the link is echo'ed it is placing the brackets around the code, so the url is not working, how do I get it to display the code without the brackets? Thanks Dave Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-453974 Share on other sites More sharing options...
herghost Posted January 30, 2008 Author Share Posted January 30, 2008 nevermind got it! Thanks to everyone that helped Quote Link to comment https://forums.phpfreaks.com/topic/88116-solved-correct-my-code/#findComment-453979 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.