I am trying to pull event data into my full calendar using php. I have successfully retrieved the data from the database and converted it to json but I have one problem.
Here is the json outputted:
[
{"id":"53","start":"2013-06-06","title":"Assignment2"},
{"id":"52","start":"2013-06-07","title":"Assignment1"},
{"id":"54","start":"2013-06-08","title":"Assignment3"}
]
So when I want to put it into my fullcalendar i do this:
var class_id = $("#calendar").attr('c_id');
$("#calendar").fullCalendar({
dayClick:function(data){
},
events: '/classes/get_due_dates/'+c_id
});
When I did this nothing was showing up on the calendar but when I copied and pasted the output and removed the quotes surrounding id, start and title it worked fine
Like so:
[
{id:"53",start:"2013-06-06",title:"Assignment2"},
{id:"52",start:"2013-06-07",title:"Assignment1"},
{id:"54",start:"2013-06-08",title:"Assignment3"}
]
Notice the quotes removed, So my question is how do i convert the output that I am getting and remove those quotes so I can display these events on my calendar?
Thanks!
Try this if it is what you are looking for.
<?php
$str = '[
{"id":"53","start":"2013-06-06","title":"Assignment2"},
{"id":"52","start":"2013-06-07","title":"Assignment1"},
{"id":"54","start":"2013-06-08","title":"Assignment3"}
]';
$str = str_replace('{"', '{', $str);
$str = str_replace('":', ':', $str);
$str = str_replace(',"', ',', $str);
echo "<pre>" . $str . "</pre>";
?>
Related
I have a problem with my code, i have this code that create image from external source of image & string. I used json to get the string.
My problem is if i used the string from json data i could not get the proper wrapping of string like this:
http://prntscr.com/dbhg4n
$url = 'https://bible-api.com/Psalm100:4-5?translation=kjv';
$JSON = file_get_contents($url);
$data = json_decode($JSON);
$string = $data->text;
But if i declare and set string directly i got the output that i want like this:
http://prntscr.com/dbhg7q
$string = "Enter into his gates with thanksgiving, and into his courts with praise: be thankful unto him, and bless his name. For the Lord is good; his mercy is everlasting; and his truth endureth to all generations.";
I dont think the error or the problem is on the code for wrapping the text on my image. I think it is on the json data. How can i fix this?
The text has \n symblols. Just replace them:
$string = preg_replace("/\n/", ' ', $data->text);
or without a regular expression:
$string = str_replace("\n", ' ', $data->text);
I am basically trying to transform any hash-tagged word in a string into a link:
Here is what my code looks like:
public function linkify($text)
{
// ... generating $url
$text = preg_replace("/\B#(\w+)/", "<a href=" . $url . "/$1>#$1</a>", $text);
return $text;
}
It works pretty good excepting the case when that $text contains a single quote. Here are
Example1:
"What is your #name ?"
Result: "What is your #name?" Works fine.
Example2:
"What's your #name ?"
Result: "What's your #name?" Does not work, I want
this result: "What's your #name?"
Any idea about how I can get rid of that single quote problem using PHP ?
EDIT1:
Just for info, before or after html_entity_decode($text) I got
"What's your #name?"
Something like this.
$string = "' \'' '";
$string = preg_replace("#[\\\\']#", "\'", $string);
Something is protecting your html entities. This can save your life if the string is coming from a get/post request - but iI it's from a trusted source just use html_entity_decode to convert it back. This 39-thing is a way to express the single quote as you might have realized.
if the problem is html_entities, then maybe you only need to html_entity_decode your $text
$text = preg_replace("/\B#(\w+)/", "<a href=" . $url . "/html_entity_decode($1)>#$1</a>", $text);
Thanks all for your suggestions, I've finally sorted this out with this :
html_entity_decode($str, ENT_QUOTES);
I'm using textarea to get data that I insert into a database.
I'm using htmlspecialchars() to get rid of the single quotes and double quotes but it doesn't convert new lines into something so I'm left with a very long piece of code that doesn't have new lines and looks messy.
I've checked the manual but I can't find how to convert it.
How would I do this?
EDIT:
My intended output is the same as what the user inputted.
So if they inputted into the textarea...
Hi
This is another line
This is another line
It would store into the database like...
Hi\r\nThis is another line\r\n This is another line.
or something like that.
Then when I echo it again then it should be fine.
Anthony,
If you are referring to when you get it back out and you want it to look nice, and you aren't putting it back into a textarea, you can use the mythical function nl2br() to convert new line characters into HTML characters.
$data = 'Testing\r\nThis\r\nagain!\r\n';
echo nl2br($data);
This results in:
Testing
This
again!
I believe what you are looking for is
nl2br($string);
That will convert the returns to <br> tags
I will also give you this script that has worked well for me in the past when nl2br does not.
$remove = array("\r\n", "\n", "\r", "chr(13)", "\t", "\0", "\x0B");
$string = str_replace($order, "<br />", $string);
It should be:
<?php
addslashes( strip_tags( nl2br( $data ) ) );
?>
addslashes : will escape quotes to prevent sql injection
strip_tags : will remove any html tags if any
nl2br : will convert newline into <br />
I have this test.php where i have this info :
callername1 : 'Fernando Verdasco1'
callername2 : 'Fernando Verdasco2'
callername3 : 'Fernando Verdasco3'
callername4 : 'Fernando Verdasco4'
callername5 : 'Fernando Verdasco5'
this page automatically changes that name every 10 min
In this another page test1.php
I need a php code that takes only the name of the callername3 and echo'it
Fernando Verdasco3
I've tried this like so test1.php?id=callername3
<?php
$Text=file_get_contents("test.php");
if(isset($_GET["id"])){
$id = $_GET["id"];
parse_str($Text,$data);
echo $data[$id];
} else {
echo "";
}
?>
but no result.
Is there any other option?
If i have "=" instade of ":"
callername1 = 'Fernando Verdasco1'
callername2 = 'Fernando Verdasco2'
callername3 = 'Fernando Verdasco3'
callername4 = 'Fernando Verdasco4'
callername5 = 'Fernando Verdasco5'
And i use This php Code it works
<?php
$Text=file_get_contents("test.php")
;preg_match_all('/callername3=\'([^\']+)\'/',$Text,$Match);
$fid=$Match[1][0];
echo $fid;
?>
i need this to work with ":"
Help?
You should store data in a file with the .php extension, since it's not executable PHP. I looks like you're going for the JSON syntax.
Since you need it to work with ':' I assume, for whatever reason, you can't change the format. Your example with '=' works because of the regexp:
preg_match_all('/callername3=\'([^\']+)\'/',$Text,$Match);
This says, match text like callername3= followed by a ' followed by one or more chars that are not a ' followed by a final '. Everything between the 's is stored in $Match[1][0] (if there were more parts in brackets they be stored in $Match[2][0], etc).
Your example doesn't work since it doesn't account for the spaces before and after the = sign. But we can fix that up and change it to work for : like this:
preg_match('/callername3\s*:\s*\'([^\']+)\'/',$Text,$Match);
echo $Match[1] ."\n";
This displays:
Fernando Verdasco3
And what that regular expression is match text that start callername3 followed by any amount of whitespace (that's the \s*) followed by a :, followed by any amount of whitespace, followed by a name in quotes (that is stored in $Match[1], this is the area of the regular expression enclosed in parenthesis).
I've also used just preg_match because it looks like you only need to match one example.
There is a rather simple approach to tihs:
$fData = file_get_contents("test.php");
$lines = explode("\n", $fData);
foreach($lines as $line) {
$t = explode(":", $line);
echo trim($t[1]); // This will give you the name
}
I want to convert special characters to HTML entities, and then back to the original characters.
I have used htmlentities() and then html_entity_decode(). It worked fine for me, but { and } do not come back to the original characters, they remain { and }.
What can I do now?
My code looks like:
$custom_header = htmlentities($custom_header);
$custom_header = html_entity_decode($custom_header);
Even though nobody can replicate your problem, here's a direct way to solve it by a simple str_replace.
$input = '<p><script type="text/javascript"> $(document).ready(function() { $("#left_side_custom_image").click(function() { alert("HELLO"); }); }); </script></p> ';
$output = str_replace( array( '{', '}'), array( '{', '}'), $input);
Demo (click the 'Source' link in the top right)
Edit: I see the problem now. If your input string is:
"{hello}"
A call to htmlentities encodes the & into &, which gives you the string
"{hello}"
The & is then later decoded back into &, to output this:
"{hello}"
The fix is to send the string through html_entity_decode again, which will properly decode your entities.
$custom_header = "{hello}";
$custom_header = htmlentities($custom_header);
$custom_header = html_entity_decode($custom_header);
echo html_entity_decode( $custom_header); // Outputs {hello}