Changing language variable in URL [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Replace values in a URI query string
I am using a language switcher in the navigation bar to switch between EN and DE, my url structure is like this:
http://www.mydomain.com/gallery.php?lang=de
http://www.mydomain.com/gallery-item.php?id=100&lang=de
The switch works well on all urls which don't have an id, but it doesn't work with the ids.
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
<a href='.$url.'?lang=en">
<a href='.$url.'?lang=de">
?>
What is a good solution to check for $lang=xx or ?lang=xx and then add ?lang=xx or $lang=xx respectively?

$params = $_GET;
$params['lang'] = 'en';
printf('En', $url, http_build_query($params));
$params['lang'] = 'de';
printf('De', $url, http_build_query($params));
Or even very compact:
printf('De',
$url, http_build_query(array('lang' => 'de') + $_GET));
http://php.net/http_build_query

Related

Scrape link in web page with specific class php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
as the title suggests, I would like to retrieve links that have a specific class.
I have the code to connect to the pages and with the preg_match function I would like to take only the url that is in href = "url".
the structure of the link I would like to take and the one found in href = "", this link is in a table and can also have other attributes but not id, only the view class.
<a title="viwe" class="view" href="link">blablabla</a>
while I wrote this code
$curl = curl_init('http://prove/prove/pag/test.php');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$page = curl_exec($curl);
if(curl_errno($curl)) // check for execution errors
{
echo 'Scraper error: ' . curl_error($curl);
exit;
}
curl_close($curl);
$regex = '/<a.*?>(.*?)<\/a>/';
if ( preg_match($regex, $page, $list) )
echo $list[0];
else
print "Not found";
Well, the method you are trying to implement is not recommended, yet if you have to, this expression might be closer to what you have in mind, that I'm guessing:
<a\s.*?\sclass="\s*view\s*"[^>]*>.*?<\/a>
Demo

PHP get the parameters that are put after the question mark in an url [duplicate]

This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 6 years ago.
I have an url that I call in this form :
http://MyIP/MyServer/php_scripts/test_sp.php?param1=key&param2=xyz5
and I would like to retrieve the value of the 'param1' and 'param2' parameters. Up to now, I was not successful, because all I get is an empty value.
Sure that I am certainly doing something wrong there, but is it with the way I am calling the url or in my php code itself? Any help will be highly appreciated. Thanks a lot for reading me.
The code below is the contents of my 'test_sp.php' file :
<html>
<head>
</head>
<body>
<?php
mainProcess();
function mainProcess()
{
$mavalue1 = "";
$mavalue2 = "";
$parts = parse_url($url, PHP_URL_QUERY); // << It looks like I also have a problem with this line.
parse_str($query, $params);
$mavalue1 = $_GET['param1'];
$mavalue2 = $_GET['param2'];
echo "Valeur de mavalue1 : " . $mavalue1; // <<< This is where I'm getting the empty value
echo "Valeur de mavalue2 : " . $mavalue2; // <<< Same problem here
}
?>
</body>
</html>
The $_GET array is an array with all the arguments passed in the URL.
You can use $_GET['param1']; to get the value of "param1" in the URL.
Have a nice day.
$mavalue1 = $_GET['param1'];
$mavalue2 = $_GET['param2'];

Get full website URL? [duplicate]

This question already has answers here:
Get the full URL in PHP
(27 answers)
Closed 8 years ago.
I have a function which looks like this:
function fullURL(){
$domain = $_SERVER['SERVER_NAME'];
$path = $_SERVER['PHP_SELF'];
$raw_url = $domain.$path;
$url = substr($raw_url, 0, -4);
echo $url;
}
This generates an URL which looks like 127.0.0.1/aura/profile
But, what if I would use this on a page which uses a $_GET-tag?
Then, my URL won't pick it up.
For example I have news?id=1, when using the function above it just becomes 127.0.0.1/aura/news.
What do I need to add to the function to make it output 127.0.0.1/aura/news?id=(id)?
What about this?
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Note that you could also do:
$actual_link = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
Original answer: https://stackoverflow.com/a/6768831/3150271

Replace multiple elements in one string [duplicate]

This question already has an answer here:
How do I use str_replace() with multiple parameters?
(1 answer)
Closed 8 years ago.
I have the following string: "English,French,German".
Replacing one element in this string is no problem, doing this:
#if (strpos($language,'English') !== false)<dd>{{{ $malestr = str_replace("English", "Eng.", $language)}}}</dd>#endif
Resulting in: "Eng.,French,German"
Now the problem is I want to replace all three elements and return only one string, but I don't know how to get to this. Can you help?
--
edit: thanks to Mahammad Alabed, found the solution in your link. This worked:
{{{$result = str_replace(
array('English', 'French', 'German'),
array('Eng.', 'Fr.', 'Ge.'),
$language
)}}}
$languages = array(
'English' => 'Eng.',
'French' => 'Fre.',
'German' => 'Ger.',
);
$malestr = str_replace(array_keys($languages), array_values($languages), $language);
This will replace all the terms you're looking for.

PHP Get URL Contents And Search For String [duplicate]

This question already has answers here:
Get content from a url using php
(3 answers)
Closed 7 years ago.
In php I need to get the contents of a url (source) search for a string "maybe baby love you" and if it does not contain this then do x.
Just read the contents of the page as you would read a file. PHP does the connection stuff for you. Then just look for the string via regex or simple string comparison.
$url = 'http://my.url.com/';
$data = file_get_contents( $url );
if ( strpos( 'maybe baby love you', $data ) === false )
{
// do something
}
//The Answer No 3 Is good But a small Mistake in the function strpos() I have correction the code bellow.
$url = 'http://my.url.com/';
$data = file_get_contents( $url );
if ( strpos($data,'maybe baby love you' ) === false )
{
// do something
}
Assuming fopen URL Wrappers are on ...
$string = file_get_contents('http://example.com/file.html');
if(strpos ('maybe baby love you', $string) === false){
//do X
}
If fopen URL wrappers are not enabled, you may be able to use the curl module (see http://www.php.net/curl )
Curl also gives you the ability to deal with authenticated pages, redirects, etc.

Categories