convert from windows-1256 to utf8 withe iconv php - type-conversion

i want to see if the string will convert from windows-1256 to utf-8 befor saving it in data base but the result is not correct
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Title of the document</title>
</head>
<body>
<?php
$text="ÇáÇäÊãÇÁ";
$utf8 = iconv('windows-1256', 'UTF-8//TRANSLIT', $text);
echo $utf8;
?></body>
</html>

Related

Passing variable to layout template in Mojolicious

I am trying to set the refresh rate of a status page using a stash variable
So I have the following code in the controller to render the page:
my $refreshrate = 10;
$self->stash( refreshrate => $refreshrate);
$self->render();
in the html template I have:
% layout 'refreshLayout', title => 'A title';
% content_for refresh => begin
% end
<div>....body content .../<div>
and in the layout template I have:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
<meta charset="UTF-8"/>
<meta http-equiv="refresh" content="<%= refreshrate %>"/>
.....
<title><%= title %></title>
</head>
<body><%= content %></body>
</html>
I always end up with a page rendered with no value in the content attribute of the refresh meta tag.
It looks like the refreshrate replacement does not work.
However both the content and title replacements do work.
So what am I doing wrong here?
And while we are here, why in the layout template do we do use <%= content %> (and not $content)
While in the template of the regular page we use the more logical <%= $variable %>
How does the replacement work in the layout template versus the replacement in the template of the main page?
The stash values appear as perl variables in your templates. (title is special as it's also a default helper function).
If you want to pass the stash value for refreshrate to the layout, you have to add it to your layout() args:
use Mojolicious::Lite;
get '/' => sub {
my $c = shift;
$c->stash (refreshrate => 10),
$c->render(template => 'foo/bar')};
app->start;
__DATA__
## foo/bar.html.ep
% layout 'mylayout', title => 'Hi there',refreshrate => $refreshrate;
Hello World!
## layouts/mylayout.html.ep
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
<meta charset="UTF-8"/>
<meta http-equiv="refresh" content="<%= $refreshrate %>"/>
<title><%= $title %></title></head>
<body><%= content %></body>
</html>
See Layouts in Rendering doc

How to loop through json and replace values with grep & sed in multiple files

I got a reference json file called reference.json with key/values that I want to replace in .html files.
{
"a.js": "first.1a.js",
"b.js": "second.2b.js",
"c.js": "third.3b.js"
}
Then I got 3 different html files
test1.html
<!DOCTYPE html>
<html lang="en">
<head>
..
<body>
<div class="container">
<h1>file 1</h1>
</div>
<script src="a.js"></script>
<script src="b.js"></script>
<script src="c.js"></script>
</body>
</html>
test2.html
<!DOCTYPE html>
<html lang="en">
<head>
...
</head>
<body>
<div class="container">
<h1>file 2</h1>
</div>
<script src="a.js"></script>
<script src="b.js"></script>
<script src="c.js"></script>
</body>
</html>
And a file in a folder folder1/test3.html
<!DOCTYPE html>
<html lang="en">
<head>
...
<body>
<div class="container">
<h1>file 3</h1>
</div>
<script src="../js/a.js"></script>
<script src="../js/b.js"></script>
<script src="../js/c.js"></script>
</body>
</html>
How can I search through all of my .html files and replace with values from my json with grep and sed?
Assuming your reference.json is well formatted, this one-liner will do it for you :
$ CMD=" sed -i $( cat reference.json | grep ":" | sed 's/ //g;s/,//g;s/"//g' | sed 's/:/\//g' | awk '{printf("s/%s/g;",$0)}' )" && find . -name "*.html" -exec ${CMD} {} \;
Explanation :
Use the reference.json to form an sed substitution expression to use it in an sed command. Use find command to find all the "*.html" files and perform the substitution.
If the reference.json is not formatted, you can use the following command to format it :
$ python -m json.tool reference.json

PHP > Unable to embed youtube video by function

I don't know what's wrong with my embed script, to my logic it should properly embed the video in the frame, but it loads a framed view of www.youtube.com instead of my video video.
There are 2 files in a single directory:
ClassMedia.php:
<?php
class Media {
public function embedYT($code){
echo "<iframe width='560' height='349' src='http://www.youtube.com/embed/".$code." frameborder='0' allowfullscreen></iframe>";
}}
Demo.php:
<?php include "classMedia.php"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Demo</title>
</head>
<body>
<?php
$media = new Media();
$code = "XSGBVzeBUbk";
$media-> embedYT($code);
?>
</body>
</html>
You're missing a single quote right after src='http://www.youtube.com/embed/".$code.", you want this:
echo "<iframe width='560' height='349' src='http://www.youtube.com/embed/".$code."' frameborder='0' allowfullscreen></iframe>";
Note the added single quote.
YouTube ends up seeing a bad URL (http://www.youtube.com/embed/$code frameborder= where $code is the real code) and hands you the homepage instead of what you think you were asking for.

How do you create case insensitive routes in Sinatra?

I'm playing around with Sinatra, and I would like to make one of my routes case insensitive. I tried adding the route like this:
get "(?i)/tileflood/?" do
end
But it doesn't match any permutation of /tileflood as expected. I tested the following regex on rubular.com, and it matches just fine. Am I missing something?
\/(?i)tileflood\/?
You want a real regexp for your route:
require 'sinatra'
get %r{^/tileflood/?$}i do
request.url + "\n"
end
Proof:
smagic:~ phrogz$ curl http://localhost:4567/tileflood
http://localhost:4567/tileflood
smagic:~ phrogz$ curl http://localhost:4567/tIlEflOOd
http://localhost:4567/tIlEflOOd
smagic:~ phrogz$ curl http://localhost:4567/TILEFLOOD/
http://localhost:4567/TILEFLOOD/
smagic:~ phrogz$ curl http://localhost:4567/TILEFLOOD/z
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body { text-align:center;font-family:helvetica,arial;font-size:22px;
color:#888;margin:20px}
#c {margin:0 auto;width:500px;text-align:left}
</style>
</head>
<body>
<h2>Sinatra doesn't know this ditty.</h2>
<img src='/__sinatra__/404.png'>
<div id="c">
Try this:
<pre>get '/TILEFLOOD/z' do
"Hello World"
end</pre>
</div>
</body>
</html>
smagic:~ phrogz$ curl http://localhost:4567/tileflaad
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body { text-align:center;font-family:helvetica,arial;font-size:22px;
color:#888;margin:20px}
#c {margin:0 auto;width:500px;text-align:left}
</style>
</head>
<body>
<h2>Sinatra doesn't know this ditty.</h2>
<img src='/__sinatra__/404.png'>
<div id="c">
Try this:
<pre>get '/tileflaad' do
"Hello World"
end</pre>
</div>
</body>
</html>

FBML and HTMLParser error

The below code:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:fb="http://www.facebook.com/2008/fbml"
xml:lang="en" lang="en">
<head>
<title>FB Test</title>
</head>
<body>
Test
<fb:serverfbml style="width: 350px;">
<script type="text/fbml">
<fb:connect-form action="http://127.0.0.1/post_invite">
</fb:connect-form>
</script>
</fb:serverfbml>
</body>
</html>
Results in the following error:
- Warning: Compilation failed
- Warning: <class 'zope.tal.htmltalparser.NestingError'>: Open tags <html>, <body>, <fb:serverfbml>, <script> do not match close tag </fb:connect-form>, at line 16, column 4
PTRuntimeError: ['Compilation failed', u"<class 'zope.tal.htmltalparser.NestingError'>: Open tags <html>, <body>, <fb:serverfbml>, <script> do not match close tag </fb:connect-form>, at line 16, column 4"]
Yet the structure seems valid to me...
You can't put tags inside of a <script> tag, and the strict ZPT parser is complaining about that. You'll have to somehow escape the contents, like with a tal:content="structure string:" construct:
<script type="text/fbml" tal:content="structure string:
<fb:connect-form action="http://127.0.0.1/post_invite"<
>/fb:connect-form<
"></script>
The script tag must not contain xml to my knowledge.
You could enclose the contents in xml comments and see if that works.