This question already has answers here:
Sitemap urls with special characters [closed]
(6 answers)
Closed 9 years ago.
I want to submit my sitemap to Google, but I don't want to mess anything up. I am also having trouble with the URLs to submit; some of them have special characters in them such as the ampersand (&) symbol and parenthesis (). I just want to know what is the correct way to handle them?
I am currently using PHP's urlencode(), which turns them in to %28, %29 and so on which doesn't really look too good and I am scared if I give Google those links and they go on to index them they will index them as
domain.com/blabla%28blabla.html
Rather than
domain.com/blabla&blabla.html
Are you generating the XML by hand? Please consider using something like the PHP DOM classes instead.
You'll actually want to encode ampersands as &, etc., but it's really best to let a library emit well-formed XML for you.
See Generating XML document in PHP (escape characters) for more discussion of this.
urlencode() is the right function to use. You definately don't want ampersands in your URL because they are a special character used to form a URL (for passing GET variables).
Related
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
I am trying to make a card reader for cardcast. I get these cards in a string using file_get_contents and then need to extract the text for each card.
Here is an example piece of the string for one card, each one of these cards is separated by a comma and has the parentheses on either end.
{"id":"030b3406-55ae-4159-9129-04463c61973c","text":["Why does this not work? ",""],"created_at":"2017-06-04T15:13:42+00:00","nsfw":true}
I am trying to extract just the information inside the text tag, in this case ' Why does this not work? ","" ' (minus the single quotes and white space on both ends).
Could someone please help to extract this information using split or regex? I could do this slowly with split but I assume it is more efficient to use regex as there could be multi-hundred cards and this needs to be time efficient.
I looked into preg_match() but did not understand it well enough to get a functioning version.
Thanks
Looks like a JSON encoded text.
Simply decode the text using 'json_decode()'
JSON: JavaScript Object Notation.
JSON is a syntax for storing and exchanging data.
JSON is text, written with JavaScript object notation.
You can learn more about json here :
http://www.json.org/
https://www.w3schools.com/js/js_json_intro.asp
change json to php array with -> $array=json_decode($json,true)
get ur text attribute:
$array['text']
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 7 years ago.
I have have string in php as follows:
shows that he/she is a firm believer in “If it ain’t broke, don’t fix it” or “leave well enough alone”
when I print this string it output something like this:
shows that he/she is a firm believer in “If it ain’t broke, don’t fix it†or “leave well enough aloneâ€
How can i print this exactly as the first one? is there any solution?
Ensure that your source code file uses the same charset as the browser interpreting your file. For example if you store your source code as utf-8, be sure to tell the browser to use utf-8 too:
<meta charset="utf-8">
If the text comes from a database or a file, ensure the table/file uses the same charset too.
try with,
print_r(utf8_encode($text));
This question already has answers here:
Best practice for PHP output
(3 answers)
Closed 9 years ago.
I have an html form that when users fill out, the information is processed and encoded into an email. The issue that keeps arising is when my company receives user emails the encoding turns colons, semi-colons, ampersands and commas into :, ;, & and , which makes emails difficult to read.
As a newcomer to PHP and encoding i'm not sure which approach to take. Should I convert the email to an html format or should I remove the encoding all together? Could someone provide information/guidance on approaching this conflict?
Thank is advance!
Should I convert the email to an html format
It will be better way, use html_entity_decode() to convert : into readable chars
If I'm understanding you right the people filling out the form are putting in punctuation or chars that are being converted to asci. To stop this use:
$yourvariable = htmlspecialchars($_POST['your input field'], ENT_QUOTES);
This should strip out most of the nasty stuff.
You can use HTMLEntities, which converts applicable characters to HTML entities.
Also, strip_tags works, when you want to remove html tags from a message.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UTF-8 all the way through
Either by using file_get_contents() or curl functions, output is wrong, to be specific, east-european č and ć latin characters is always replaced by nasty characters, šđž sometimes, depends if I'm trying something with changing internal encoding of PHP - it only get worse.
Yes, I've read similar topics, but with following these instructions, things get worse.
Does anyone have solution? Thanks in advance.
It's likely that you try to display the data with wrong encoding, if the data your accessing with file_get_contents/curl is a remote html page, read the Content-Type from header or tag and convert the encoding using iconv
This question already has answers here:
PHP String Length Without strlen()
(3 answers)
Closed 3 years ago.
PHP coding standards say:
... PHP holds the length property of each string, and that it
shouldn't be calculated with strlen(). Write your functions in a such
a way so that they'll take advantage of the length property, both
for efficiency and in order for them to be binary-safe. ...
How can I access this length property? Or do I misunderstand it?
As Ignacio mentioned in another post:
They're talking about the C function, not the PHP function. The C
function will stop counting after the first \0, but PHP strings can
contain \0 elsewhere other than the end.
That document is most likely about how to extend PHP's engine, rather than programming in PHP itself.
These coding standards are for not intended for web sites developpers using PHP, but for the developpers of the PHP engine itself.
PHP is developped using the C language, and the strlen function to avoid is the C one, not the PHP one.
It's not a "property" in the sense that it would be in other languages (like C#).
You cannot do:
myString.Length;
Instead you would need to do:
strlen(myString);