I am trying to google a solution for parsing a PHP variable "Joe Soap"
into a html header
I am expecting:
http://myurl.com&name=joe soap
but I am getting
http://myurl.com&name=joe
I know I can not have spaces in my http header but I can't find out how to parse my variable.
I just need a hint so that I can google it.
Regards
Without some code it's guessing work.
I think you need PHP urlencode
$test = 'Joe Soap';
echo 'http://example.com/?name=' . urlencode ( $test );
Which results in: http://example.com/?name=Joe+Soap
2 minutes after I ask the question, I find the answer.
use the urlencode function in php
regards
Related
I am loading an external JSON file. Which seems to load fine. Im using this script to load it:
$file ="https://creator.zoho.com/api/json/los/view/All_clients?
authtoken=xxx";
$bors = file_get_contents($file);
When i dump the results, I get:
string(505) "var zohoappview55 = {"Borrowers":[{"Full_Name":"Mike Smith","Email":"dadf#gmail.com","Address":"111 S. Street Ct., Aurora, CO, 80012","Position":"Borrower","ID":"1159827000004784102","Mobile":"+13033324675","Application":"Application 1 - 1159827000004784096"},{"Full_Name":"Stacy Smith","Email":"sdfa#gmail.com","Address":"111 S. Street, 80012","Position":"Co-Borrower","ID":"1159827000004784108","Mobile":"+1303558977","Application":"Application 1 - 1159827000004784096"}]};"
Looks like the json has a predefined var zohoappview55 at the begining of the json. Not sure if this is my issue but when i use json_decode it doesn't not decode. If i remove this beginning variable it decodes just fine.
i don't have a way to change this variable or edit the json file as it's a remote file. Does anyone know how to decode it in the native format with the variable at the beginning?
Having a quick look through the API documentation of zoho, it seems it should normally return correct json. It may think that it's a browser requesting the file as a javascript source so you may need to add an Accept header to your request.
This cannot be done with file_get_contents so you will probably need to use curl instead.
Try to perform a normal php curl request with the header Accept: application/json.
See: PHP cURL custom headers for reference.
But as Alex Howansky said in the comment. The API might not be intended for that. In that case you will need to strip the beginning and end of the received document.
I have this php code
<?php
$status_code=1;
echo "<?xml version=\"1.0\"?>\n";
echo "<response>\n";
echo "\t<status>$status_code</status>\n";
echo "\t<time>" . time() . "</time>\n";
if ($status_code == 1) {
echo "\t<message>\n";
echo "\t\t<author>Vlad</author>\n";
echo "\t\t<text>Ova e poraka</text>\n";
echo "\t</message>\n";
}
echo "</response>";
?>
Why I don't get the printed xml code in browser?
Also is it good practice to create ajax requests by printing the xml code directly in php or should I use some xml php function to create xml code?
I wanted to create a chat system by using jquery, ajax, php and mysql using this tutorial, but I get error that the above printed xml is not well formed
Try viewing your source, the xml is there but just not visible. Your browser will try to show the page as HTML, since you dont supply the correct headers. Since there are no tags, you wont see anything.
You should send the correct headers, as such:
header('content-type: text/xml')
As the others have said, you need to give the browser a header to let it know how to display the page.
As for xml functions, have a look at the PHP SimpleXMLElement
you have to put header (to let the browser know how to render the display):
header ("Content-Type:text/xml");
You have to use proper content-type, as the others have said.
I recommend you to use XMLReader.
Or if you want a n easier way, then try to use Heredoc string syntax with sprintf and htmlspecialchars to encode values with characters that have special meanings in XML (like <>&)
I'm using a mailto link to send e-mail with a php string variable inside it as follows:
'Would you like to E-mail stakeholders? <a class="emailLink" href="mailto:stakeholders?subject=Alert '.$ticket.'&body=Experience: %0D%0A'.$exp.'">Review and Send!</a><br><br>';
I discovered that in the e-mail client it prints it out like:
Experience:
Th Experience should contain:\r\n\r\nImpact\r\nHow to replicate\r\nComprehensive notes
So I tried:
$charactersToRemove = array("\r\n","\n","\r");
$replaceWith = "%0D%0A";
$exp = str_replace($charactersToRemove,$replaceWith,$exp);
Which doesn't seem to make a difference...Can someone tell me why? Thanks.
Can you post more of your code to show how $exp was assigned it's value?
I've edited out my answer until I can provide better help.
EDIT: Can you seriously try using "\r\n" with \" to escape "'s instead, just in case PHP is somehow localizing %0D$0A in your HTML output? What does the source of your page look like?
It keeps killing me for some time...
I'm new to php and I'm writing a parser for price comparison site, therefore I need to have quite a few variables:
$plae = "<pastwisko>";
$user = "<krowa>";
$product "<trawa>";
But without spaces...
Using or echoing those gives me nothing. I've tried to search stackoverflow, google and php documentation and nothing... maybe my english sucks...
Thou I'll be really greatfull for help
If you are echoing those into HTML then they will be parsed as [incorrect] HTML tags by your browser and will not show. You should use htmlentities to make them display as text: http://php.net/manual/en/function.htmlentities.php
You've forgot a "=".
$product "<trawa>";
// Should be.
$product = "<trawa>";
Edit:
Joe has the correct answer to your problem, I ran the script in the terminal and that was the only error I was given. I didn't think of htmlentities, I'm sorry if my post was irrelevant and unnecessary.
i have a case where i have to send an html code from client side to server side through json object ,but there are some problems caused by symbols inside the html tag such as ",/ ...etc which create errors in the json object. I tried many ways to build the json but none of them were correct .I hop you understand my case clearly .
this is an example of my json structure:
{
"html":"the html code injected dynamical here"
}
as i said my main problem is that i am having problems caused by html symbols how can i solve it! please give me an explicit example on how to solve this problem and pars the html code successfully to json object.
I'm not sure about how you're building the JSON objects but if you're using json_encode() and json_decode() you shouldn't have any problems with HTML characters, they'll be escaped properly.
Not sure If I misread the question, but to pass json from server to client, use json_encode()
http://php.net/manual/en/function.json-encode.php
This should work for you and take care of all the escaping.
<?php
header('Content-type: application/json');
$json = array(
'html' => '<h1 style="color:#0F0">Hello World!</h1>'
);
echo json_encode($json);
exit;
Hmm, just learned this tidbit from reading the docs:
json_encode() won't work with
character sets other than UTF-8
I can't see why you'd need to send json to the server, maybe you can share the reason with us or someone can enlighten me in the comments?
Perhaps you could use url encoding.
You can encode the html. Some html encoding and decode can be find here:
http://code.google.com/p/jsool/source/browse/jsool-site/js/util/Encoder.js?r=176
You just need the html.encode and html.decode functions, that way you can do something like
function html_encode(string){
return string.replace(/./g,function(chr, index){
return chr.match(/[\w\d]/) ? chr : "&#"+chr.charCodeAt(0)+";" ;
});
},
function html_decode(string){
return string.replace(/&#[0-9]+;/g,function(text,index){
return String.fromCharCode(text.match(/[0-9]+/)[0]);
});
}
That way you can do
var html = html_encode(myhtml);
{"html": html}
to encode and recover the values using html_decode().
PS.: Sry, i didnt tested out the html_encode and decode functions, but its easy to find some at google.
If you are using jQuery this js library might help. http://www.rahulsingla.com/blog/2010/05/jquery-encode-decode-arbitrary-objects-to-and-from-json
In your javascript before you send the request do this:
var thing = {plugin: 'jquery-json', other: "blah-blah"};
var encoded = $.JSON.encode(thing);
Then after you send the request to php, do this to decode:
var thing = json_decode($_POST['thing']);
You can read more on this here