samoht Posted September 30, 2011 Share Posted September 30, 2011 Hello, I am having a problem with a link text when ever there is a text that has multiple words. (e.g. 2 Cor prints out with the white space but when I click on the text the url is printed without the text as 2Cor and so it doesn't find a match) here is my code: <?php if($_GET['book'] == ''){ $books = $wpdb->get_results(" SELECT book_name FROM {$wpdb->prefix}sb_books_sermons LEFT JOIN {$wpdb->prefix}sb_books ON {$wpdb->prefix}sb_books_sermons.book_name = name GROUP BY book_name ORDER BY {$wpdb->prefix}sb_books.id ASC "); foreach($books as $book){ echo '<li><div class="droidlist"><a href="bible/?book='.htmlentities($book->book_name).'">'.$book->book_name.'</a></div></li>'."\n\t"; } } Quote Link to comment https://forums.phpfreaks.com/topic/248189-space-in-link-text-problem/ Share on other sites More sharing options...
samoht Posted September 30, 2011 Author Share Posted September 30, 2011 alright, I changed the htmlentities to preg_replace - to search for any spaces and replace with %20 like so: <?php foreach($books as $book){ echo '<li><div class="droidlist"><a href="bible/?book='.preg_replace('[\s]', '%20', $book->book_name).'">'.$book->book_name.'</a></div></li>'."\n\t"; } } This loos like it should work but again when I click on the link the url removes the %20 and the space and I am back with 2Cor ?? Quote Link to comment https://forums.phpfreaks.com/topic/248189-space-in-link-text-problem/#findComment-1274444 Share on other sites More sharing options...
Pikachu2000 Posted September 30, 2011 Share Posted September 30, 2011 There are functions to handle url encoding: urldecode/urlencode. Quote Link to comment https://forums.phpfreaks.com/topic/248189-space-in-link-text-problem/#findComment-1274445 Share on other sites More sharing options...
samoht Posted September 30, 2011 Author Share Posted September 30, 2011 Thanks, but when I use urlencode() it adds a '+' where the white space is (all good) - which is then removed in the url after I click the link? I assume I need to use urldecode() - but where?? is my .htaccess rewriting the url? Quote Link to comment https://forums.phpfreaks.com/topic/248189-space-in-link-text-problem/#findComment-1274448 Share on other sites More sharing options...
samoht Posted September 30, 2011 Author Share Posted September 30, 2011 ok, I am trying rawurlencode() now and it is writing in the %20 then ON the page reload when I get the variable from the url I am using rawurldecode() but the space is still being removed before I run my query?? <?php if($_GET['book'] == ''){ $books = $wpdb->get_results(" SELECT book_name FROM {$wpdb->prefix}sb_books_sermons LEFT JOIN {$wpdb->prefix}sb_books ON {$wpdb->prefix}sb_books_sermons.book_name = name GROUP BY book_name ORDER BY {$wpdb->prefix}sb_books.id ASC "); foreach($books as $book){ echo '<li><div class="droidlist"><a href="bible/?book='.htmlentities(rawurlencode($book->book_name)).'">'.$book->book_name.'</a></div></li>'."\n\t"; } }else { $book = $wpdb->escape(rawurldecode($_GET['book'])); Quote Link to comment https://forums.phpfreaks.com/topic/248189-space-in-link-text-problem/#findComment-1274457 Share on other sites More sharing options...
samoht Posted September 30, 2011 Author Share Posted September 30, 2011 So, I got it to work sort of! I removed the relative link and for some reason that worked. So then I tried just shortening the relative link to <a href="?book='.rawurlencode($book->book_name).'">' Which also worked. - not sure why adding the /bible/ in front of the query broke it? Quote Link to comment https://forums.phpfreaks.com/topic/248189-space-in-link-text-problem/#findComment-1274489 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.