I wondering how to translate a URL in ZF2 that has a parameter on it.
For example:
/{:language_link-schools-:city_link}
The reason why I don't do:
/:language_link-{schools}-:city_link
It is because in some languages, for example, Spanish, the order of the words will change.
I am using PhpArray, and when I translate it, the parameters are not replaced, therefore the URL is rendered as (example in Spanish):
/:language_link-escuela-:city_link
Instead of the expected behaviour:
/ingles-escuela-miami
Edit:
The parameters are
:language_link and :city_link
So the idea is that in one language the rendered URL could be:
/:language_link-schools-:city_link
and in another language it could be:
/:language_link-:city_link-school
Similarly as it is done when you translate a statement doing:
sprintf($this->translate('My name is %s'), $name) ;
There is a function in PHP called strtr. It allows translating any pattern into values.
With your example, we can do the following:
If the string is like this: /:language_link-escuela-:city_link
Then you can do the following
<?php
$rawUrl = "/:language_link-escuela-:city_link";
$processedUrl = strtr($rawUrl, [
':language_link' => 'es',
':city_link' => 'barcelona',
]);
echo $processedUrl; // Output: /es-escuela-barcelona
Related
I am using simplexml_load_file function to fetch data from a URL which contains XML content. There are few parameters in URL and simplexml_load_file is working correctly normally.
But there are few parameters which contains & and for these simplexml_load_file function is not working. This is my code. I tried urlencoding function but even this one is not working. This is how my code look like
$api_url = urlencode($api_url);
$xml = simplexml_load_file($api_url);
This is how URL look like
https://atlas.atdw-online.com.au/api/atlas/products?key=MYKEY&ar=Merimbula & Sapphire Coast&cats=ATTRACTION&size=10
In above URL you can see parameter ar has value which contains & and simplexml_load_file is not working for these type of parameters.
Any suggestion regarding this will be much appreciated.
As you can see in your URL, the ampersand (&) has two functions:
As a separator for URL parameters.
As a character that is part of the value of such a parameter.
To avoid the ambiguity that is presented in your question, you generally urlencode() the URL parameter values.
You would then do something like $ar = urlencode('Merimbula & Sapphire Coast');. If you have no control over how the ar parameter is constructed, then you'll have to remove the ampersand from "Merimbula & Sapphire Coast" and replace it with something else (like 'and').
Ive been using URL-parameters to make a landingpage behind a searchform more personal. I felt relatively bulletproof validating stuff like this
$string = $_GET['city']
$res = preg_replace("/[^a-zA-Z0-9]/", "", $string);
until I tried something like ?city=# as a value and my whole page crashed and im not so sure anymore.
What is the way to go to validate without writing a whole engine or at least stop my page crashing from #?
Thanks
PHP has a lot of functionalities which help you avoid problems like this.
Whenever you create URL to be displayed in the browser it has to be urlencoded. If you are just appending the query string part to a fixed url you can build that string with http_build_query. For example:
$querystring = [
'param1' = 123,
'param2' = 'hello with a #'
];
$QS_encoded = http_build_query($querystring);
echo 'My link';
# in URL denotes another part of URL which is the hash part. This is not going to be a part of your $_GET superglobal.
If for any reason you would like to type out the URL with a query string containing # manually by hand, then you need to use the encoded version %23. e.g. http://php.net/manual-lookup.php?pattern=%23
On a side note. You shouldn't use regex for filtering data like this. PHP once again has already an extension for this: filters.
Basically, what I want to achieve is dynamically replace {SOME_TAG} with "Text".
My idea was to read all tags like {SOME_TAG}, put them into array.
Then convert array keys into variables like $some_tag, and put them into array.
So, this is how far I got:
//Some code goes here
$some_tag = "Is defined somewhere else.";
$different_tag = 1 + $something;
Some text {SOME_TAG} appears in different file, which contents has been read earlier.
//Some code goes here
preg_match_all('/{\w+}/', $strings, $search);
$search = str_replace(str_split('{}'),"",$search[0]);
$search = array_change_key_case( array_flip($search), CASE_LOWER);
...some code missing here, which I cant figure out.
Replace array should look something like this
$replace = array($some_tag, $different_tag);
//Then comes replacing code and output blah blah blah..
How to make array $replace contain variables dynamically depending on $search array?
Why not something along the lines of:
<?php
$replace = array(
'{TAG_1}' => 'hello',
'{TAG_2}' => 'world',
'{TAG_3}' => '!'
);
$myString = '{TAG_1} {TAG_2}{TAG_3}{TAG_3}';
echo str_replace(array_keys($replace), array_values($replace), $myString);
If I understand correctly:
You're working on trying to create a customizable document, using {TAGS} in order to represent replaceable areas that can be filled in with dynamic information. At some point in time while replacing the {TAGS} with the dynamic information, you want the dynamic information to be stored in automatically generated basic variable names, as $tags.
I'm not sure why you want to convert these tags to basic variables instead using them entirely as array keys. I would like to point out that this represents a security or functionality hole - what happens if someone puts {REPLACE} in as a tag in your document? Your replace array would get overwritten with dynamic data, and your whole program would fall apart. Either that, or the whole replace array would get dumped in for {REPLACE}, making for a very messy document with perhaps data you don't WANT them to have in it. Perhaps you have this dealt with - I don't have all the context here - but I thought I'd point out the risk factor.
As for a better solution, unless there's some specific need that you're addressing by going through $tags instead of using using the $replace array directly, I like #Emissary's answer.
I think I have the need to take a uri which has been decoded in PHP, and re-encode it.
Here is the situation:
JavaScript passes encoded uri as query string parameter to php script.
PHP script embeds uri as a hidden input value in an html document, responds with the document to a user agent.
JavaScript reads embedded uri and sets location of current document based on value of hidden input.
On Step 2, I am finding that the Uri is fully decoded after reading it in via $_GET. So when I embed the uri in the hidden input, it becomes un-encoded. So I would like to run a PHP script which re-encodes the Uri properly ex:
http://my.example.com/dog walk?is=very great
==>
http://my.example.com/dog%20walk?is=very%20great
Is there a pre-built php function for this or should I just write my own?
PLEASE NOTE: urlencode and urldecode are not the answer to get the desired input/output I have in the example above.
Thanks,
Macy
Are you looking for : http://fr.php.net/manual/en/function.urlencode.php ?
I don't know if will help you, but PHP have 3 useful functions:
$url = parse_url('put the url here');
parse_str( $url['query'], $query ); // generating an array by reference (yes, kinda weird)
echo $query; //in this line, you can encode or decode.
or, if you want to mount a query, you can use http_build_query(); that accepts values from an array, like:
$url = 'http://my.example.com/dog walk?';
$array = Array (
'is' => 'very_great',
);
$url_created = $url . http_build_query($array);
urldecode:
http://www.php.net/manual/en/function.urldecode.php
My current implementation, which is array based stores keys and values in a dictionary, example:
$arr = array(
'message' => 'Paste a flickr URL below.',
);
I realize that it was probably a bad idea storing html inside of a string such as this, but if I'm using gettext then in my .mo/.po files how should I handle storing a similar string? Should I just store words, such as 'Paste a' and 'URL below' and 'flickr' separately?
You should store something like
"Paste a %1 URL below"
and replace all 'vars' using something simple like str_replace('%1', $link, $message);
$link can also be translatable
"%1"
although that might be overkill (does flickr translate between languages?)
rationale behind this is that different languages have different grammatical structures and the ordering of the words wont always be the same.
Update:
as #alex and #chelmertz mention in the comments, try using the sprintf function, which is built for this very thing.
I'd go for this:
$arr = array(
'message' => _('Paste a %s URL below.'),
);
Having all translations as string literals within gettext function calls allows to use standard tools to update *.po catalogues.