Viewed   70 times

I have a lang system in my website. When people click to switch to another language, the following will be added to the url: ?lang=. The problem is, my website is divided by pages which add ?p= to the url aswell. So basically, if they change the lang on one of those pages, it'll overwrite the ?p= and go back to the main page. So it'll be index.php?lang=.

What is the code or how should I code it so that php verifies if there's already a ? string in the url and switch ?lang= to &lang=?

 Answers

1

You can use http_build_query to avoid this problems:

$params = array(
    'p'    => 'foo',
    'lang' => 'bar'
);

echo http_build_query($params); // p=foo&lang=bar

echo '?' . http_build_query($params); // ?p=foo&lang=bar
Thursday, December 22, 2022
 
5

This code may be helpful (see MadTechie's latest post):

http://www.phpfreaks.com/forums/index.php/topic,245248.msg1146218.html#msg1146218

<?php
$string = "some random text http://tinyurl.com/9uxdwc some http://google.com random text http://tinyurl.com/787988";

$regex = '$b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]$i';

preg_match_all($regex, $string, $result, PREG_PATTERN_ORDER);
$A = $result[0];

foreach($A as $B)
{
   $URL = GetRealURL($B);
   echo "$URL<BR>";   
}


function GetRealURL( $url ) 
{ 
   $options = array(
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_HEADER         => true,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_ENCODING       => "",
      CURLOPT_USERAGENT      => "spider",
      CURLOPT_AUTOREFERER    => true,
      CURLOPT_CONNECTTIMEOUT => 120,
      CURLOPT_TIMEOUT        => 120,
      CURLOPT_MAXREDIRS      => 10,
   ); 

   $ch      = curl_init( $url ); 
   curl_setopt_array( $ch, $options ); 
   $content = curl_exec( $ch ); 
   $err     = curl_errno( $ch ); 
   $errmsg  = curl_error( $ch ); 
   $header  = curl_getinfo( $ch ); 
   curl_close( $ch ); 
   return $header['url']; 
}  

?>
Friday, October 14, 2022
 
1

$_SERVER['QUERY_STRING'] does not include the ? so when you do $url = str_replace($query,'',$uri);, you are not replacing the ?. It's therefore looking for a controller named controller?

There are various ways around this

  • replace with '?'.$query
  • Use explode('?', $url) to separate the query string from the URI
  • Get the $_SERVER['REQUEST_URL'] (which doesn't include the query string), rather than getting the whole thing and then splitting out yourself

Personally I would go with the last option, because wherever the code is already written for you, it tends to be quicker and more robust than anything you can write.

Sunday, October 16, 2022
 
5

Use parse_url like below:

<?php
$url = 'http://www.example.com/news?q=string&f=true&id=1233&sort=true';

$values = parse_url($url);

$host = explode('.',$values['host']);

echo $host[1];

?>

This would work for any url that has the sub domain included (www. etc)

The PHP documentation can be found here: http://php.net/manual/en/function.parse-url.php

Sunday, September 18, 2022
 
cheatex
 
4

One of the best options in my opinion is to use CURL to get the raw XML data from the url:

$curl = curl_init();
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $curl, CURLOPT_URL, "http://somedomain.com/frieventexport.php" );

$xml = curl_exec( $curl );
curl_close( $curl );

You can then use DOMDocument to parse the xml:

$document = new DOMDocument;
$document->loadXML( $xml );

I would also recommend using <![CDATA[]> tags in your XML. Please read the following:

  • What does <![CDATA[]]> in XML mean?
  • CDATA Sections in XML

More information about DOMDocument and usage

  • PHP.net DOMDocument documentation
  • W3Schools DOMDocument example
Saturday, October 29, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :