Replace stylesheet over other webpage's own stylesheet - php

I am building the website www.verbum.xtrweb.com/verbum.php
I use the request function in php to get the definition of spanish words from the site: www.rae.es . The output i get is in an , but unfortunately, i cannot change the style of the elements it outputs. For example, if you look up the word "hola" you will get the meaning for "hola" but with www.rae.es 's stylesheet. What i want is to apply to this output my own style (color, font, font size, etc.)
I now there is something called $important! but according to the way i have previously tested, it did not work.
I evoke the magic aid of the stackoverflow community with urgence.

The content you want to change is located in an iframe. I would suggest that you just inject the stylesheet into the iframe direct, but unfortunately the content you want is on a different domain. Because of the cross-domain security policy, you will not be able to modify the content of the iframe directly.
There are two ways to solve this. You can use a PHP application to get the content you need, then you can filter it and change it in any way you would like. This would replace the iframe, and instead you would just display the content you need on the page. The other option is to use a more simple PHP application that gets the content, but doesn't modify it at all. Then you point your iframe to this PHP app, and it would display in the same way as it does now, BUT it will be from the same domain. Then you can inject a stylesheet with Javascript and make it look any way you want.
The "important!" CSS override might be needed if you go with the second method I mentioned, but it is not the root cause of your problem. If you want to read more about how to use important!, I recommend this article.
Below I have implemented an example of the first method I described. This will download the requested page, then inject a custom set of styles.
<?php
$url = 'http://lema.rae.es/drae/srv/search?id=IwxflJmT9DXX2DMkYs8Z';
$css = <<<EOT
<style type="text/css">
body
{
background: #eeeeee;
}
.a
{
color: green;
}
.f
{
font-size: 200%;
}
.o
{
font-size: 80%;
}
img
{
opacity: .5;
}
img:hover
{
opacity: 1;
}
</style>
EOT;
$data = file_get_contents($url);
$data = str_replace('</head>', $css.'</head>', $data);
echo $data;
?>

Related

If an <a> link is a PDF file, style accordingly

I am working on a website for a client, who is an exam board. They have a number of content pages with summary details about their syllabuses, and need to link directly to PDF Syllabus files from within the text.
There is also a page just for PDF files, and I am currently making those Custom Post Types in WordPress, but those have their own icon setup that I don't want being affected by the PHP rule that I'm about to describe.
(This is more of a PHP question than a WordPress question, so don't worry.)
Let's say I have a link, New Syllabus. With WordPress, one is unable to add a class to the link, without knowledge of HTML, which in the case of my client and its rather old-school employees, is non-existent. They will be shown how to add links to PDF files using the Add Media button in the WordPress post or page editor, which will simply create a link to the .pdf file. This issue here is that it's just a link, and the client wants users to immediately know that it's a PDF file, and not a page, to make it easier to find the relevant syllabus files. Therefore, I've proposed creating a rule that adds a .png icon next to any link that is linking to a .pdf file, and it'll change the link colour as well.
Does anyone have any idea how I could achieve this? I've looked for WordPress plugins that do this automatically, but they're either outdated or hard to use (i.e., employees will need HTML knowledge to restyle the links.)
I found this code snippet online, but don't know if it's the right sort of direction.
$whitelist = array(".pdf");
foreach ($whitelist as $item) {
if (preg_match("/$item\$/i", $_FILES['uploadfile']['name'])) {
}
else {
redirect_to("index.php");
}
}
Thoughts?
This can be done with just CSS and it supports all major browsers including IE7+. There is no need to add any extra classes or <img> elements.
The CSS below will target any link to a .pdf, change the link color, give it some left padding and apply a background image in the space created:
a[href$=".pdf"] {
background: url(images/pdf-icon.png) no-repeat 0 50%;
padding-left: 25px;
color: red;
}
Demo: http://jsfiddle.net/xuBpG/1/
Try this:
Add this code in your functions.php file.
add_filter('media_send_to_editor', 'my_filter_pdf', 20, 3);
function my_filter_pdf($html, $id) {
$attachment = get_post($id); //fetching attachment by $id passed through
$mime_type = $attachment->post_mime_type; //getting the mime-type
if ($mime_type == 'application/pdf') { //checking mime-type
$src = wp_get_attachment_url( $id );
$html = 'File';
return $html; // return new $html
}
return $html;
}
This is a function which will add a class to the .pdf anchor tag files when you will insert pdf file into the post.
Using #tw16's answer provides a simple and similar way to use Font Awesome icons rather than static images. For those looking to use Fontawesome or some other icon library that supports unicode, try this:
/* MIME TYPE ICONS */
a[href$=".pdf"]::after {
font-family: "fontawesome";
content: "\0020\f1c1";
color: inherit
}
a[href$=".doc"]::after {
font-family: "fontawesome";
content: "\0020\f1c2";
color: inherit
}
a[href$=".xls"]::after {
font-family: "fontawesome";
content: "\0020\f1c3";
color: inherit
}
Of course, you could use the ::before or ::after pseudo classes depending on where you want the placement of the icon to be (left or right).
Thanks tw16 for the simplistic approach to this.

How to style a dynamic html created by PHP

My php page will generate a dynamic html.
The following is the output;
<label class="required">suject *</label>
Unfortunately i can't edit the html
Now I need to show '*' in red color.
How I can write the css for that?
There are several ways to do this:
If you have access to the PHP source code, then you can modify the output accordingly, perhaps wrap the * with <span> tags and style the accordingly.
Alternative, you can use JS to do the same:
$(document).ready(function() {
// Loop through all labels with required class
$('label.required').each(function() {
$(this).html($(this).html().replace(/\*/g, '<span class="asterisk">*</span>'));
});
});
And in your CSS, you can style it in any way you want:
.required .asterisk {
color: red;
}
See fiddle here - http://jsfiddle.net/teddyrised/JTesR/
Even better: Using CSS
This is written in light of trying to be as semantically correct as possible - you can remove the * character with JS (or changing the PHP code), but use a pseudo-class to add it back to the label element:
label.required:after {
content: '*';
color: red;
}
CSS allows you to select elements (and a limited number of pseudo-elements such as :first-line).
There is no way to select "last character", "characters matching *" or anything else that would make what you want achievable.
You have to modify the DOM. The easiest way to do that would be to modify the HTML it is generated from. The hacky approach would be to modify it with JavaScript after it has loaded.
This is based on Terry's answer: You can simply use :after to simulate effect, and place that content over generated HTML.
label.required:after {
content: '*';
color: red;
background:white;
position: relative;
left: -8px;
}
http://jsfiddle.net/x2KN7/
Its not possible through CSS, but you can do something with the javascript. If this content is generated on page load then you can hook page load event, get reference to this element and make changes.
<script type='text/javascript'>
document.onload = function (e) {
var col = document.getElementsByClassName("required");
for (var i=0;i<col.length;i++) {
if (col[i].innerHTML == "subject *") {
col[i].innerHTML = "subject <span style='color:red'>*</span>";
}
}
}
</script>
Try the above, hopefully it will solve your problem. I have used simple javascript, but if you know a javascript library (YUI, dojo, jquery) then you can do this in 1 or 2 line.

Set default zoom for web page programmatically?

Is it possible to set the default zoom level on a site? For instance, could I code my site in such as a way that it is zoomed to 125% when a user opens it?
My website body has this code
<body ID="phpbb" class="section-{SCRIPT_NAME} {S_CONTENT_DIRECTION}">
How to put this zoom code inside?
Add zoom: 125%; to body style
body {
color: #536482;
background-color: white;
zoom: 125%;
}
This does not directly answer your question, but is an alternative that I recommend considering.
If you use relative sizing for your page (such as em), then you can set the base size of the site in one place, and the whole page will scale up and down accordingly.
For instance, if I want 125% of default size:
body { font-size: 1.25em }
Then, suppose I want a reasonable amount of margin around a header <div>:
#header { margin: 1em }
If I then go back and change that base size on the body to something else, the margin on my header will scale with it. If you do your entire page in relative units, this becomes very easy to do.
You might want to check the zoom CSS attribute. Bear in mind however that it is part of CSS3 and that, therefore, you might find it to behave oddly on old IEs. This is also completely separate from the interface zoom.
webView.setInitialScale((int) getResources().getDimension(R.dimen._50sdp));
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.loadDataWithBaseURL(null,htmlContent,"text/html","UTF-8", null);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setDisplayZoomControls(true);
webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);

Is it considered bad practice to write an element's style directly with PHP?

I know that it is usually considered bad practice to mix CSS with HTML and that CSS selectors (ids, classes, etc) should generally be used to style elements in external stylesheets. But this can be quite inconvenient when writing a dynamic page in PHP. It is much easier to output the element's style directly using PHP by writing to the element's style attribute. Would this be considered bad practice, even if using classes would make it even more difficult to manage from the developer's end?
For example, I have an element's color style store in a database and this element has a different color depending on the parameters provided in the dynamic PHP page. In this scenario, I could write to the css stylesheet with php some class with the color variable I get from the database
echo '.custom-color {';
echo 'color: ' . $colorFromDatabase . ';';
echo '}';
and then append the class to the element:
<div class="custom-color"></div>
or I could just echo the style directly to the element.
echo '<div style="color:' . $colorFromDatabase . ';" >';
Which is the better way to go?
Writing the style inline is usually considered bad, whether it's with PHP or by hand. It is simpler but harder to maintain and you don't get to cache your CSS content.
If you want the CSS to be more dynamic , you should experiment with http://sass-lang.com/
Note that custom-color is not a very good class name, it should be something like main-header, blog-post, last-modified since they tell you about the content not about its style.
SASS would let you define a variable for a color and reuse that within your CSS files, you could then just change the variable names and regenerate your CSS files
The advantage of what you're doing is that you don't have to learn new things. PHP will get it done. You need to write something into your build if you want to use SASS with content from a database and come up with a scheme for which CSS to use.
Note that there are a few cases where it would make sense to write the attribute directly, but it's hard to explain when exactly. It's usually when it's only going to be used once.
Example from their page
// Variable Definitions
$page-width: 800px;
$sidebar-width: 200px;
$primary-color: #eeeeee;
// Global Attributes
body {
font: {
family: sans-serif;
size: 30em;
weight: bold;
}
}
// Scoped Styles
#contents {
width: $page-width;
#sidebar {
float: right;
width: $sidebar-width;
}
#main {
width: $page-width - $sidebar-width;
background: $primary-color;
h2 { color: blue; }
}
}
#footer {
height: 200px;
}
Why not both? Separated CSS from HTML and PHP in your CSS file?
You might also check out CSS-preprocessors. LESS is the most common one, but there's also SASS, Stylus and whatnot.
For the best semantic use, I would use this:
div.colored {
color: attr(data-color)
}
Then use PHP like:
<div class="colored" data-color="<?= $color ?>"></div>
And you could always back this up with some simple Javascript.

php css-less dynamically changeable css designed websites

I am going to start on a website whose requirement is to change the color scheme after every 2 weeks.
I am looking for a dynamic solution to change colours and somewhat structure of a website using css & php.
One solution which i can see is using dynamic css method for example
<?php
header("content-type: text/css");
$mencolour = "#ff0000";
echo 'h1 {color:$menucolor}
?>
Other solution is using some php classes to do the same task.
such as one is available on phpclasses website.
http://www.phpclasses.org/package/6482-PHP-Parse-and-process-Leaner-CSS-files.html
Is there any other better way of doing this? if any one has used above two methods, what could be drawbacks of using them.
Need some expert opinions :)
Sass is a popular CSS pre-processor that, among other things, lets you use variables in CSS, for things like your color scheme. You'd compile the CSS when you change it, so no need for the overhead of running a PHP script each time it loads. (Yeah, you could write your own cache system for that in PHP, but no need to redo others' hard work ;D)
$menu-color: #123456;
#menu { color: $menu-color; }
You can use a body class to change the theme:
/* Base style */
h1 { color: grey; }
.spring h1 { color: green; }
.summer h1 { color: yellow; }
.fall h1 { color: orange; }
.winter h1 { color: blue; }
To change the theme, just add the class on the body:
<body class="fall">
<h1>The leaves are falling!</h1>
</body>
Given that this is on a 2-week schedule, firing off a dynamically generated css file is overkill. It would be far easier to serve up a static .css file, which can be generated by PHP. That way you don't have to mess with outputtting cache headers and whatnot - the file will be cached the way any other static file would.
Use cron a similar timed-job tool to rebuild the .css file whenever you need those color changes to occur.
The PHP method, by default, would cause the clients to re-query the server for changes on every hit. That's a waste of bandwidth and cpu time just to change a few values once every 14 days.

Categories