I am creating a redirect script I have the 2 variables "m" and "b" and I want to echo that whole url after b= including the bit after "&" visit: http://www.newdesignworld.com/link.php?m=1&b=http://www.newdesignworld.com/today&=c I am echoing the variable $b but it is only showing http://www.newdesignworld.com/today but i want it to show: http://www.newdesignworld.com/today&=c is this possible? thanks
Where you are echoing the variable, surround it with urlencode() like: <? echo urlencode($_REQUEST['b']); ?> http://www.php.net/urlencode
that encodes the url but i want it to include the extra "&=c" as well, but currently it is just showing up to: today
If you use _REQUEST['b'] to get at the parameter then it will only include up to the &. Anything after that is part of the next parameter. If it's always in the format you showed you can do something like $url = _REQUEST['b'] . "&c=" . _REQUEST['c'] If it need to be more flexable then you'll need to parse the string yourself. This is off the top of my head so ... $query_string = _SERVER['QUERY_STRING']; $pattern = '/b=(.*)/'; preg_match ($pattern, $query_string, $matches); $url = $matches[0];
That is sloppy and not good form. The proper solution to the problem is to use the functions that PHP provides for dealing with URLs. If proper form is not used I hope that in the future the script does not need to use another GET variable...otherwise there may be a lot of refactoring to do. This would be a shame since it could have been solved permanently with the use of url functions. Bobby
There is no way to use "the functions that PHP provides for dealing with URLs" to solve the problem as stated. I don't know where that initial url is coming from. If it's something you can change then by all means do so. If it's not something you can change then we have a working solution. Sure it's ugly but sometimes external requirements force us to do ugly things.
Actually...there is a way of doing this correctly. <?php /* * Define the base domain for the links below */ define('BASE_DOMAIN', 'http://www.newdesignworld.com/'); /* * Define the base script for the links below */ define('BASE_SCRIPT', 'link.php'); /** * Function to implode an array associatively * @author Bobby Easland, PHP.net manual * @version 1.0 * @param array $array Array to be imploded * @param string $inner_glue * @param string $outer_glue * @return array */ function implodeAssoc($array, $inner_glue='=', $outer_glue='&') { $output = array(); foreach( $array as $key => $item ){ $output[] = $key . $inner_glue . $item; } return implode($outer_glue, $output); } /** * Function to URL encode values of an array * @author Bobby Easland * @version 1.0 * @param array $array * @return array */ function fixArray($array){ $container = array(); foreach ( $array as $key => $value ){ $container[$key] = rawurlencode($value); } return $container; } /* * $initialize paramater container */ $params = array('m' => '1', 'b' => 'http://www.newdesignworld.com/today&=c'); /* * Create original link, NOT url encoded * NOTE: this is bad coding practice */ $link = BASE_DOMAIN . BASE_SCRIPT . '?' . implodeAssoc($params); echo '<b>Original link:</b> <a href="'.$link.'">'.$link.'</a><br>'; /* * Create proper URL encoded link */ $link = BASE_DOMAIN . BASE_SCRIPT . '?' . implodeAssoc(fixArray($params)); echo '<b>URL encoded link:</b> <a href="'.$link.'">'.$link.'</a><br>'; ?> PHP: In addition, he is using the Indexu application for his link directory script. I currently run 2 directories using that as a platform and am very familiar with the API. Whether he is using his own database abstraction layer or the default he simply needs to encode the URL on the way out and then urldecode before use. @ridesign => upload the above code to your site and click both links (one will be original the other in proper URL encoded format). I believe you'll see the benefit of doing things properly instead of down, dirty, and ugly. Bobby
Again you're changing the question. Given this url: http://www.newdesignworld.com/link.php?m=1&b=http://www.newdesignworld.com/today&=c parse it such that you get everything after the b= including any & and what follows. I think what it comes down to is that the best answer to the initial question is "you're asking the wrong question". Yes, it's better to change the incoming url to encode the query string in such a way that it can be parsed easily and properly. If you've never encountered a real world case where "change the input" wasn't a valid option you haven't been around for very long. Edit: And I get this in my "reputation" Show me how to accomplish the task as stated using good coding practice please.
I am not changing the question. The problem is that he is pulling the URL ($_GET['b']) from the database but it has a parameter itself. Hence, the solution is to urlencode() the value so that the entire string it is treated as a parameter...this will eliminate the error in parsing the $_GET['c'] from database record. No, he is not asking the wrong question. The issue is that you are not familiar with the Indexu application and the PHP functions that provide the solution. I have been around longer that some. In the solution provided I do not "change the input" rather transform it with the proper PHP function. Is that a sign of a beginner coder or one that actually has used the URL functions in a real world application? Before you spout off keep this in mind: I authored a contribution for the osCommerce application that transforms the dynamic URLs to static HTML using mod_rewrite. As such not only does it handle one or two parameters but anything that is thrown at it. The contribution is used on thousands of sites and is robust, scalable, feature rich, and mature. I have already posted a code snippet above that illustrates the proper use of the functions. Futher, I have tested it on my development server and checked the results against HIS OWN TEST SCRIPT. The output is clearly what he needs. Bobby
Sorry, I missed that part of the original post. Can you point out to me where this is mentioned? Clearly where it says "I am creating a redirect script" it means "I'm using the existing script Indexu". Verb * S: (v) transform (subject to a mathematical transformation) * S: (v) transform, transmute, transubstantiate (change or alter in form, appearance, or nature) "This experience transformed her completely"; "She transformed the clay into a beautiful sculpture"; "transubstantiate one element into another" * S: (v) transform, transmute, metamorphose (change in outward structure or looks) "He transformed into a monster"; "The salesman metamorphosed into an ugly beetle" * S: (v) translate, transform (change from one form or medium into another) "Braque translated collage into oil" * S: (v) transform (convert (one form of energy) to another) "transform energy to light" * S: (v) transform (change (a bacterial cell) into a genetically distinct cell by the introduction of DNA from another cell of the same or closely related species) * S: (v) transform (increase or decrease (an alternating current or voltage)) So which definition of transform DOESN'T mean change? I guess I'm having reading comprehension problems here. Never mind, it's not worth my time to get into a pissing match with you. You're clearly the expert who knows all because you submitted a contribution to osCommerce. I'll keep that in mind from now on.
The fact that I have created and released contributions for osC means very little if anything at all. However, the fact that I've created one that deals DIRECTLY WITH THE ISSUE AT HAND which is urlencoding parameters and general use of the PHP url functions is very relevant. Further, the fact that it is used on thousands of sites lets me know as a coder that the code works...and works very well. Even if this weren't true...the code I supply above fits his requirements and checks out on his own test script. Thus, I have offered a solution that is scalable, robust, and proper. Hence, I have experience creating enterprise level code that deals with URLs and the associated use of PHP URL functions. You asked in an earlier post whether I had this experience. However, once I post the reply that I have experience you requested you reply back negatively...I was just giving you what you asked for. Have a nice day. Bobby