Asked  7 Months ago    Answers:  5   Viewed   37 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=?



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
Saturday, May 29, 2021
answered 7 Months ago

Try this:

$str = ' file [that] needs "to" be (encoded).zip';
$pos = strrpos($str, '/') + 1;
$result = substr($str, 0, $pos) . urlencode(substr($str, $pos));

You're looking for the last occurrence of the slash sign. The part before it is ok so just copy that. And urlencode the rest.

Wednesday, March 31, 2021
answered 9 Months ago

Here is how the referred code example supposed to work:


define( 'INCLUDE_DIR', dirname( __FILE__ ) . '/' );

$rules = array( 
    'redirect-category'  => "/product-category\.php\?cat_id=(?'category'\d+)"

$uri = urldecode( $uri );

foreach ( $rules as $action => $rule ) {
    if ( preg_match( '#'.$rule.'#', $uri, $params ) ) {
        include( INCLUDE_DIR . $action . '.php' );


$categories = array(
    2 => 'men-items'
header("Location: " . preg_replace( '#'.$rule.'#', "/product_category/{$categories[$params['category']]}/", $uri ));
Saturday, May 29, 2021
answered 7 Months ago

Use parse_url like below:

$url = '';

$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:

Friday, August 6, 2021
answered 4 Months ago

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, "" );

$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

  • DOMDocument documentation
  • W3Schools DOMDocument example
Monday, August 30, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :