gladtobegrey Posted October 12, 2009 Share Posted October 12, 2009 I'm a PHP novice. I have code that dynamically build a webpage that displays reviews/comments about a restaurant from XML content, one component of which is a link to a source webpage if the review is published elsewhere. The code parses the xml file; if a <link>....</link> entry is found it builds the anchor, thus ... On entry $source contains the name and date of the review as text, $data contains the target URL as text (e.g. 'http://www.eatoutpoole.com/reviews/myresturant.php' or similar). The 'LINK' case below stores the name and date string, and wraps the HTML anchor code around it. function tag_contents($parser, $data) { global $source, $current_tag; $patterns = array ("/\[\^\]/","/\[\~\]/","/\[/","/\]/","/\t/"); $replaces = array ("<br />"," ","<",">",""); $result = preg_replace($patterns, $replaces,$data); switch ($current_tag) { case "TEXT": echo '<div class="reviewtext">'.$result.'</div>'."\n\r"; break; case "NAME": $source = $result; break; case "DATE": $source .= ', '; $source .= $result; break; case "LINK": $savsrc = $source; $source = '<a href="'; $source .= $result; $source .= '" target="_blank">'; $source .= $savsrc; $source .= '</a>'; break; } } The regex converts BBcode-like formatting commands into HTML tags (e.g. '{b}' to '<b>', but these are not used in the <link></link> tags, so can (I think) be ignored here. The bit I don't understand is this; originally line 3 of the "LINK" case above was coded thus ... $source .= urlencode($result); ... but when run live this appended the url in $result to the url of the webpage... <a href="http://www.mywebpage.co.ukhttp://www.eatoutpoole.com/reviews/myrestaurant.php" ... etc Removing urlencode() as above created the expected url for the anchor ... <a href="http://www.eatoutpoole.com/reviews/myresturant.php" target="_blank .... etc Can some king soul enlighten me as to why it behaved in this way? I've searched several PHP sites to no avail. What am I missing or not understanding? Quote Link to comment https://forums.phpfreaks.com/topic/177480-building-html-anchors-with-urlencode/ Share on other sites More sharing options...
.josh Posted October 12, 2009 Share Posted October 12, 2009 hmm... it makes no sense that this: $source .= $result; would work, but changing it to this: $source .= urlencode($result); would somehow make an extra url in there. Are you sure you didn't change anything else? Quote Link to comment https://forums.phpfreaks.com/topic/177480-building-html-anchors-with-urlencode/#findComment-935807 Share on other sites More sharing options...
gladtobegrey Posted October 13, 2009 Author Share Posted October 13, 2009 hmm ... Are you sure you didn't change anything else? Yes. To be absolutely sure, I've re-run it live with both versions of the code, and snipped an example of the output from both. With $source .= $result; I get ... <div class="reviewtext">I have lived in Poole a few years now and La Lupa is one of the few places we love to go back to again and again as the food is good and you get a good crack with the staff.</div> <div class="reviewsource"><a href="http://www.restaurant-guide.com/la-lupa-3.htm" target="_blank">Dee, 26th September 2009</a></div> <hr class="horizline" /> </div> ... and it opens the correct page when the link's clicked. With $source .= urlencode($result); I get ... <div class="reviewtext">I have lived in Poole a few years now and La Lupa is one of the few places we love to go back to again and again as the food is good and you get a good crack with the staff.</div> <div class="reviewsource"><a href="http%3A%2F%2Fwww.restaurant-guide.com%2Fla-lupa-3.htm" target="_blank">Dee, 26th September 2009</a></div> <hr class="horizline" /> </div> However, when I click the link, it tries to open : http://www.la-lupa-3.co.uk/http://www.restaurant-guide.com/la-lupa-3.htm I've also tried changing the <link>...</link> content by removing the 'http://' at the start of the url string, but the same behaviour results. I'm running the test on Google Chrome (3.0.195.25), but I see the exact same behaviour in all the other browsers I've tested in - IE7 (I don't have IE8), Firefox (3.0.10) and Safari (4.0.3). All running under WinXP SP3 here. The web hosting package I'm constrained to use runs PHP 4.4.2 ... see more at: http://www.la-lupa-3.co.uk/phpinfo.php PS For completeness, here's a snip from the xml file supplying the content: <?xml version="1.0" encoding="ISO-8859-1"?> <read> <review> <text>I have lived in Poole a few years now and La Lupa is one of the few places we love to go back to again and again as the food is good and you get a good crack with the staff.</text> <name>Dee</name> <date>26th September 2009</date> <link>http://www.restaurant-guide.com/la-lupa-3.htm</link> </review> <review> <text>[p]Just a quick e-mail to say a very big THANK YOU to all the staff who looked after my son and his friends when he celebrated his 18th birthday at La[~]Lupa last Thursday.[/p][p]They all enjoyed the evening, thought the food was fantastic and my son was really pleased with everything. We believe you serve the best risotto in the world!![/p][p]Many thanks once again[/p]</text> <name>Jo A (by email)</name> <date>7th September 2009</date> </review> ..... </read> Quote Link to comment https://forums.phpfreaks.com/topic/177480-building-html-anchors-with-urlencode/#findComment-935936 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.