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=?

 Answers

47

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
 
LaKaede
answered 7 Months ago
73

Try this:

$str = 'http://www.example.com/some_folder/some 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
 
TheTechnicalPaladin
answered 9 Months ago
44

Here is how the referred code example supposed to work:

product-category.php

<?php
define( 'INCLUDE_DIR', dirname( __FILE__ ) . '/' );

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

$uri = $_SERVER['REQUEST_URI'];
$uri = urldecode( $uri );

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

redirect-category.php

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

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

Friday, August 6, 2021
 
koenHuybrechts
answered 4 Months ago
58

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
Monday, August 30, 2021
 
Tak
answered 3 Months ago
Tak
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 :
 
Share